Lutex
Lutex

Reputation: 25

GPIOD on Raspberry Pi 3b+ not found

I installed the GPIOD library for c on my Raspberry Pi 3b+ using sudo apt-get install gpiod which finished successfully. I can find the respective files in /usr/lib etc. Also I can execute gpioinfo.

In my program I #inlcude <gpiod.h> but when compiling with GCC using gcc -lgpiod -o btn button.c, I get an error stating gpiod.h was not found after which the comilation is stopped.

Upvotes: 0

Views: 715

Answers (1)

larsks
larsks

Reputation: 312390

You need to install the libgpiod-dev package, which provides header files and libraries for linking.

It is very common to split packages between "things that provide user facing commands" and "things that are required for development". The latter generally have a -dev suffix.


root@rpi:~# apt install libgpiod-dev
.
.
.
The following NEW packages will be installed:
  libgpiod-dev
.
.
.
root@rpi:~# dpkg -L libgpiod-dev
/.
/usr
/usr/include
/usr/include/gpiod.h
/usr/include/gpiod.hpp
/usr/lib
/usr/lib/arm-linux-gnueabihf
/usr/lib/arm-linux-gnueabihf/libgpiod.a
/usr/lib/arm-linux-gnueabihf/libgpiodcxx.a
/usr/lib/arm-linux-gnueabihf/pkgconfig
/usr/lib/arm-linux-gnueabihf/pkgconfig/libgpiod.pc
/usr/lib/arm-linux-gnueabihf/pkgconfig/libgpiodcxx.pc
/usr/share
/usr/share/doc
/usr/share/doc/libgpiod-dev
/usr/share/doc/libgpiod-dev/README.gz
/usr/share/doc/libgpiod-dev/changelog.Debian.armhf.gz
/usr/share/doc/libgpiod-dev/changelog.Debian.gz
/usr/share/doc/libgpiod-dev/copyright
/usr/lib/arm-linux-gnueabihf/libgpiod.so
/usr/lib/arm-linux-gnueabihf/libgpiodcxx.so

To link against libgpiod (or any other library), you need to ensure that any library references come after your source files (symbols are resolved from left to right):

gcc -o btn button.c -lgpiod

For example, if I have in example.c:

#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
  const char *chipname = "gpiochip0";
  struct gpiod_chip *chip;

  // Open GPIO chip
  chip = gpiod_chip_open_by_name(chipname);

  gpiod_chip_close(chip);
  return 0;
}

This fails:

root@rpi:~# gcc -lgpiod  -o example example.c
/usr/bin/ld: /tmp/cc30WQLG.o: in function `main':
example.c:(.text+0x20): undefined reference to `gpiod_chip_open_by_name'
/usr/bin/ld: example.c:(.text+0x2c): undefined reference to `gpiod_chip_close'
collect2: error: ld returned 1 exit status

But this works:

root@rpi:~# gcc -o example example.c -lgpiod

Upvotes: 2

Related Questions