I am doing FRVT 1:1 verification. So I need to use the program provided by FRVT. I have connected to the program I wrote, and completed implementation.
But I want to transplant what I wrote in cython step by step in the NullImp Example provided by FRVT. But I got this result:
nullimplfrvt11.cpp
....
#include <Python.h> //(is ok)
#include "numpy/arrayobject.h" //(error)
....
CMakelists.txt
cmake_minimum_required(VERSION 2.8)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/../../../common/src/include)
# Configure built shared libraries in top-level lib directory
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../lib)
find_package(numpy REQUIRED)
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(${PYTHON_LIBRARIES})
# Build the shared libraries
add_library (frvt_11_null_001 SHARED nullimplfrvt11.cpp)
output:
[root#4d3eca5735a2 11]# bash run_validate_11.sh
Checking installation of required packages [SUCCESS]
Looking for core implementation library in /frvt/11/lib.[SUCCESS] Found core implementation library /frvt/11/lib/libfrvt_11_null_001.so.
Attempting to compile and link /frvt/11/lib/libfrvt_11_null_001.so against test harness.
Scanning dependencies of target validate11
[ 50%] Building CXX object src/testdriver/CMakeFiles/validate11.dir/frvt/common/src/util/util.cpp.o
[100%] Building CXX object src/testdriver/CMakeFiles/validate11.dir/validate11.cpp.o
Linking CXX executable ../../../bin/validate11
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyErr_Format'
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyCObject_AsVoidPtr'
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyExc_RuntimeError'
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyObject_GetAttrString'
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyExc_AttributeError'
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyImport_ImportModule'
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyErr_SetString'
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyCObject_Type'
collect2: error: ld returned 1 exit status
make[2]: *** [../bin/validate11] Error 1
make[1]: *** [src/testdriver/CMakeFiles/validate11.dir/all] Error 2
make: *** [all] Error 2
[ERROR] There were errors during compilation of your library with the validation test harness. Please investigate and re-compile.
There are a few issues with the CMake file. The use of find_package(PythonLibs ...) has been deprecated since CMake 3.12. You should consider using the newer commands, such as find_package(Python2 ...). Also, CMake does not provide a find module specifically for NumPy, you have to specify NumPy as a COMPONENT when you call find_package(Python2 ...). This way, you can use the imported target Python2::NumPy defined by the FindPython module to get the Numpy includes and library.
The call to target_link_libraries() must specify a target to link the libraries to. The only target defined in your CMake file is frvt_11_null_001, so that should be the first argument to target_link_libraries(). You should also prefer to use the target-specific variant of include_directories() so to not pollute the CMake directory scope with include directories.
With these fixes, your CMake can look something like this.
cmake_minimum_required(VERSION 2.8)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Configure built shared libraries in top-level lib directory
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../lib)
find_package (Python2 COMPONENTS Interpreter NumPy)
# Build the shared libraries
add_library (frvt_11_null_001 SHARED nullimplfrvt11.cpp)
target_include_directories (frvt_11_null_001 PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../include
${CMAKE_CURRENT_SOURCE_DIR}/../../../common/src/include
)
target_link_libraries(frvt_11_null_001 PUBLIC Python2::NumPy)
This is how I did it:
find_package(Python3 3.7 COMPONENTS Interpreter NumPy REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(c__14 ${PYTHON_LIBRARIES} Python3::NumPy)
where 3.7 is your version and c__14 is the project name
You can use add_subdirectory() to add numpy library in project.
example add_subdirectory(your/library/path)
Related
Let's consider the following trivial code for connecting C to python as follows
// main.cpp
#include <Python.h>
int main()
{
Py_Initialize();
return 0;
}
The following CMake code works fine for
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.9)
project (myapp)
find_package(Threads)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(Python_ADDITIONAL_VERSIONS 3.5)
find_package(Threads REQUIRED)
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
add_executable(myapp
main.cpp
)
target_link_libraries(myapp rt)
target_link_libraries(myapp ${CMAKE_DL_LIBS})
target_link_libraries(myapp "-L/usr/lib/python3.5/config-3.6m-x86_64-linux-gnu -L/usr/lib -lpython3.5m -Bsymbolic-functions")
#target_link_libraries(myapp ${PYTHON_LIBRARIES})
target_link_libraries(myapp ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(myapp ${PYTHON_LIBRARIES})
The problem is when I move the code to another computer, the linking fails as I have python 3.6 there instead of 3.5. Thus I should replace every 3.5 with 3.6. Is there any portable solution for linking python?
I have tried this instead of the lengthy line but it does not work:
target_link_libraries(myapp ${PYTHON_LIBRARIES})
With the following message:
/usr/local/lib/libpython3.5m.a(posixmodule.o): In function `os_forkpty_impl':
/usr/src/Python-3.5.2/./Modules/posixmodule.c:5972: undefined reference to `forkpty'
/usr/local/lib/libpython3.5m.a(posixmodule.o): In function `os_openpty_impl':
/usr/src/Python-3.5.2/./Modules/posixmodule.c:5878: undefined reference to `openpty'
/usr/local/lib/libpython3.5m.a(dynload_shlib.o): In function `_PyImport_FindSharedFuncptr':
/usr/src/Python-3.5.2/./Python/dynload_shlib.c:82: undefined reference to `dlsym'
/usr/src/Python-3.5.2/./Python/dynload_shlib.c:95: undefined reference to `dlopen'
/usr/src/Python-3.5.2/./Python/dynload_shlib.c:126: undefined reference to `dlsym'
/usr/src/Python-3.5.2/./Python/dynload_shlib.c:95: undefined reference to `dlopen'
/usr/src/Python-3.5.2/./Python/dynload_shlib.c:101: undefined reference to `dlerror'
collect2: error: ld returned 1 exit status
CMakeFiles/myapp.dir/build.make:96: recipe for target 'myapp' failed
make[2]: *** [myapp] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/myapp.dir/all' failed
make[1]: *** [CMakeFiles/myapp.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Distributing standalone application dynamically linked against python
Generally speaking, since the API and ABI between minor version of python change, you would have to distribute one version of your executable for each python version you would like to support.
For example, your different application could be named myapp-cpython-34m-linux_x86_64, myapp-cpython-35m-linux_x86_64, ... or myapp-py34m, ...
The Stable Application Binary Interface
As described in Stable Application Binary Interface document, you could compile your program with Py_LIMITED_API definition (meaning only a subset of the API is used), that would allow you create a binary compatible with any version of python start with python 3.2.
That said, this only work well for cpython binary extension that are imported in the cpython interpreter. Indeed, in that case the python symbols are already available.
In your care, myapp would have to find any of the shared python 3.x library and I believe that is not supported by the operating system library loader.
Possible solutions
Redistribute your own python shared library
Statically link against CPython library
Note that if your project uses a compiler different from the one officially associated with a given version of CPython, you could easily compile CPython itself using https://github.com/python-cmake-buildsystem/python-cmake-buildsystem
Tweak and improvement to your project
Here is an improved version of your project specifying Usage requirements for the target.
cmake_minimum_required(VERSION 3.12)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(myapp)
# dependencies
set(Python_ADDITIONAL_VERSIONS 3.5)
find_package(PythonLibs REQUIRED)
find_package(Threads REQUIRED)
# executable
add_executable(myapp
main.cpp
)
target_compile_definitions(myapp
PRIVATE
Py_LIMITED_API
)
target_include_directories(myapp
PRIVATE
${PYTHON_INCLUDE_DIRS}
)
target_link_libraries(myapp
PRIVATE
${CMAKE_DL_LIBS}
Threads::Threads
${PYTHON_LIBRARIES}
rt
)
I tried to import Boost 1.61.0 (downloaded from SourceForge - Boost 1.61.0 as .7z), but failed.
Console:
"D:\Program Files (x86)\JetBrains\CLion 2016.2\bin\cmake\bin\cmake.exe" --build C:\Users\Marczak\.CLion2016.2\system\cmake\generated\WsServer-e351c9f9\e351c9f9\Debug --target WsServer -- -j 4
[ 50%] Linking CXX executable WsServer.exe
CMakeFiles\WsServer.dir\build.make:96: recipe for target 'WsServer.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/WsServer.dir/all' failed
CMakeFiles\WsServer.dir/objects.a(main.cpp.obj): In function `_static_initialization_and_destruction_0':
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [WsServer.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/WsServer.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/WsServer.dir/rule] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/WsServer.dir/rule' failed
mingw32-make.exe: *** [WsServer] Error 2
Makefile:117: recipe for target 'WsServer' failed
CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(WsServer)
set(BOOST_ROOT "C:/Users/Marczak/boost_1_61_0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(SOURCE_FILES src/main.cpp)
find_package(Boost)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(WsServer ${SOURCE_FILES})
If I do find_package(Boost 1.61.0 COMPONENTS system filesystem REQUIRED) I get:
Error: Unable to find the requested Boost libraries.
Boost version: 1.61.0
Boost include path: C:/Users/Marczak/boost_1_61_0
Could not find the following static Boost libraries:
boost_system boost_filesystem
No Boost libraries were found. You may need to set BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT to the location of Boost.
I tried to set Boost_USE_STATIC_LIBRARIES on, but it failed too. I use CLion 2016.2.
UPDATE: I tried older versions too. Same error. What's inside the .7z:
In other topics I see lib folder. But here I don't see it. What I should put in BOOST_LIBRARYDIR?
UPDATE 2: Installed binary from https://sourceforge.net/projects/boost/files/boost-binaries/1.61.0/ . I noticed there's new folder: lib64-msvc-14.0. It contains many .dll and .lib files, e.g. boost_atomic-vc140-mt-1_61.dll.
Boost.org says:
If you plan to use your tools from the Windows command prompt, you're in the right place. If you plan to build from the Cygwin bash shell, you're actually running on a POSIX platform and should follow the instructions for getting started on Unix variants. Other command shells, such as MinGW's MSYS, are not supported—they may or may not work.
I'll try using Cygwin.
If you're new to C++, I suggest you to download MinGW distribution maintained by Stephan T. Lavavej (Microsoft C++ developer): https://nuwen.net/mingw.html. It, among other tools and libraries, contains pre-built boost binaries. Unpack it and specify the path to it via Settings | Build, Execution, Deployment | Toolchains.
After that you should be able to compile the program with the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(WsServer)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(SOURCE_FILES src/main.cpp)
find_package(Boost REQUIRED COMPONENTS filesystem)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(WsServer ${SOURCE_FILES})
target_link_libraries(WsServer ${Boost_LIBRARIES})
Don't forget to drop CMake cache as find_packages doesn't update successful results due to performance reasons (in CLion it can be done via Cmake toolbar | Cache | red arrows icon).
Some additional remarks:
Boost_USE_STATIC_LIBRARIES is not meant to be set manually, it is set by running find_package(Boost), which uses BOOST_ROOT or BOOST_INCLUDEDIR + BOOST_LIBRARYDIR, you should set those if required. You don't have to do it with the MinGW distro I've linked because it already has boost includes and libraries in accessible locations.
You can check that the paths to libraries are correct by looking at Boost_* variables in CMake cache.
libs directory inside boost sources is unrelated to the problem, it doesn't conitain any binaries
You've downloaded boost binaries built with Visual Studio toolchain, not MinGW, so they are incompatible with your setup. If you don't want to use MinGW package I've linked, you have to either find boost binaries built with correct MinGW version or build it yourself.
OK, I know that opencv linking has been discussed before, but I can't see an error and I don't understand the corrective action. I'm trying to link DBoW2 library, which requires opencv. I'm getting undefined reference errors.
$ mingw32-make
Linking CXX shared library ..\lib\libDBoW2.dll
CMakeFiles\DBoW2.dir/objects.a(FORB.cpp.obj):FORB.cpp:(.text+0x124): undefined reference to `cv::Mat::zeros(int, int, int)'
CMakeFiles\DBoW2.dir/objects.a(FORB.cpp.obj):FORB.cpp:(.text+0x1db): undefined reference to `cv::fastFree(void*)'
CMakeFiles\DBoW2.dir/objects.a(FORB.cpp.obj):FORB.cpp:(.text+0x394): undefined reference to `cv::Mat::deallocate()'
CMakeFiles\DBoW2.dir/objects.a(FORB.cpp.obj):FORB.cpp:(.text+0x46a): undefined reference to `cv::Mat::copyTo(cv::_OutputArray const&) const'
CMakeFiles\DBoW2.dir/objects.a(FORB.cpp.obj):FORB.cpp:(.text+0x503): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
CMakeFiles\DBoW2.dir/objects.a(FORB.cpp.obj):FORB.cpp:(.text+0x93f): undefined reference to `cv::Mat::create(int, int const*, int)'
collect2.exe: error: ld returned 1 exit status
Here's the CMakeLists.txt file
cmake_minimum_required(VERSION 2.8)
project(DBoW2)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -march=native ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -march=native")
set(HDRS_DBOW2
DBoW2/BowVector.h
DBoW2/FORB.h
DBoW2/FClass.h
DBoW2/FeatureVector.h
DBoW2/ScoringObject.h
DBoW2/TemplatedVocabulary.h)
set(SRCS_DBOW2
DBoW2/BowVector.cpp
DBoW2/FORB.cpp
DBoW2/FeatureVector.cpp
DBoW2/ScoringObject.cpp)
set(HDRS_DUTILS
DUtils/Random.h
DUtils/Timestamp.h)
set(SRCS_DUTILS
DUtils/Random.cpp
DUtils/Timestamp.cpp)
find_package(OpenCV REQUIRED)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
include_directories(${OpenCV_INCLUDE_DIRS})
add_library(DBoW2 SHARED ${SRCS_DBOW2} ${SRCS_DUTILS})
target_link_libraries(DBoW2 ${OpenCV_LIBS})
I'm using the gui interface, and the parameter OpenCV_DIR automatically sets to C:/OpenCV/minwg_64. Under that directory there is a directory lib containing library files like "libopencv_videostab300.dll.a"
The CMakeCache.txt file contains the lines
//Dependencies for the target
DBoW2_LIB_DEPENDS:STATIC=general;opencv_videostab;general;opencv_videoio;general;opencv_video;general;opencv_superres;general;opencv_stitching;general;opencv_shape;general;opencv_photo;general;opencv_objdetect;general;opencv_ml;general;opencv_imgproc;general;opencv_imgcodecs;general;opencv_highgui;general;opencv_hal;general;opencv_flann;general;opencv_features2d;general;opencv_core;general;opencv_calib3d;
From what I've read, the following lines should be sufficient, but I'm getting the linker errors.
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(DBoW2 ${OpenCV_LIBS})
Edit 1:
In response to Chris Maes
added the pkg check
pkg_check_modules (OPENCV REQUIRED opencv)
Now the configure causes an error, which is progress, but I'm still at a loss. I must have an error in the opencv installation / build. I do have a second copy on an external drive, that was built recently (and easily) using something like cmake .. . But setting OpenCV_DIR to the external build produces the same error.
Found PkgConfig: C:/msys64/usr/bin/pkg-config.exe (found version "0.28")
checking for module 'opencv'
package 'opencv' not found
Edit 2:
Here are the settings that CMake automatically generates when I start a new cache and press generate twice (first time it only generates the make, sh).
CMAKE_BUILD_TYPE (BLANK)
CMAKE_GNUtoMS (UNCHECKED)
CMAKE_INSTALL_PREFIX C:/Program Files (x86)/DBoW2
CMAKE_MAKE_PROGRAM C:/msys64/mingw64/bin/mingw32-make.exe
CMAKE_SH C:/msys64/usr/bin/sh.exe
OpenCV_DIR C:/OpenCV/minwg_64
The directory C:/OpenCV/minwg_64 contains the opencv library built from mingw gcc 64, and cmake files including OpenCVConfig.cmake and directories bin and lib. I wonder if I need directory staticlib?
For windows environment, I have
OPENCV_DIR = C:\OpenCV\minwg_64 (where built bin and lib are located)
OPENCV_VER = 300
PATH includes C:\OpenCV\minwg_64\bin
On linux I use pkg_check_modules plugin for cmake:
find_package( OpenCV REQUIRED )
find_package( PkgConfig REQUIRED )
pkg_check_modules (OPENCV REQUIRED opencv)
include_directories(/usr/include/opencv2)
target_link_libraries(DBoW2 ${OPENCV_LDFLAGS})
note: I don't remember why I used hardcoded /usr/include/opencv2, but I guess OPENCV_INCLUDE_DIRS wasn't any good when I tried. There might be a more clean way :)
for my project in c++ I wanted to create a cmake file to compile and link everything together.
This is my dir structure so far:
"quadro/minimu9-ahrs" alias home
|-build
|-include
|-src
in my home dir I have this 'CMakeLists.txt' file:
cmake_minimum_required (VERSION 2.6)
project(minimu)
set(HEADER_FILES $("include/*.h")
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
add_executable(minimu ${SOURCES})
add_definitions(-std=c++0x -lwiringPi -lpthread)
#install(TARGETS minimu DESTINATION /usr/lib)
in my 'src' dir I got all the .cpp files, and in 'include' all my headers. When I'm comiling, I go into my build dir, delete everything existing in there (from previous builds) and type
cmake ..
make
my problem in now, that my main.cpp in src uses '#include wiringPi.h', but when I make the project it gives me the following error:
pi#raspberrypi ~/quadro/minimu9-ahrs/build $ make
Scanning dependencies of target minimu
[ 20%] Building CXX object CMakeFiles/minimu.dir/src/L3G.cpp.o
[ 40%] Building CXX object CMakeFiles/minimu.dir/src/LSM303.cpp.o
[ 60%] Building CXX object CMakeFiles/minimu.dir/src/main.cpp.o
[ 80%] Building CXX object CMakeFiles/minimu.dir/src/I2CBus.cpp.o
[100%] Building CXX object CMakeFiles/minimu.dir/src/MinIMU9.cpp.o
Linking CXX executable minimu
CMakeFiles/minimu.dir/src/main.cpp.o: In function `frequency_thread(void*)':
main.cpp:(.text+0x1c): undefined reference to `digitalWrite'
main.cpp:(.text+0x38): undefined reference to `digitalWrite'
CMakeFiles/minimu.dir/src/main.cpp.o: In function `signalHandler(int)':
main.cpp:(.text+0xd4): undefined reference to `digitalWrite'
main.cpp:(.text+0xe8): undefined reference to `digitalWrite'
CMakeFiles/minimu.dir/src/main.cpp.o: In function `main':
main.cpp:(.text+0x14c): undefined reference to `wiringPiSetup'
main.cpp:(.text+0x184): undefined reference to `pinMode'
main.cpp:(.text+0x1e8): undefined reference to `pthread_create'
main.cpp:(.text+0x29c): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
CMakeFiles/minimu.dir/build.make:185: recipe for target 'minimu' failed
make[2]: *** [minimu] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/minimu.dir/all' failed
make[1]: *** [CMakeFiles/minimu.dir/all] Error 2
Makefile:72: recipe for target 'all' failed
make: *** [all] Error 2
so how do I tell the compiler in cmake where to find and how to use the wiringPi lib? And is there an easier way, instead of deleting everything in my build folder before cmaking? Like it compiles all the 'static' files one time, and only adds the changing file (=main.cpp) everytime again.
Further I want to execute my program everywhere like
sudo minimu
instead of going into the 'build' dir and type
sudo ./minimu
Thanks if you can help me guys!
And sorry if my english isn't that good :)
Have a nice day.
I just figured this out today thanks to Sebastian over at github.
Assuming that you have wiringPi installed, the easiest thing to do is to make a FindWiringPi.cmake with the following:
find_library(WIRINGPI_LIBRARIES NAMES wiringPi)
find_path(WIRINGPI_INCLUDE_DIRS NAMES wiringPi.h)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(wiringPi DEFAULT_MSG WIRINGPI_LIBRARIES WIRINGPI_INCLUDE_DIRS)
And then put this in the folder where cmake looks for it's modules. For Ubuntu it's /usr/share/cmake-2.x/Modules. It's basically the same path for pi too.
Then in your CMakeLists.txt file use:
# Locate libraries and headers
find_package(WiringPi REQUIRED)
find_package(Threads REQUIRED)
# Include headers
include_directories(${WIRINGPI_INCLUDE_DIRS})
# Link against libraries
target_link_libraries(<yourProjectName> ${WIRINGPI_LIBRARIES})
target_link_libraries(<yourProjectName> ${CMAKE_THREAD_LIBS_INIT})
The pthread module is a stock package and should be on your system already. So then navigate to your folder and do a cmake ./ and then make. If this doesn't work the first time, then remove your CMakeCache.txt with rm CMakeCache.txt and try again. MAKE SURE THAT YOU REMOVE THE CACHE AND NOT THE LIST!!
Thanks for the QA, I figured out this can be done in a simpler manner (Raspberry Pi 3 Model B+) without altering anything in /usr/share/cmake-x.y/Modules. After your add_executable line, add the following
find_library(WIRINGPI_LIBRARIES NAMES wiringPi)
target_link_libraries(<executable_name> ${WIRINGPI_LIBRARIES})
For example:
cmake_minimum_required(VERSION 3.5)
project(OpenInsulin)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_VERBOSE_MAKEFILE ON)
# Include headers
include_directories(.)
add_executable(OpenInsulin
main.cpp
MCP9600.cpp
MCP9600.h)
# Link against wiringPi
find_library(WIRINGPI_LIBRARIES NAMES wiringPi)
target_link_libraries(OpenInsulin ${WIRINGPI_LIBRARIES})
Provide a flag -L <path> to compiler flags.
add_definitions(-std=c++0x -L/path/to/libwringPi.???.(so|a) -lwiringPi -lpthread)
I do not understand, why you clear the build dir before running cmake (this is not critique. I really do not understand, There might be a reason I dont know) . make checks if the sources are newer than the last compile output and compiles only the files, that are affected. But this works only, if do not delete the build targets.
To install the resulting program uncomment the install directive in you cmake and setup a target dir that's in you PATH. You can also create a dir /home//bin or so. Prepend it to the PATH environment variable and configure the target of you install directive with the new path. Then, in addition to cmake and make you have to perform a make install.
I'll begin stating that I'm almost complete dumb in Cmake matter.
I have the following CMakeLists.txt for a Kdevelop 4.1 project:
project(uart)
find_package(KDE4 REQUIRED)
include (KDE4Defaults)
include_directories( ${KDE4_INCLUDES} ${QT_INCLUDES} src/include src/include/QSerialDevce )
add_subdirectory(doc)
add_subdirectory(src)
add_subdirectory(icons)
link_directories(/usr/lib)
find_library(SERIALDEVICE_LIB qserialdeviced)
add_executable(uart ${uart_SRCS})
target_link_libraries(uart ${SERIALDEVICE_LIB})
When I try to build my project I see:
uart/build> make -j2
-- Found Qt-Version 4.6.3 (using /usr/bin/qmake-qt4)
-- Found X11: /usr/lib64/libX11.so
-- Found KDE 4.5 include dir: /usr/include/kde4
-- Found KDE 4.5 library dir: /usr/lib64/kde4/devel
-- Found the KDE4 kconfig_compiler4 preprocessor: /usr/bin/kconfig_compiler4
-- Found automoc4: /usr/bin/automoc4
CMake Error at CMakeLists.txt:16 (add_executable):
add_executable called with incorrect number of arguments
CMake Error: Attempt to add link library "/usr/lib/libqserialdeviced.so" to target "uart" which is not built by this project.
-- Configuring incomplete, errors occurred!
make: *** [cmake_check_build_system] Error 1
*** Failed ***
Everything I read says that add_executable and target_link_libraries should look like the last two lines of my file:
add_executable(uart ${uart_SRCS})
target_link_libraries(uart ${SERIALDEVICE_LIB})
If I change those two lines of CMakeLists.txt leaving it as:
project(uart)
find_package(KDE4 REQUIRED)
include (KDE4Defaults)
include_directories( ${KDE4_INCLUDES} ${QT_INCLUDES} src/include src/include/QSerialDevce )
add_subdirectory(doc)
add_subdirectory(src)
add_subdirectory(icons)
link_directories(/usr/lib)
find_library(SERIALDEVICE_LIB qserialdeviced)
target_link_libraries(${SERIALDEVICE_LIB})
I see:
uart/build> make -j2
-- Found Qt-Version 4.6.3 (using /usr/bin/qmake-qt4)
-- Found X11: /usr/lib64/libX11.so
-- Found KDE 4.5 include dir: /usr/include/kde4
-- Found KDE 4.5 library dir: /usr/lib64/kde4/devel
-- Found the KDE4 kconfig_compiler4 preprocessor: /usr/bin/kconfig_compiler4
-- Found automoc4: /usr/bin/automoc4
-- Configuring done
-- Generating done
-- Build files have been written to: uart/build
[ 11%] Built target doc-handbook
[ 11%] Built target uart_automoc
Linking CXX executable uart
CMakeFiles/uart.dir/uart.o: In function `uart::setupSerial()':
uart/src/uart.cpp:126: undefined reference to `AbstractSerial::AbstractSerial(QObject*)'
CMakeFiles/uart.dir/uart.o: In function `uart::setupEnumerator()':
uart/src/uart.cpp:108: undefined reference to `SerialDeviceEnumerator::SerialDeviceEnumerator(QObject*)'
CMakeFiles/uart.dir/uart.o: In function `uart::setupSerial()':
uart_/uart/src/uart.cpp:136: undefined reference to `AbstractSerial::enableEmitStatus(bool)'
CMakeFiles/uart.dir/uart.o: In function `uart::setupEnumerator()':
uart_/uart/src/uart.cpp:112: undefined reference to `SerialDeviceEnumerator::setEnabled(bool)'
collect2: ld returned 1 exit status
make[2]: *** [src/uart] Error 1
make[1]: *** [src/CMakeFiles/uart.dir/all] Error 2
make: *** [all] Error 2
*** Failed ***
That clearly shows that target_link_libraries is not linking my qserialdeviced.
qserialdeviced is at /usr/lib/libqserialdeviced.so.1.0.0, correctly simlinked to /usr/lib/libqserialdeviced.so and easily found if I manually add it in the Makefile.
I obviously tried:
target_link_libraries(-lqserialdeviced)
with no change.
I also tried:
if ("${SERIALDEVICE_LIB}" STREQUAL "SERIALDEVICE_LIB-NOTFOUND")
message(FATAL_ERROR "'qserialdeviced' wasn't found!")
else()
message("'qserialdeviced' found: " ${SERIALDEVICE_LIB})
endif ()
But this test succeeds. The library is found:
'qserialdeviced' found: /usr/lib/libqserialdeviced.so
Can anybody please help me to understand what happens here?
I am using Linux Fedora 13, cmake version 2.8.0, gcc (GCC) 4.4.5 20101112 (Red Hat 4.4.5-2) and kdevelop-4.1.0-1.fc13.x86_64.
Thanks i advance.
EDIT:
As suggested by #DatChu, I split my CMakeLists.txt across my subdirectories and everything makes sense to me now.
Thanks everbody!
For the original CMakeLists.txt file, the problem is not with target_link_libraries but with add_executable
add_executable(uart ${uart_SRCS})
where did you set your uart_SRCS variable? Do you have
set(uart_SRCS src/blahblah.cpp src/somethingblahblah.cpp)
I think you might misunderstand what add_subdirectory does. It does not add the source files inside. It tells CMake to descend into that folder and look for another CMakeLists.txt. You typically use it when you have a sub-project inside of your project folder.
If you have many source files which you don't want to manually set, you can also do
file(GLOB uart_SRCS src/*.cpp src/*.c)
The downside is you need to manually re-run CMake in order for it to detect new files. See Jack's comment on why this might not be what you want to use.
Your CMakeLists.txt will most likely be
project(uart)
find_package(Qt4 REQUIRED)
include (${QT_USE_FILE})
find_package(KDE4 REQUIRED)
include (KDE4Defaults)
include_directories( ${KDE4_INCLUDES} ${QT_INCLUDES} src/include src/include/QSerialDevice )
link_directories(/usr/lib)
file(GLOB uart_SRCS src/*.cpp src/*.h)
file(GLOB uart_HDRS include/*.h include/QSerialDevice/*.h)
find_library(SERIALDEVICE_LIB qserialdeviced)
add_executable(uart ${uart_SRCS} ${uart_HDRS})
target_link_libraries(uart ${SERIALDEVICE_LIB} ${QT_LIBRARIES})
This isn't really a direct solution, but I was having such difficulty with "undefined reference" errors (solved previously by linking the appropriate libraries, but not in this case), until I just discovered something - an incompatibility with c vs cpp somehow. The files that defined these reference functions were in .c files (which would default cmake to compile with a C compiler.) and my file referencing these functions is a .cpp file (using g++ compiler or whatever your environment c++ compiler is). Once I changed the .c file to .cpp the "undefined reference" errors disappeared. Above it looks like your uart file is .cpp, but maybe check what the other files are and try this method. It's probably not the appropriate solution or even one at all, but this might get you through the day and moving forward.