3

Does anyone have any experience with the libgpiod API? I have installed it on my linux platform but am unable to call its library functions.

I installed by:

git clone git://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git
cd libgpiod
./autogen.sh
make
make install

Afterwards, I see the libgpiod.so library in /usr/local/lib/ and gpiod.h in /usr/local/include/ (for good measure I ran ldconfig as well). However, when I try to compile the following:

test.c

#include <gpiod.h>

struct gpio_chip *chip;    

int main(void)
{
    chip = gpio_chip_open("/dev/gpiochip4");
    return 0;
}

I get the following error: undefined reference to 'gpiod_chip_open'

Can anyone help me see where I am going wrong?

Many thanks in advance!

Bastinoboy
  • 63
  • 1
  • 4
  • How did you compile? Command? – Azeem Apr 13 '20 at 11:12
  • Using `gcc test.c` – Bastinoboy Apr 13 '20 at 11:18
  • Not linking? Try: `gcc -lgpiod test.c` – Azeem Apr 13 '20 at 11:21
  • Thank you! That compiled. But when I run, I am told `cannot open shared object file: No such file or directory` ... – Bastinoboy Apr 13 '20 at 11:36
  • `libgpiod` is a part of all main Linux distributions. Why do you want to compile it yourself? (Yes I have an experience with it, but I never compiled `libgpiod` myself) – 0andriy Apr 13 '20 at 11:54
  • @Bastinoboy: Check `LD_LIBRARY_PATH` if the `.so` file path is listed in it. Generally, `/usr/lib` and `/usr/lib64` contain the SO files and/or their symlinks. You need to cofirm that to run th executable. Look at this relevant thread: https://unix.stackexchange.com/questions/22926/where-do-executables-look-for-shared-objects-at-runtime – Azeem Apr 13 '20 at 12:09
  • @0andriy I am using a custom debian based distro for an embedded device. It was built a few years ago (not by me) and it doesn't have `libgpiod` .... @Azeem Thanks again! `export LD_LIBRARY_PATH=/usr/local/lib` did the trick :) – Bastinoboy Apr 13 '20 at 14:02
  • Perhaps time to upgrade... – 0andriy Apr 13 '20 at 16:08

2 Answers2

5

You missed the library for linking with -l flag.

Compile it like this:

gcc -lgpiod test.c

In addition, you might need to configure the runtime paths also for the SO file if it's a custom one. See this thread for more details on runtime shared object locations.

Azeem
  • 11,148
  • 4
  • 27
  • 40
  • This along with another link shared helped me so much with getting rid of a symbol lookup error which I was stuck with – Meenohara Aug 03 '22 at 07:14
3

Please try this command:

Install library first:

sudo apt-get install -y libgpiod-dev

Compile command:

gcc GPIO_LED.c -o led_read_status -lgpiod

Run as root user.

In addition you can use gpio tool for debugging GPIO:

  1. gpiodetect
  2. gpioinfo
  3. gpioset
  4. gpioget