craspberry-pishared-librariesgpiolibgpiod

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


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?


Solution

  • 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.