0

[solved] I am coding in a Raspberry Pi and I am having some trouble combining code that uses OpenCV and pigpio libraries in c++. I have some code that uses OpenCV that is compiled with CMake, and other code that uses pigpio compiled with g++. I am not able to get the OpenCV library work with g++, and i cant figure out how to add the pigpio library to the CMakeLists.

I dont know if it is possible to add the -lpigpio to the CMakeLists

g++ -Wall -pthread -o pigpio pigpio.cpp -lpigpio

I have tried creating a Findpigpio.cmake copying this https://github.com/joan2937/pigpio/blob/master/util/Findpigpio.cmake

and adding it to /usr/share/cmake-3.6/Modules, then creating a CMakeLists.txt with the following:


cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

project(pigpio1_project)

find_package(pigpio REQUIRED)


include_directories(${pigpio_INCLUDE_DIRS})


add_executable(pigpio1 pigpio1.cpp)

target_link_libraries(pigpio1 LINK_PRIVATE ${pigpio_LIBS})

but when doing "cmake .. && make" it shows me the following error:

Scanning dependencies of target pigpio1
[ 50%] Building CXX object CMakeFiles/pigpio1.dir/pigpio1.cpp.o
[100%] Linking CXX executable pigpio1
CMakeFiles/pigpio1.dir/pigpio1.cpp.o: In function `main':
pigpio1.cpp:(.text+0x1c): undefined reference to `gpioInitialise'
pigpio1.cpp:(.text+0x64): undefined reference to `gpioServo'
pigpio1.cpp:(.text+0x70): undefined reference to `gpioServo'
pigpio1.cpp:(.text+0x84): undefined reference to `gpioServo'
pigpio1.cpp:(.text+0x90): undefined reference to `gpioServo'
collect2: error: ld returned 1 exit status
CMakeFiles/pigpio1.dir/build.make:94: recipe for target 'pigpio1' failed
make[2]: *** [pigpio1] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/pigpio1.dir/all' failed
make[1]: *** [CMakeFiles/pigpio1.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2


I could make it work in Cmake (thanks to the comment of tsyvarev) I wasnt using the right name of the pigpio library variable.

  • Welcome to Stack Overflow! Here we expect a question to concentrate on a **single problem**. But your question seems to ask about two **different things**: linking libraries with a command line and with CMake. Please, choose a single problem. Also, make sure that you have searched before asking. E.g. https://stackoverflow.com/questions/43136418/how-to-add-l-ell-compiler-flag-in-cmake, https://stackoverflow.com/questions/8774593/cmake-link-to-external-library, etc. – Tsyvarev Oct 29 '19 at 21:21
  • The `Findpigpio.cmake` script you use sets variables `pigpio_LIBRARY`, `pigpiod_if_LIBRARY` and `pigpiod_if2_LIBRARY`. But you use `pigpio_LIBS` variable which isn't touched by the script at all.. – Tsyvarev Oct 29 '19 at 22:11

1 Answers1

0

First I want to clarify one thing. CMAKE is only a build tool that will use a compiler like g++ or clang to compile your code.

Coming back to your next question. You should be able to use Cmakelists to make a library of pigpio and link it with your main executable code along with opencv library.

Like so:

add_library(PIGPIO SHARED pigpio.cpp)
target_link_libraries(PIGPIO pthread)

And now you have your PIGPIO library that can be used for your actual executable along with your opencv library like this:

add_executable(YOUR_MAIN src/yourmain.cpp)
target_link_libraries(YOUR_MAIN PIGPIO ${OpenCV_LIBS})
Vincent
  • 2,073
  • 1
  • 17
  • 24
pankycodes
  • 61
  • 2
  • 6