0

I am trying to compile my C++ project using CMake. My Raspberry Pi OS does include /usr/include/wiringPi.h and all the required .so files in /usr/lib/. Here is the output of gpio -v command:

gpio version: 2.52
Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Pi 4B, Revision: 02, Memory: 4096MB, Maker: Sony 
  * Device tree is enabled.
  *--> Raspberry Pi 4 Model B Rev 1.2
  * This Raspberry Pi supports user-level GPIO access.

I did #include <wiringPi.h> in my project. However, I get errors when I try to build the project:

/usr/bin/ld: /home/.../**.so: undefined reference to `pinMode'
/usr/bin/ld: /home/.../**.so: undefined reference to `digitalWrite'
/usr/bin/ld: /home/.../**.so: undefined reference to `delay'
/usr/bin/ld: /home/.../**.so: undefined reference to `wiringPiSetup'
collect2: error: ld returned 1 exit status

I added add_definitions(-std=c++0x -L/usr/lib/libwiringPi.so -lwiringPi) to my CMakeList.txt, no success. Then, I tried compiling a simple .c code to see if this is a linking issue where again I get errors:

$ gcc -o test my_program.c -lwiringPi

/usr/bin/ld: skipping incompatible /usr/lib/gcc/aarch64-linux-gnu/8/../../../../lib/libwiringPi.so when searching for -lwiringPi
/usr/bin/ld: skipping incompatible /lib/../lib/libwiringPi.so when searching for -lwiringPi
/usr/bin/ld: skipping incompatible /usr/lib/../lib/libwiringPi.so when searching for -lwiringPi
/usr/bin/ld: skipping incompatible /usr/lib/gcc/aarch64-linux-gnu/8/../../../libwiringPi.so when searching for -lwiringPi
/usr/bin/ld: skipping incompatible //lib/libwiringPi.so when searching for -lwiringPi
/usr/bin/ld: skipping incompatible //usr/lib/libwiringPi.so when searching for -lwiringPi
/usr/bin/ld: cannot find -lwiringPi
collect2: error: ld returned 1 exit status

Is there any issue with the 64bit CPU or what else can I do? My application requires turning On/Off relay switch on certain GPIOs. I don't think I2C is a workaround here. Also I would prefer not to download/install libraries written by random developers. My very last option would be invoking python functions through RPi.GPIO library (all just works fine in Python), though c++ workaround is what I am looking for.

7
  • wiringPi is a library written by a random developer. You will need to find a copy of the source and build the library if you want to use it. There are other C libraries written by random developers: pigpio and bcm2835 are the obvious ones.
    – joan
    Jul 20 at 17:57
  • I should also mention lgpio and the /dev/gpiochip library.
    – joan
    Jul 20 at 17:58
  • @joan thanks, you are right. wiringPi however, is OK as it comes with Raspbian OS, at least, I didn't need to install it. I will take a look on those two.
    – zlg
    Jul 20 at 18:00
  • pigpio also comes with Raspbian/Raspberry Pi OS.
    – joan
    Jul 20 at 18:03
  • 1
    wiringpi.com/wiringpi-deprecated it's completely unsupported on 64-bit systems.
    – Dougie
    Jul 20 at 18:23
0

Tried BCM2835 out and it does the job, below is quick-start for those who need it:

BCM2835 is the MCU chip of the Raspberry Pi, whose library can often be used to access the GPIO on BCM 2835 chip. You can use the librarty to control the rigistors of a BCM2835 chip directly just like using STM32 library to control an STM32 chip. in contrast, the libraries of WiringPi or Python control GPIO by reading and writing device files on the Linux system.

To install BCM2835 and get to know PIN numbers, follow this simple guide or try this out:

mkdir ~/BCM2835 && cd ~/BCM2835                  
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.58.tar.gz                       
tar xvfz bcm2835-1.58.tar.gz;                      
cd bcm2835-1.58;                       
./configure;                      
make;        
sudo make install

To make sure you are all set, you can build a sample program test.cpp and include bcm2835.h:

#include <bcm2835.h>
#include <iostream>
int main(int argc, char **argv)
{
    if (!bcm2835_init()) return 1;
    std::cout << "BCM2835 is all set!" << std::endl
    return 0;
}

and build it using g++:

g++ test.cpp -o test -lbcm2835
./test

NOTE: There might be other options as well other than BCM2835, like those were suggested in the comments. Thanks to those who contributed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.