Mike Murphy 1948
Posts: 4
Joined: Tue Dec 05, 2023 9:01 pm

Library list for gpiozeroc++

Tue Dec 05, 2023 9:21 pm

I have the following packages installed:

gpiod/jammy,now 1.6.3-1build1 arm64 [installed]
libgpiod-dev/jammy,now 1.6.3-1build1 arm64 [installed]
libgpiod2/jammy,now 1.6.3-1build1 arm64 [installed,automatic]
liblgpio1/jammy,now arm64 [installed,automatic]
libpigpiod-if-dev/jammy,now 1.78-1 arm64 [installed]
libpigpiod-if1/jammy,now 1.78-1 arm64 [installed,automatic]
libpigpiod-if2-1/jammy,now 1.78-1 arm64 [installed,automatic]
python3-gpiozero/jammy,now 1.6.1-0ubuntu1 all [installed]
python3-lgpio/jammy,now arm64 [installed]
python3-rpi.gpio/jammy,now 0.7.1~a4-1build1 arm64 [installed]
rpi.gpio-common/jammy,now 0.7.1~a4-1build1 arm64 [installed]

I compiled all the sources from the gpiozerocpp repository with the following switches:

g++ %c -fpic -c

And collected them into a shared library, whose not original name is libgpiozero.so.

I wrote a very simple program that creates a button on a gpio pin. When I compile it with:

g++ -f pic test5.cpp -lgpiod -lgpiodcxx -lpigpiod_if2 -lgpiozero

I get a long list of undefined references to functions I have been unable to trace. This, FWIW, is the list:

I wonder whether anyone can tell me which library I have missed, or which additional package I need to install?

Thx in advance, chaps!

Posts: 19722
Joined: Tue Jul 17, 2012 3:02 pm

Re: Library list for gpiozeroc++

Wed Dec 06, 2023 2:48 pm

I'm not sure and it is quite a while since I used C++ or had this kind of problem with it. And of course I have no idea what you have actually done in your code.

But you might want to change your gcc command from this:

Code: Select all

g++ -f pic test5.cpp -lgpiod -lgpiodcxx -lpigpiod_if2 -lgpiozero
to this:

Code: Select all

g++ -f pic test5.cpp -lgpiozero -lgpiod -lgpiodcxx -lpigpiod_if2 
Why? Because gcc will compile and link things in the order they presented.

Lets say some function f() exists in gpiod but it is not used in there, then when linked f() may well not be included. Then when gpiozero is linked and requires f() it is not there.

But when putting gpiozero first the function f() is missing. f() is then found later when linking bipod.

Something like that, if you see what I mean. Point is link order matters here sometimes. So try changing things around.
Slava Ukrayini.

Mike Murphy 1948
Posts: 4
Joined: Tue Dec 05, 2023 9:01 pm

Re: Library list for gpiozeroc++

Wed Dec 06, 2023 6:45 pm

That helps, up to a point ...

I copied the recompiled library, and the libgpiod and libgpiodcxx archives into my source directory (if only to make typing the compile command simpler). I compiled the program (whose simple source is:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include "/usr/include/linux/gpio.h"
#include "[local]/gpiozero.hpp"
#include "[local]Button.hpp"
int main()
Button b(6));
std::cout << "Pressed";
and compile it:
I don't understand the residual undefined reference, because:

So now I wonder what I am overlooking ...

Posts: 1233
Joined: Fri Dec 04, 2015 11:22 am

Re: Library list for gpiozeroc++

Sat Dec 09, 2023 5:37 pm

It would (guessing) appear "Button.hpp" has a declaration for both those methods but no implementation. Is there perhaps a "Button.cpp" which did not get compiled?

Mike Murphy 1948
Posts: 4
Joined: Tue Dec 05, 2023 9:01 pm

Re: Library list for gpiozeroc++

Sat Dec 09, 2023 9:27 pm

Here is Button.hpp

Code: Select all

#ifndef BUTTON_H
#define BUTTON_H
class Button
        int pin = 0;
        Button(int pin);
        bool is_pressed();
        void wait_for_press(double time);
        void wait_for_release(double time);

#endif // BUTTON_H
Here is Button.cpp

Code: Select all

#include "Button.hpp"
#include <pigpio.h>
Button::Button(int pin)
    this->pin = pin;
bool Button::is_pressed()
    if(gpioRead(pin) == 0)
        return true;
        return false;
void Button::wait_for_press(double time)
    while(gpioRead(pin) != 0){ time_sleep(time); }
void Button::wait_for_release(double time)
    while(gpioRead(pin) != 1){ time_sleep(time); }
Button.o exists and is a member of the shared libgpiozero.so library, which I built as follows:

Code: Select all

g++ -shared *.o -lgpiodcxx -lgpiod -lpigpiod_if2 -lpigpio -lstdc++ -lc -o libgpiozero.so
Your assistance will be very valuable! Thx in advance!

Mike Murphy 1948
Posts: 4
Joined: Tue Dec 05, 2023 9:01 pm

References resolved, if not explained

Mon Dec 11, 2023 11:28 am

Copying all the sources into the same directory, and compiling them with this instruction:

Code: Select all

g++ -fpic *.cpp -lgpiodcxx -lgpiod -lpigpiod_if2 -lpigpio -lstdc++ -lc -o test5
has solved the linkage issue

Return to “C/C++”