0

So, I have a C++ program that I've compiled with CMake and uses the OpenCV library which I'd like to integrate with wiringPi so I can use functions like digitalWrite(), digitalRead() etc...

Before I tried integrating wiringPi, my CMakeLists.txt looked like:

cmake_minimum_required(VERSION 2.8)
project( Object )
find_package( OpenCV REQUIRED )
add_executable( Object objectTrackingTutorial.cpp )
target_link_libraries( Object ${OpenCV_LIBS})

I was then able to run

make
./Object

and my program would work 100%!

The wiringPi folder I got from running

git clone git://git.drogon.net/wiringPi

is in /home/pi

To integrate wiringPi with my C++ program, I've done the following:

  1. Modified CMakeLists.txt to look like:

    cmake_minimum_required(VERSION 2.8)
    project( Object )
    find_package( OpenCV REQUIRED )
    add_executable( Object objectTrackingTutorial.cpp )
    target_link_libraries( Object ${OpenCV_LIBS} ${wiringPi})
    add_definitions(-std=c++0x -L/home/pi/wiringPi/wiringPi -lwiringPi )
    
  2. My C++ program looks like:

    #include <sstream>
    #include <string>
    #include <iostream>
    #include <opencv2/highgui.hpp>
    #include <opencv2/opencv.hpp>
    #include <opencv2/imgproc.hpp>
    #include <wiringPi.h> 
    
    /* Functions and variable declarations for image processing */
    
    int main(int argc, char* argv[])
    {
        /*Testing to see if wiringPi works. Will verify by probing with multimeter.*/
        wiringPiSetupGpio () ;
        pinMode (17, OUTPUT) ;
        digitalWrite(17, LOW);
    
        while(1){
            digitalWrite(17, HIGH);
            delay(500);
            digitalWrite(17, LOW);
            delay(500);
        }
        /*OpenCV nonsense.*/
        return 0;
    }
    

When trying to compile with make, I get lots of errors like:

 objectTrackingTutorial.cpp:(.text+0x16bc): undefined reference to `wiringPiSetupGpio'

I suspect that I'm linking wiringPi incorrectly. I'm new to linking multiple libraries together, so if someone could help me out that would be greatly appreciated.

Note that even though wiringPi is a C library, I have code that looks like this in /home/pi/wiringPi/wiringPi/wiringPi.h:

#ifdef __cplusplus
extern "C" {
#endif

...

#ifdef __cplusplus
}
#endif

which from what I understand wraps the C library and allows for it to be used in C++.

  • Did you install WiningPi? – Milliways Jun 12 '17 at 0:52
0

It's easiest to use the find_library function.

add_executable(gpioboth gpioboth.c gpiocommon.c)
find_library(wiringPi_LIB wiringPi)
target_link_libraries(gpioboth ${wiringPi_LIB})
0

You only need to change:

target_link_libraries( Object ${OpenCV_LIBS} ${wiringPi})

to:

target_link_libraries( Object ${OpenCV_LIBS} wiringPi)

The last line:

add_definitions(-std=c++0x -L/home/pi/wiringPi/wiringPi -lwiringPi )

is not necessary.

-2

The only thing I see is in your cpp code when you call the wiringPi functions:

wiringPiSetupGpio () ; // space between function name and parentheses
pinMode (17, OUTPUT) ; // space between function name and parentheses

If this is exactly how it is in your code the problem can be fixed by removing the space.

To me the error message you are getting then makes sense as it does not include the parentheses in the single quotes.

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.