Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8013

C/C++ • Using GPIOD correct in this C++ GPIO test programme?

$
0
0
I'm trying to use the GPIOD library[1] and related header file properly on a Raspberry Pi 5 in combination with C++.
The reason that I want to use C++ instead of C, is that I want to use it later on in another environment which requires correct working C++ code.

I have doubts that I'm actually using the GPIOD library in C++ and appreciate your feedback (I'm still learning) on this.

Background:
I found out that I actually have to install the related development library by entering:

Code:

sudo apt install -y libgpiod-dev 
and compile the minimum code below to put a GPIO output pin HIGH with:

Code:

  g++ -Wall -O3 -std=c++17 -o led27on led27on.cpp -lgpiod
What I notice that when "gpiod" in the compile command line is replaced by "gpiodcxx" or "libgpiodcxx" the compilation fails so I (a rookie) guess C++ code also uses gpiod…
So I wonder: Am I actually using the C++ library or the C-version?
After compiling the code works and the following additional provide some information which I do not not fully understand:

Code:

ldd led27on | grep gpiod            libgpiod.so.2 => /lib/aarch64-linux-gnu/libgpiod.so.2 (0x00007fff543b0000)These additional commands provide:    ldd /usr/lib/aarch64-linux-gnu/libgpiod.so            linux-vdso.so.1 (0x00007ffed740c000)            libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x00007ffed71d0000)            /lib/ld-linux-aarch64.so.1 (0x00007ffed73d4000)        ldd /usr/lib/aarch64-linux-gnu/libgpiodcxx.so            linux-vdso.so.1 (0x00007fffbd1d4000)            libgpiod.so.2 => /lib/aarch64-linux-gnu/libgpiod.so.2 (0x00007fffbd120000)            libstdc++.so.6 => /lib/aarch64-linux-gnu/libstdc++.so.6 (0x00007fffbcf00000)            libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x00007fffbcd40000)            /lib/ld-linux-aarch64.so.1 (0x00007fffbd19c000)            libgcc_s.so.1 => /lib/aarch64-linux-gnu/libgcc_s.so.1 (0x00007fffbcd00000)            libm.so.6 => /lib/aarch64-linux-gnu/libm.so.6 (0x00007fffbcc60000)        ldd  /lib/aarch64-linux-gnu/libgpiod.so.2            linux-vdso.so.1 (0x00007fffb2fcc000)            libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x00007fffb2d90000)            /lib/ld-linux-aarch64.so.1 (0x00007fffb2f94000)
Test code:
Due to the fact that the code below includes a .hpp file and the source code is a .cpp file (and not a .h and .c files), I assume I'm using the C++ language.
After compiling and running the code below the connected LED to gpio output pin27 lights on.

Code:

// (Linux raspberrypi 6.6.74+rpt-rpi-2712 #1 SMP PREEMPT Debian 1:6.6.74-1+rpt1 (2025-01-27))    //  sudo apt update && sudo apt install -y libgpiod-dev    // Compile with:    //  g++ -Wall -O3 -std=c++17 -o led27on led27on.cpp -lgpiod    //    // Notice when "gpiod" in the compile command line above is replaced    // by "gpiodcxx" or "libgpiodcxx" the compilation fails so I (a rookie) guess C++ code also uses gpiod    //     // Run by:    //  ./led27on    //        #include <iostream>    #include <gpiod.hpp>  // Please note this header file which must be included    #include <chrono>    #include <thread>        #ifndefCONSUMER    #defineCONSUMER"Consumer"    #endif        int main() {        const char* chipname = "gpiochip0";        const int gpio_write_pin = 27;  // GPIO27 LED            // Open GPIO chip        gpiod_chip* chip = gpiod_chip_open_by_name(chipname);        if (!chip) {            std::cerr << "Error opening chip" << std::endl;            return 1;        }                // Initialize output line        gpiod_line* write_line = gpiod_chip_get_line(chip, gpio_write_pin);        if (!write_line) {            std::cerr << "Error setting output line" << std::endl;            gpiod_chip_close(chip);            return 1;        }                // Request output mode        if (gpiod_line_request_output(write_line, CONSUMER, 0) < 0) {            std::cerr << "Request line as output failed\n" << std::endl;            gpiod_line_release(write_line);            gpiod_chip_close(chip);            return 1;        }            // Set LED on    int ret = gpiod_line_set_value(write_line, 1);    if (ret < 0) {    perror("Set line output failed\n");    }            // Cleanup    gpiod_line_release(write_line);        gpiod_chip_close(chip);        return 0;    }

[1]: https://git.kernel.org/pub/scm/libs/lib ... gpiod.git/.

Statistics: Posted by Rbert — Mon Apr 21, 2025 8:16 pm



Viewing all articles
Browse latest Browse all 8013

Trending Articles