I'm trying to build python bindings for a library that i wrote, and i'm having some trouble getting cmake to understand that it should use the boost-python library for python 3.
Here is my cmake file:
cmake_minimum_required(VERSION 2.8)
FIND_PACKAGE(Boost COMPONENTS
system
thread
python REQUIRED)
find_package(PythonLibs REQUIRED)
INCLUDE_DIRECTORIES(${PYTHON_LIBRARIES})
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
ADD_LIBRARY(
pschulze SHARED
src/candidate_relation.cpp
src/schulze.cpp
src/calculate.cpp
src/candidate.cpp
src/ranking.cpp
src/userinput.cpp
python.cpp)
TARGET_LINK_LIBRARIES(pschulze ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
ADD_EXECUTABLE(
schulze
src/candidate_relation.cpp
src/schulze.cpp
src/calculate.cpp
src/candidate.cpp
src/ranking.cpp
src/userinput.cpp
src/json-spirit/json_spirit_reader.cpp
src/json-spirit/json_spirit_value.cpp
main.cpp)
TARGET_LINK_LIBRARIES(schulze ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
ADD_DEFINITIONS(-std=gnu++0x -Os)
add_subdirectory (tests)
set(CMAKE_BUILD_TYPE Debug)
And this is the linker error that I get:
Linking CXX executable schulze
CMakeFiles/schulze.dir/src/schulze.cpp.o: In function `arg_to_python':
/usr/include/boost/python/converter/builtin_converters.hpp:122: undefined reference to `PyInt_FromLong'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../lib/libboost_python.so: undefined reference to `PyString_Size'
This might do the trick :
set(Python_ADDITIONAL_VERSIONS 3.2)
find_package(Boost COMPONENTS system thread python-py32 REQUIRED)
Related
I'm developing a cpp project in Clion. For the build I use cmake. Last weekend I upgraded from ubuntu 18.04 to 20.04, from then I cannot build my project and I get the error:
/usr/local/include/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'. It seems that something in the linking isn't right. I reinstalled all packages, tried different versions but nothing. My CMakeLists.txt is:
cmake_minimum_required(VERSION 3.7)
project(pki_cpp)
set(CMAKE_CXX_STANDARD 11)
set(THREADS_PREFER_PTHREAD_FLAG ON)
#find_package(libmongocxx REQUIRED)
find_package(mongocxx REQUIRED)
find_package(bsoncxx REQUIRED)
include_directories(${LIBMONGOCXX_INCLUDE_DIR})
include_directories(${LIBBSONCXX_INCLUDE_DIR})
include_directories(SYSTEM ./lib)
set(BOOST_ROOT /usr/lib/)
find_package(Boost 1.71.0 REQUIRED COMPONENTS system chrono thread filesystem)
include_directories(${Boost_INCLUDE_DIR})
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
# A custom library
add_subdirectory(asnDatatypes)
include_directories({CMAKE_CURRENT_SOURCE_DIR} asnDatatypes)
set_property(TARGET asnLibrary PROPERTY POSITION_INDEPENDENT_CODE ON)
# end custom library
#cryptopp library
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/contrib/cmake")
find_package(CryptoPP REQUIRED)
include_directories(${CRYPTOPP_INCLUDE_DIR})
link_directories(${CryptoPP_INCLUDE_DIR})
# end cryptopp
add_executable(pki_cpp main.cpp rootCA.cpp rootCA.h)
target_include_directories(pki_cpp PRIVATE ${Boost_INCLUDE_DIRS} ${LIBMONGOCXX_INCLUDE_DIRS} )
link_directories(pki_cpp ${Boost_LIBRARY_DIRS})
target_link_libraries(pki_cpp ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_LIBRARIES} Boost::system Threads::Threads boost_system asnLibrary OpenSSL::SSL ${CRYPTOPP_LIBRARIES} ${LIBMONGOCXX_LIBRARIES} mongo::bsoncxx_shared mongo::mongocxx_shared ${CMAKE_THREAD_LIBS_INIT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} Boost::system)
Any help is appreciated!
That's a lot of stuff, all randomly ordered and commented out. The obvious error is a failure to link Boost System
I'd go with
FIND_PACKAGE(Boost 1.65.0
COMPONENTS system program_options REQUIRED)
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${Boost_INLUDE_DIRS})
LINK_LIBRARIES(${Boost_LIBRARIES})
Alternatively you can be specific and link specific libs to specific targets
TARGET_LINK_LIBRARIES(mylib ${Boost_SYSTEM_LIBRARY})
TARGET_LINK_LIBRARIES(myexecutable ${Boost_THREAD_LIBRARY})
I used this link to install OpenCV.
What works:
1.OpenCV works fine with python (running from terminal).
2.I can import opencv libraries in a single C++ program.
What does not work :
When the code is spread across multiple and you need to build it using CMake.
Here's my CmakeLists.txt :
1.cmake_minimum_required(VERSION 3.9)
2.project(Image_processing)
3.set(CMAKE_CXX_STANDARD 14)
4.find_package(OpenCV REQUIRED)
5.include_directories(/home/user/opencv/build)
6.add_executable(main main.cpp)
7.target_link_libraries(project_name ${OpenCV_LIBS})
Errors (can regenerate them by commenting lines 4,5 and 7 in above CMake file):
undefined reference to OpenCV functions.
CMake Error at CMakeLists.txt:7 (target_link_libraries):
Cannot specify link libraries for target "Image_processing" which is not
built by this project.
Correct it with:
cmake_minimum_required(VERSION 3.5)
project(Image_processing)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBS})
In your CMakeLists.txt, the exe-name is not matching with the target-link-name. I modify the line, then it works on my PC.
The CMakeLists.txt of an OpenCV Project:
cmake_minimum_required(VERSION 3.5)
project(Image_processing)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV REQUIRED)
#include_directories(/home/user/opencv/build)
add_executable(Image_processing main.cpp)
target_link_libraries(Image_processing ${OpenCV_LIBS})
Using the following CMake code:
cmake_minimum_required(VERSION 3.6)
project(TrustLineManager)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(BOOST_ROOT D:/Tools/boost_1_62_0/)
set(Boost_INCLUDE_DIRS D:/Tools/boost_1_62_0/boost)
set(Boost_LIBRARY_DIRS D:/Tools/boost_1_62_0/libs)
set(BOOST_INCLUDEDIR C:/MinGW/include)
set(BOOST_LIBRARYDIR C:/MinGW/lib)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost
1.62.0
COMPONENTS system
filesystem
REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(TrustLineManager ${Boost_LIBRARIES})
I get this error:
Error:Unable to find the requested Boost libraries.
Boost version: 1.62.0
Boost include path: D:/Tools/boost_1_62_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.
What should I do to solve it?
EDIT:
I modified my CMake code into:
cmake_minimum_required(VERSION 3.6)
project(TrustLineManager)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
set(BOOST_ROOT "D:/Tools/boost_1_62_0")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(TrustLineManager ${SOURCE_FILES})
target_link_libraries(TrustLineManager Boost::filesystem Boost::thread)
I have now this error:
Error:Target "TrustLineManager" links to target "Boost::filesystem" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing?
Here is a screenshot of Boost directory contents:
Obviously CMake finds Boost as it is able to detect its version (1.62.0).
CMake uses the file FindBoost.cmake to determine what libraries to link against.
This file is updated continuously when new boost versions appear.
There, support for Boost 1.62.0 is available only for CMake >= 3.7.0.
So just update your CMake version (or just the file FindBoost.cmake) and you will be fine.
CLion 1.2, with bundled CMake 3.3.2 and MinGW-w64 4.8.4
I need to get a single DLL in a result of building that no need any other libraries to work. But can't link Boost libraries statically. I bootstrapped and built Boost with corresponding MinGW.
cmake_minimum_required(VERSION 3.3)
project(SampleProject)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(BOOST_ROOT "..\\lib\\boost_1_59_0")
set(Boost_USE_STATIC_LIBS ON)
set(BOOST_COMPONENTS_NEEDED filesystem )
find_package(Boost 1.59.0 REQUIRED COMPONENTS ${BOOST_COMPONENTS_NEEDED})
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
include_directories(${Boost_INCLUDE_DIR})
set(SOURCE_FILES main.cpp)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--kill-at -static-libgcc -static-libstdc++")
add_library(${CMAKE_PROJECT_NAME} SHARED ${SOURCE_FILES})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}\\..\\..\\output")
target_link_libraries(${CMAKE_PROJECT_NAME} ${Boost_LIBRARIES})
Build output:
O:/SampleProject/Cpp/lib/boost_1_59_0/stage/lib/libboost_filesystem-mgw48-mt-d-1_59.a(operations.o): In function error':
O:\SampleProject\Cpp\lib\boost_1_59_0/libs/filesystem/src/operations.cpp:286: undefined reference toboost::system::system_category()'
What else should I do to link with boost?
UPDATE: there is a list of built libraries
libboost_filesystem-mgw48-1_59.a
libboost_filesystem-mgw48-d-1_59.a
libboost_filesystem-mgw48-mt-1_59.a
libboost_filesystem-mgw48-mt-d-1_59.a
libboost_filesystem-mgw48-mt-s-1_59.a
libboost_filesystem-mgw48-mt-sd-1_59.a
libboost_filesystem-mgw48-s-1_59.a
libboost_filesystem-mgw48-sd-1_59.a
libboost_system-mgw48-1_59.a
libboost_system-mgw48-d-1_59.a
libboost_system-mgw48-mt-1_59.a
libboost_system-mgw48-mt-d-1_59.a
libboost_system-mgw48-mt-s-1_59.a
libboost_system-mgw48-mt-sd-1_59.a
libboost_system-mgw48-s-1_59.a
libboost_system-mgw48-sd-1_59.a
This looks like a linker error suggesting that you are not linking to Boost::system
You need to add system to BOOST_COMPONENTS_NEEDED. Change this line and see if it helps
set(BOOST_COMPONENTS_NEEDED system filesystem )
I'm tring to use CMS and cannot link libraries that i compiled on my linux. My CMakeLists.txt:
project(region_algorithm)
SET_TARGET_PROPERTIES(PROPERTIES LINKER_LANGUAGE CXX)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
INCLUDE_DIRECTORIES(
/usr/local/include/activemq-cpp-3.8.3
/usr/local/apr/include/apr-1)
link_libraries(
/usr/local/lib/libactivemq-cpp.a
/usr/local/apr/lib/libapr-1.a)
When i'm building project i got errors like:
regionalgorithmconsumer.cpp:-1: error: undefined reference to
`decaf::util::concurrent::CountDownLatch::CountDownLatch(int)'