How to use CMake RPath for boost.Python - boost

I'm writing a Python extension module in C++ using Boost.Python. However, I
would like to use a newer version of the Boost library than the system
installation offers. This newer version of boost is contained in
BOOST_ROOT=$HOME/opt/boost/1.55.0.
Following this guide on how
to use RPath in CMake I came up with the following CMakeLists.txt.
cmake_minimum_required(VERSION 2.8)
project("test")
set(PROJECT_DESC "Test Boost.Python")
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
add_definitions(-std=c++11 -Wall -Wextra -pedantic)
find_package(PythonInterp REQUIRED)
find_package(PythonLibsNew REQUIRED)
find_package(Boost COMPONENTS python REQUIRED)
message(STATUS "Using Boost installation in:")
message(STATUS " INCLUDE: ${Boost_INCLUDE_DIRS}")
message(STATUS " LIB: ${Boost_LIBRARIES}")
include_directories(
${PROJECT_SOURCE_DIR}
${PYTHON_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
macro(add_python_module _name _srccpp)
PYTHON_ADD_MODULE(${_name} ${_srccpp})
target_link_libraries(${_name} ${Boost_LIBRARIES})
endmacro()
add_python_module(ownership ownership.cpp)
Then I run the following commands to build the module
mkdir build; cd build
cmake -DCMAKE_INSTALL_PATH="$BOOST_ROOT/lib" ..
make
The status message after running cmake points to the right boost
installation. (The CMake boost module picks up the environment variable
$BOOST_ROOT) I.e. the CMake variable Boost_LIBARIES points to
$BOOST_ROOT/lib/libboost_python.so.
But, if I check which libraries would actually be used, the system libraries
are listed:
$ ldd ownership.so
# ...
libboost_python.so.1.53.0 => /usr/lib64/libboost_python.so.1.53.0 (0x00007f09abfc1000)
# ...
This is version 1.53, even though the status message above explicitely pointed
to 1.55.
What am I doing wrong? How can I get ldd to pick the library in
$BOOST_ROOT/lib/libboost_python.so.1.55.0?

First of all as I already mentioned in commens you don't need to use CMake RPATH-manipulations
options. Example: http://pastebin.com/UDyYbQ1d, output: standard and custom
Do you know of a way of convincing CMake otherwise even if LIBRARY_PATH is set
This issue is not related to CMake, it's compiler responsibility. Read this discussion.
Solution
You can clear LIBRARY_PATH if you set BOOST_ROOT variable explicitly. And you can check
environment variable in CMakeLists.txt to avoid this problem in future:
string(COMPARE NOTEQUAL "$ENV{LIBRARY_PATH}" "" library_path_warning)
if(library_path_warning)
message(
WARNING
"LIBRARY_PATH environment variable is not empty ($ENV{LIBRARY_PATH}) "
"This may cause dynamic linking errors!"
)
endif()

Related

Error in include omp.h library with MacOS and CMake [duplicate]

I'm attempting to add OpenMP to a project that is building with CMake. I'm having no problem building it on Linux with the standard CMake/OpenMP addition:
find_package(OpenMP)
if (OPENMP_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}
${OpenMP_EXE_LINKER_FLAGS}")
endif()
Unfortunately this doesn't seem to work on macOS targets. When cmake is called, the following error is given:
-- Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS)
-- Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS)
-- Could NOT find OpenMP (missing: OpenMP_C_FOUND OpenMP_CXX_FOUND)
I'm using macOS High Sierra (10.13.3) along with CMake 3.11. I've installed OpenMP 5.01 via brew, 'brew install libomp'. I've found some previous posts commenting on issues regarding these three but they all seem to deal with a previous way of installing OpenMP on macOS, "brew install clang-omp".
I'm thinking this might have something to do with CMake not support this OpenMP install as I'm able to use OpenMP no problem with standard makefiles. Any information provided would be much appreciated.
I've been able to answer my own question (apologies for not figuring this out beforehand, hopefully this can help others with the same issue).
It seems that a patch has been submitted to CMake to allow it to properly create buildsystems with the new OpenMP install: https://gitlab.kitware.com/cmake/cmake/merge_requests/1812
For others seeing this in the future, update to CMake 3.12 if it has been released at the time of reading.
I had the same problem and it took one complete day to find the solution. I am using Mac-Sierra 10.13.4. I want to use Opencv3 (I think the same problem also appears for opencv2) and openMP. I was actually using Clion as the IDE (CLion uses cmake to configure the project unlike other IDE), so I have to write the CMakeLists.txt file.
There was a conflict of using gcc as the compiler for openCV and openMP. If you use gcc as the compiler then it gives error for opencv as :
imwrite() on OS X error: Undefined symbols
You need to specifically use llvm compiler on OS X to resolve this issue. Following, I am giving the correct code for using OpenCV and OpenMP on Mac-Sierra:
cmake_minimum_required(VERSION 3.10)
project(MyOpenCVTest)
set(CMAKE_CXX_STANDARD 11)
add_executable(MyOpenCVTest main.cpp)
# set("OpenCV_DIR" "/modules/opencv/3.4.1/share/OpenCV/")
set(CMAKE_PREFIX_PATH "/usr/local/Cellar/opencv#3/")
set(OpenCV_INCLUDE_DIRS "/usr/local/Cellar/opencv#3/include/")
set(OpenCV_LIBS "/usr/local/Cellar/opencv#3/lib/")
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if(UNIX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++0x")
endif()
set(CMAKE_C_COMPILER "/usr/local/Cellar/llvm/6.0.0/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/local/Cellar/llvm/6.0.0/bin/clang++")
set(OPENMP_LIBRARIES "/usr/local/Cellar/llvm/6.0.0/lib")
set(OPENMP_INCLUDES "/usr/local/Cellar/llvm/6.0.0/include")
OPTION (USE_OpenMP "Use OpenMP to enamble <omp.h>" ON)
# Find OpenMP
if(APPLE AND USE_OpenMP)
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
set(OpenMP_C "${CMAKE_C_COMPILER}")
set(OpenMP_C_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
set(OpenMP_C_LIB_NAMES "libomp" "libgomp" "libiomp5")
set(OpenMP_libomp_LIBRARY ${OpenMP_C_LIB_NAMES})
set(OpenMP_libgomp_LIBRARY ${OpenMP_C_LIB_NAMES})
set(OpenMP_libiomp5_LIBRARY ${OpenMP_C_LIB_NAMES})
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
set(OpenMP_CXX_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
set(OpenMP_CXX_LIB_NAMES "libomp" "libgomp" "libiomp5")
set(OpenMP_libomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
set(OpenMP_libgomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
set(OpenMP_libiomp5_LIBRARY ${OpenMP_CXX_LIB_NAMES})
endif()
endif()
if(USE_OpenMP)
find_package(OpenMP REQUIRED)
endif(USE_OpenMP)
if (OPENMP_FOUND)
include_directories("${OPENMP_INCLUDES}")
link_directories("${OPENMP_LIBRARIES}")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
# set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif(OPENMP_FOUND)
include_directories( ${OpenCV_INCLUDE_DIRS} )
target_link_libraries( MyOpenCVTest ${OpenCV_LIBS})
TARGET_LINK_LIBRARIES(MyOpenCVTest opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs)
You might want to set e.g. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread") such that the linker automatically detects the appropriate pthread library
Do
brew reinstall llvm
to install llvm compiler.
Please note that, you can't use gcc compiler on Mac-Sierra for your project which needs openMP and also openCV. You need to use llvm compiler.
Verify the correct location of llvm and openCV installation directory.

Custom MPI Path in CMake Project

I'm building a project that uses MPI.
Here's the CMakeLists.txt
if(__OPENNN_MPI__)
find_package(MPI)
if(MPI_FOUND)
message("Using MPI")
set(CMAKE_CXX_COMPILER mpicxx)
set(CMAKE_C_COMPILER mpicc)
set(CMAKE_CXX_COMPILE_FLAGS "${CMAKE_CXX_COMPILE_FLAGS} ${MPI_COMPILE_FLAGS}")
set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} ${MPI_LINK_FLAGS}")
add_definitions(-D__OPENNN_MPI__ )
endif()
endif()
What argument to cmake do I pass to enable this block of code.
My MPI Installation is in a custom path, say /path/to/MPI
Do I simply add the MPI path to my $PATH and run cmake . or is there a flag that I should set, like we would set -DCMAKE_INSTALL_PREFIX:PATH
To tell CMakes FindMPI about a custom installation, it is sufficient to make sure that your custom mpicc and mpicxx is in $PATH. Alternatively, you can supply them as such:
cmake -DMPI_C_COMPILER=/path/to/bin/mpicc -DMPI_CXX_COMPILER=/path/to/bin/mpicxx
CMake will pick up the rest based on the compiler wrappers. You should however not set CMAKE_C_COMPILER / CMAKE_CXX_COMPILER to the MPI compiler wrappers. Instead:
include_directories(SYSTEM ${MPI_INCLUDE_PATH})
And for each C++ target:
target_link_libraries(target ${MPI_CXX_LIBRARIES})
if(MPI_CXX_COMPILE_FLAGS)
set_target_properties(target PROPERTIES COMPILE_FLAGS "${MPI_CXX_COMPILE_FLAGS}")
endif()
if(MPI_CXX_LINK_FLAGS)
set_target_properties(target PROPERTIES LINK_FLAGS "${MPI_CXX_LINK_FLAGS}")
endif()
C targets similarly.

CMAKE: how to include and link alternative libraries?

My project need four libraries including MPI, BOOST, VTK and SQLITE3. Since BOOST and SQLITE3 are too old, I installed new version of the two libraries and the old version are not uninstalled:
BOOST # alternative BOOST
|___include
|___lib
|
SQLITE # alternative SQLITE
|___bin
|___include
|___lib
|___share
|
Mypoject
|___src
| |___CMakeLists
| |___.cpp and .h file
|
|___CMakeLists
And I used FindSQLite3.cmake to find the alternative SQLite3:
# Look for the header file.
FIND_PATH(SQLITE3_INCLUDE_DIR sqlite3.h /export/home/hh/hh/sqlite/include)
# Look for the library.
FIND_LIBRARY(SQLITE3_LIBRARY sqlite3 /export/home/hh/hh/sqlite/lib)
# Handle the QUIETLY and REQUIRED arguments and set SQLITE3_FOUND to TRUE if all listed variables are TRUE.
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SQLITE3 DEFAULT_MSG SQLITE3_LIBRARY SQLITE3_INCLUDE_DIR)
# Copy the results to the output variables.
IF(SQLITE3_FOUND)
SET(SQLITE3_LIBRARIES ${SQLITE3_LIBRARY})
SET(SQLITE3_INCLUDE_DIRS ${SQLITE3_INCLUDE_DIR})
ELSE(SQLITE3_FOUND)
SET(SQLITE3_LIBRARIES)
SET(SQLITE3_INCLUDE_DIRS)
ENDIF(SQLITE3_FOUND)
MARK_AS_ADVANCED(SQLITE3_INCLUDE_DIRS SQLITE3_LIBRARIES)
I am trying to link alternative libraries by following lines:
# CMake version
CMAKE_MINIMUM_REQUIRED (VERSION 2.6)
SET(VERSION 0.1)
SET(SOVERSION 1)
# searching ENV{PATH} to find mpicc and
# store the path of mpicc to MPI_INTEL_C
FIND_PATH(MPI_INTEL_C mpicc $ENV{PATH})
# searching ENV{PATH} to find mpicxx and
# store the path of mpicc to MPI_INTEL_CXX
FIND_PATH(MPI_INTEL_CXX mpicxx $ENV{PATH})
IF(MPI_INTEL_C AND MPI_INTEL_CXX)
MESSAGE(STATUS "Intel MPI compiler is used.")
# set C++ complier as mpicxx, and set C complier as mpicc
SET(CMAKE_CXX_COMPILER mpicxx)
SET(CMAKE_C_COMPILER mpicc)
ENDIF(MPI_INTEL_C AND MPI_INTEL_CXX)
find_package(VTK REQUIRED NO_MODULE)
IF(VTK_FOUND)
MESSAGE(STATUS "VTK IS USED")
include(${VTK_USE_FILE})
ELSE()
MESSAGE("VTK IS NOT FOUND")
ENDIF(VTK_FOUND)
list(APPEND CMAKE_MODULE_PATH "/export/home/hh/hh/iRoot-make/cmake")
FIND_PACKAGE(SQLite3 REQUIRED)
MESSAGE(STATUS "${SQLITE3_INCLUDE_DIRS}")
MESSAGE(STATUS "${SQLITE3_LIBRARY}")
# try to link boost--error
INCLUDE_DIRECTORIES(/export/home/hh/hh/boost/include)
LINK_DIRECTORIES(/export/home/hh/hh/boost/lib)
find_package(MPI REQUIRED)
IF(MPI_FOUND)
MESSAGE(STATUS "MPI IS USED")
INCLUDE_DIRECTORIES(${MPI_INCLUDE_PATH})
ELSE()
MESSAGE("MPI IS NOT FOUND")
ENDIF(MPI_FOUND)
aux_source_directory(. DIR_SRCS)
ADD_EXECUTABLE(test ${DIR_SRCS})
#TARGET_LINK_LIBRARIES(iRoot)
# set location of binary generated by the program,
# here PROJECT_BINARY_DIR = DIR OF build
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
# set location of lib generated by the program
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
install(TARGETS test DESTINATION bin)
CMake could not find the alternative SQLite3, and could find the old version:
-- /usr/include
-- /usr/lib64/libsqlite3.so
but the new version is located at:
/export/home/hh/hh/sqlite/include
/export/home/hh/hh/sqlite/lib/libsqlite3.so
I am very new to CMAKE, please help me, thank you in advance!
I will explain this using boost as an example. This is how I have it done in my setup.
Let's assume the newer boost version is installed in:
/export/home/hh/hh/Boost/boost-1_60/boost/include/
/export/home/hh/hh/Boost/lib
Now in CMakeLists.txt add these paths to CMAKE_INCLUDE_PATH and CMAKE_LIBRARY_PATH:
LIST (APPEND CMAKE_INCLUDE_PATH "/export/home/hh/hh/Boost/boost-1_60")
LIST (APPEND CMAKE_LIBRARY_PATH "/export/home/hh/hh/Boost/lib)
Then call FindPackage mentioning the minimum required version and optionally the required library components which are built into .so:
FIND_PACKAGE (Boost 1.60 COMPONENTS "program_options" "log" "system" "filesystem" "thread" "python" REQUIRED)
This should work similarly for other libraries too.
Update:
Add to CMakeLists.txt paths to your custom cmake modules, for example the path where FindSQLite3.cmake is located:
LIST (APPEND CMAKE_MODULE_PATH "${MAINFOLDER}/tools/share/cmake")
Then call: FIND_PACKAGE(SQLite3 REQUIRED)
Also remove: /export/home/hh/hh/sqlite/include and /export/home/hh/hh/sqlite/lib from FindSQLite3.cmake but have them added in a similar to boost way.
Instead of hardcoding these settings in the CMakeLists.txt you should change appropriate value in the cache (builddir/CMakeCache.txt). You can do this
manually
with cmake -D VARNAME=value .
with cmake GUI app

CMake and Boost

I've searched and found out that a lot of people have the same problem, but no solution exists.
I'm using CMake to generate Makefiles for MinGW and when compiling I'm getting an error:
CMakeFiles\boosttest.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x5e): undefined reference to `_imp___ZN5boost6thread4joinEv'
CMakeFiles\boosttest.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x71): undefined reference to `_imp___ZN5boost6threadD1Ev'
CMakeFiles\boosttest.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x88): undefined reference to `_imp___ZN5boost6threadD1Ev'
This seems to be a linking problem, I get it. My CMake configuration is:
project(boosttest)
cmake_minimum_required(VERSION 2.6)
include_directories(${boosttest_SOURCE_DIR}/include c:/boost_1_48_0/)
link_directories(c:/boost_1_48_0/lib)
file(GLOB_RECURSE cppFiles src/*.cpp)
add_executable(boosttest ${cppFiles})
target_link_libraries(boosttest libboost_thread-mgw46-mt-1_48.a)
First I tried using find_package(Boost COMPONENTS thread) and it was working the same way, so I thought to try to do this manually and I still get the same error.
Any insights on this?
I've compiled it for mingw using bjam and as a static link. Also tried doing:
add_library(imp_libboost_thread STATIC IMPORTED)
set_property(TARGET imp_libboost_thread PROPERTY IMPORTED_LOCATION c:/boost_1_48_0/lib/libboost_thread-mgw46-mt-1_48.a)
target_link_libraries(boosttest imp_libboost_thread)
And I still get the same error messages.
For mingw32 you may add definition BOOST_THREAD_USE_LIB. And linking with boost::thread will work. Also you may need Threads package (but i'm not sure, may be it needs only for *nix platforms).
Here is part of my CMakeLists. I copied it from project, which uses boost::thread, and compiles under mingw-gcc (and other compilers):
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_ADDITIONAL_VERSIONS "1.44" "1.44.0")
find_package(Boost COMPONENTS thread date_time program_options filesystem system REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
find_package(Threads REQUIRED)
#...
if (WIN32 AND __COMPILER_GNU)
# mingw-gcc fails to link boost::thread
add_definitions(-DBOOST_THREAD_USE_LIB)
endif (WIN32 AND __COMPILER_GNU)
#...
target_link_libraries(my_exe
${CMAKE_THREAD_LIBS_INIT}
#...
${Boost_LIBRARIES}
)
In my opinion, this question is similar to this question and this one. My best guess would be that you need the same resolution as in my answer to the first question.
I would strongly recommend the use of find_package (Boost ) and take care with the auto-linking:
project(boosttest)
cmake_minimum_required(VERSION 2.6)
# Play with the following defines
# Disable auto-linking.
add_definition( -DBOOST_ALL_NO_LIB )
# In case of a Shared Boost install (dlls), you should then enable this
# add_definitions( -DBOOST_ALL_DYN_LINK )
# Explicitly tell find-package to search for Static Boost libs (if needed)
set( Boost_USE_STATIC_LIBS ON )
find_package( Boost REQUIRED COMPONENTS thread )
include_directories( ${Boost_INCLUDE_DIRS} )
file(GLOB_RECURSE cppFiles src/*.cpp)
add_executable(boosttest ${cppFiles})
target_link_libraries(boosttest ${Boost_LIBRARIES} )

How do I force cmake to include "-pthread" option during compilation?

I know there is something like find_package(Threads) but it doesn't seem to make a difference (at least by itself). For now I'm using SET(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-pthread"), but it doesn't look like a correct solution to me.
The Threads module in the latest versions (>= 3.1) of CMake generates the Threads::Threads imported target. Linking your target against Threads::Threads adds all the necessary compilation and linking flags. It can be done like this:
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
add_executable(test test.cpp)
target_link_libraries(test Threads::Threads)
Use of the imported target is highly recommended for new code, according to the CMake docs
find_package( Threads ) calls a CMake module that first, searches the file system for the appropriate threads package for this platform, and then sets the CMAKE_THREAD_LIBS_INIT variable (and some other variables as well). It does not tell CMake to link any executables against whatever threads library it finds. You tell CMake to link you executable against the "Threads" library with the target_link_libraries() command. So, for example lets say your program is called test. To link it against threads you need to:
find_package( Threads )
add_executable( test test.cpp )
target_link_libraries( test ${CMAKE_THREAD_LIBS_INIT} )
How about the following:
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)
if(CMAKE_USE_PTHREADS_INIT)
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-pthread")
elseif(...)
...
endif()
add_executable( test test.cpp )
target_link_libraries( test ${CMAKE_THREAD_LIBS_INIT} )
If I explicitly specify the default entry point and the library to use, it compiles without problems. The default entry point here is to specify the version in cmake.
cmake_minimum_required(...), target_link_libraries(...)
Below is an example.
# important
cmake_minimum_required(VERSION 2.8)
project(main)
# set c++ version & etc...
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# important
find_package( Threads )
add_executable(main main.cpp)
# important
target_link_libraries(main ${CMAKE_THREAD_LIBS_INIT})

Resources