How to compile a C program using libgpio(d) for Raspberry Pi?

983 views Asked by At

I've got an RPi 5, and I need to do some GPIO programming. I have read that all (most anyway) of the GPIO libraries used on previous models of RPi, and in earlier versions of the Raspbian OS - are now deprecated. I'm told the GPIO library that has survived 'the great cull' is called libgpio or libgpiod (not sure what the difference is).

I saw a posting on the RPi forum in which a sample program in C was said to have compiled & run successfully. However, I cannot get this to work, so I'm posting here to ask for help.

My C programming skills border on non-existent, but I generally manage to get something working if I'm persistent. Here's what I've tried:

sudo update
sudo apt install gpiod libgpiod-dev libgpiod-doc 

cd blinky_c                      # blinky.c is in blinky_c
gcc -o blinky -libgpiod blinky.c
blinky.c:3:10: fatal error: lgpio.h: No such file or directory
    3 | #include "lgpio.h"
      |          ^~~~~~~~~

I do not find lgpio.h nor libgpio(d).h in either /usr/include/linux or /usr/include.

blinky.c:

#include <stdio.h>
#include <stdlib.h>
#include "lgpio.h"

int h, i;

int main(void) {
    h = lgGpiochipOpen(4);
    lgGpioClaimOutput(h, 0, 17, 0);
    while(1)
    {
        lgGpioWrite(h, 17, 0);
        lguSleep(1);
        lgGpioWrite(h, 17, 1);
        lguSleep(1);
    }
    return EXIT_SUCCESS;
}

How do I get the lgpio.h header file, and where & how should it be installed? Is it actually the header file I need?

3

There are 3 answers

1
KamilCuk On BEST ANSWER

Read the thread there. People mention lg library.

How to compile a C program using libgpio(d) for Raspberry Pi?

If you would have a program using libgpio, you would properly compiole it.

How do I get the lgpio.h header file

Install the library that provides it.

where

Typically, headers from package managers are installed into /usr/include. Manually installed headers are installed at /usr/local/include.

how should it be installed?

Follow the installation instructions at https://github.com/joan2937/lg/tree/master#download--install .

You may also consider, instead of using lg library, rewriting your program to use libgpiod library.

1
Plaisk On

Looking at the source for the library, the only .h file is gpiod.h. This should have been installed to /usr/include/gpiod.h, so you should be able to access it with just

#include <gpiod.h>

When in doubt, run whereis file.h on the terminal and as long as the file is installed properly, you'll find it.

1
Dan On

Using quotes for your include "gpiod.h" will force the compiler to prioritise the search for this file in your local path (where you are compiling your code). Using <gpiod.h> will force the compiler to look in systems files, or as defined in LD_LIBRARY_PATH (see also ldconfig). APT should have done this for you. Try using

#include <gpiod.h>

as Plaisk suggested.