libgpiod API usage (Linux shared libraries) On Eclipse- Solving Problem with libgpiod — undefined reference to its function — skipping incompatible
It is hard to believe that after 3 days of struggling, I had run out of ideas and I tried every suggested solution on the internet, but I just wanted to try the last possible idea to fix the problem. So,
If we define our problem,
first of all; I am developing and testing my code on the target device(MaaxBoard-I.MX8 based on arm64)by cross-compiling. When I tried to include <gpiod.h> file.
Firstly, I got this error:
undefined reference to `gpiod_ctxless_find_line’
/home/ubu/eclipse-workspace/Maax-B-C/Debug/../src/Maax-B-C.c:32: undefined reference to `gpiod_chip_open’
/home/ubu/maaxboard/gcc-arm-10.3–2021.07-x86_64-aarch64-none-linux-gnu/bin/../lib/gcc/aarch64-none-linux-gnu/10.3.1/../../../../aarch64-none-linux-gnu/bin/ld: /home/ubu/eclipse-workspace/Maax-B-C/Debug/../src/Maax-B-C.c:33: undefined reference to `gpiod_chip_open’
/home/ubu/maaxboard/gcc-arm-10.3–2021.07-x86_64-aarch64-none-linux-gnu/bin/../lib/gcc/aarch64-none-linux-gnu/10.3.1/../../../../aarch64-none-linux-gnu/bin/ld: /home/ubu/eclipse-workspace/Maax-B-C/Debug/../src/Maax-B-C.c:37: undefined reference to `gpiod_chip_get_line’
/home/ubu/maaxboard/gcc-arm-10.3–2021.07-x86_64-aarch64-none-linux-gnu/bin/../lib/gcc/aarch64-none-linux-gnu/10.3.1/../../../../aarch64-none-linux-gnu/bin/ld: /home/ubu/eclipse-workspace/Maax-B-C/Debug/../src/Maax-B-C.c:38: undefined reference to `gpiod_chip_get_line’
/home/ubu/maaxboard/gcc-arm-10.3–2021.07-x86_64-aarch64-none-linux-gnu/bin/../lib/gcc/aarch64-none-linux-gnu/10.3.1/../../../../aarch64-none-linux-gnu/bin/ld: /home/ubu/eclipse-workspace/Maax-B-C/Debug/../src/Maax-B-C.c:39: undefined reference to `gpiod_chip_get_line’
After short searching, I came across a solution like this:
You missed the library for linking with
-l
flag.Compile it like this:
gcc -lgpiod test.c
I had added “gpiod” keyword into the section in which the project properties => C/C++ Build => Cross GCC linker => Libraries => under “-l” on eclipse before I compiled. That was successful. But This time I had another frustrating error, That message isn’t actually an error — it’s just a warning that the file in question isn’t of the right architecture (e.g. 32-bit vs 64-bit, wrong CPU architecture). The linker will keep looking for a library of the right type.
Secondly,
skipping incompatible
/home/ubu/maaxboard/gcc-arm-10.3–2021.07-x86_64-aarch64-none-linux-gnu/bin/../lib/gcc/aarch64-none-linux-gnu/10.3.1/../../../../aarch64-none-linux-gnu/bin/ld: skipping incompatible /home/ubu/MaaxBoard/libgpiod/build/lib/libgpiod.so when searching for -lgpiod
/home/ubu/maaxboard/gcc-arm-10.3–2021.07-x86_64-aarch64-none-linux-gnu/bin/../lib/gcc/aarch64-none-linux-gnu/10.3.1/../../../../aarch64-none-linux-gnu/bin/ld: skipping incompatible /home/ubu/MaaxBoard/libgpiod/build/lib/libgpiod.a when searching for -lgpiod
So, This error was the main problem for me, which I struggled with for 2 days. Dealing with this problem, I reached my target device file system(/usr/lib/aarch64-linux-gnu) and found libgpiod.so library under that file path. I copied it to my host machine and I added it as a library on Eclipse into the same location as well “gpiod” keyword above. Only the section is different: you must be sure that you add this library path into the project properties => C/C++ Build => Cross GCC linker => Libraries => under “-L”
for example, it was for me:
/home/ubu/MaaxBoard/libgpiod/build/lib64