link path confusion after target_link_libraries call - c++

I have a cmake project where I want to add a class containing the matlab engine. For compiling it I need to include two libraries eng and mx, which I do by adding
target_link_libraries( ${TARGET} /usr/local/MATLAB/R2013b/bin/glnxa64/libeng.so)
target_link_libraries( ${TARGET} /usr/local/MATLAB/R2013b/bin/glnxa64/libmx.so)
to my CMakeLists.txt file.
However there are also lots of other old versions of libraries in /usr/local/MATLAB/R2013b/bin/glnxa64/, which seem
to be automatically added to the path as well when calling the above command. I think this causes the compiler to not find my
normal libraries anymore and produces an error.
How can I include only the two above libraries, and not all the others in the glnxa64 folder?
The warning shown after running cmake . :
CMake Warning at CMakeLists.txt:23 (add_executable):
Cannot generate a safe runtime search path for target CCDWidget because
files in some directories may conflict with libraries in implicit
directories:
runtime library [libboost_program_options.so.1.49.0] in /usr/lib may be hidden by files in:
/usr/local/MATLAB/R2013b/bin/glnxa64
runtime library [libboost_system.so.1.49.0] in /usr/lib may be hidden by files in:
/usr/local/MATLAB/R2013b/bin/glnxa64
runtime library [libboost_filesystem.so.1.49.0] in /usr/lib may be hidden by files in:
/usr/local/MATLAB/R2013b/bin/glnxa64
runtime library [libboost_regex.so.1.49.0] in /usr/lib may be hidden by files in:
/usr/local/MATLAB/R2013b/bin/glnxa64
Some of these libraries may not be found correctly.
And the error message during linking:
Linking CXX executable CCDWidget
/usr/lib/x86_64-linux-gnu/libharfbuzz.so.0: undefined reference to `FT_Face_GetCharVariantIndex'
/usr/lib/x86_64-linux-gnu/libharfbuzz.so.0: undefined reference to `FT_Get_Advance'
collect2: error: ld returned 1 exit status
make[2]: *** [CCDWidget] Error 1
make[1]: *** [CMakeFiles/CCDWidget.dir/all] Error 2
make: *** [all] Error 2
Below is my full CMakeLists.txt file. Lines commented out with two ## are alternatives that I tried before and didn't solve my problem.
I also added LINK_PRIVATE to the target_link_libraries command as shown in the code below, which didn't make a difference.
The option PRIVATE alone seems to be not accepted by my cmake version, as it changed the error meassage to
/usr/bin/ld: cannot find -lPRIVATE
collect2: error: ld returned 1 exit status
When the #eng line is commented out, the compilation and linking works without errors
(when calling the matlab engine is also commented out in Readout.cpp), so the error must be produced by that line.
#Specify the version being used as well as the language
cmake_minimum_required(VERSION 2.6)
##cmake_policy(SET CMP0003 NEW)
#Name your project here
project(CCDWidget)
set(TARGET CCDWidget)
set(MAIN_SOURCES CCDWidget.cpp main.cc CCDControl.cpp VideoWindow.cpp ImageWindow.cpp ThisMeasurement.cpp KineticSeries.cpp FastKinetics.cpp Readout.cpp)
##SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
#set_source_files_properties(Readout.cpp PROPERTIES COMPILE_FLAGS "-I/usr/local/MATLAB/R2013b/extern/include -I/usr/local/MATLAB/R2013b/simulink/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -I/usr/local/MATLAB/R2013b/extern/include/cpp -I/usr/local/MATLAB/R2013b/extern/include -DGLNXA64 -DGCC -DMX_COMPAT_32 -DNDEBUG -Wno-effc++")
find_package(Boost COMPONENTS program_options system filesystem regex REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTKMM gtkmm-3.0)
include_directories( ${GTKMM_INCLUDE_DIRS} )
include_directories( ${Boost_INCLUDE_DIR} )
include_directories( ${PROJECT_SOURCE_DIR} )
##link_directories(/usr/local/MATLAB/R2013b/bin/glnxa64)
##target_link_libraries( ${TARGET} eng)
##target_link_libraries( ${TARGET} mx)
set(CMAKE_CXX_FLAGS "--std=c++11")
add_executable( ${TARGET} ${MAIN_SOURCES} )
target_link_libraries( ${TARGET} ${GTKMM_LIBRARIES} )
target_link_libraries( ${TARGET} ${Boost_LIBRARIES} )
target_link_libraries( ${TARGET} LINK_PRIVATE /usr/local/MATLAB/R2013b/bin/glnxa64/libeng.so) # eng
#target_link_libraries( ${TARGET} LINK_PRIVATE /usr/local/MATLAB/R2013b/bin/glnxa64/libmx.so ) # mx
target_link_libraries( ${TARGET} andor )

You could try using an imported target:
add_library(eng SHARED IMPORTED)
set_property(TARGET eng PROPERTY IMPORTED_LOCATION /usr/local/MATLAB/R2013b/bin/glnxa64/libeng.so)
...
add_executable( ${TARGET} ${MAIN_SOURCES} )
...
target_link_libraries(${TARGET} eng)
For debugging you could try to build with "make VERBOSE=1".
This will show you the used gcc command line.
CMake probably transforms your target_link_libraries command to something like:
g++ ... -L/usr/local/MATLAB/R2013b/bin/glnxa64 -leng ...
gcc then finds some boost libraries in this folder.

Related

AirSim/Unity build on macOS ('boost/filesystem.hpp' file not found)

I am trying to install AirSim/Unity on MacOS Catalina. When I run Unity/build.sh I get a fatal error:
In file included from /Users/nfirbas/Documents/AirSim/Unity/AirLibWrapper/AirsimWrapper/Source/Logger.cpp:3:
/Users/nfirbas/Documents/AirSim/Unity/AirLibWrapper/AirsimWrapper/Source/Logger.h:6:11: fatal error: 'boost/filesystem.hpp' file not found
#include <boost/filesystem.hpp>
^~~~~~~~~~~~~~~~~~~~~~
1 warning and 1 error generated.
make[2]: *** [CMakeFiles/AirsimWrapper.dir/Source/Logger.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
I have installed boost with brew. It's my first time working with brew so I assume that I need to edit my CMakeList.txt. I tried changing it myself but I didn't get it working.
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.5.0)
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(MACOSX TRUE)
endif()
find_path(AIRSIM_ROOT NAMES AirSim.sln PATHS ".." "../.." "../../.." "../../../.." "../../../../.." "../../../../../..")
message(AirSim Root directory: ${AIRSIM_ROOT})
LIST(APPEND CMAKE_MODULE_PATH "${AIRSIM_ROOT}/cmake/cmake-modules")
LIST(APPEND CMAKE_MODULE_PATH "${RPC_SOURCE_DIR}/cmake")
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
INCLUDE("${AIRSIM_ROOT}/cmake/cmake-modules/CommonSetup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/rpc-setup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/mav-setup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/airlib-setup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/airsimwrapper-setup.cmake")
IncludeEigen()
project(AirsimWrapper VERSION 0)
# RPC includes & source files
BuildRpc()
# MavLink source files
BuildMavLink()
#AirLib source files
BuildAirlib()
#AirsimWrapper source files
BuildAirsimWrapper()
###################### Link source files to library ######################################
if (${APPLE})
add_library(
${PROJECT_NAME} MODULE
${RPC_LIBRARY_SOURCE_FILES}
${MAVLINK_LIBRARY_SOURCE_FILES}
${AIRLIB_LIBRARY_SOURCE_FILES}
${AIRSIMWRAPPER_LIBRARY_SOURCE_FILES}
)
set_target_properties(${PROJECT_NAME} PROPERTIES BUNDLE TRUE)
else ()
add_library(
${PROJECT_NAME} SHARED
${RPC_LIBRARY_SOURCE_FILES}
${MAVLINK_LIBRARY_SOURCE_FILES}
${AIRLIB_LIBRARY_SOURCE_FILES}
${AIRSIMWRAPPER_LIBRARY_SOURCE_FILES}
)
endif ()
target_link_libraries(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT} -lstdc++ -lpthread -lboost_filesystem)
##################### Build Options #############################3
# Rpc
RpcCheckMSVN()
RpcCmakePkg()
RpcMSVNConfig()
You have told your library target to link against boost_filesystem, but you did not convey in your CMake where to find the Boost header files.
The idiomatic way to find Boost using CMake is to use the configuration files that now ship with Boost (e.g. BoostConfig.cmake), as of Boost version 1.70 and greater. You can make use of these by calling find_package(Boost ...), then linking with the imported target Boost::filesystem:
# Tell CMake to locate Boost on your machine, specifically
# looking for the filesystem library.
find_package(Boost REQUIRED COMPONENTS filesystem)
...
# Link the Boost::filesystem target, which includes the Boost headers.
target_link_libraries(${PROJECT_NAME} PUBLIC
${CMAKE_THREAD_LIBS_INIT}
-lstdc++
-lpthread
Boost::filesystem
)
This will pull in the Boost headers as well, so you don't need an explicit call to target_include_directories() to specify where the Boost headers are.
Note: To ensure the headers are installed on your system, you may need to install boost-devel, in addition to your boost installation.

Undefined symbols when building libuv

I need to use libuv with my library. Since I cannot link it two static libraries I decided to include the source for libuv along with my code. I have a .cmake file that downloads libuv, checks out the right tag and adds the source files to a variable:
include(DownloadProject.cmake)
download_project(PROJ libuv
GIT_REPOSITORY https://github.com/libuv/libuv.git
GIT_TAG v1.10.0
${UPDATE_DISCONNECTED_IF_AVAILABLE}
)
exec_program(COMMAND "./autogen.sh" WORKING_DIRECTORY ${libuv_SOURCE_DIR})
exec_program(COMMAND "./configure" WORKING_DIRECTORY ${libuv_SOURCE_DIR})
include_directories(${LIBUVDIR}/include ${LIBUVDIR}/src)
set(LIBUV_SOURCES
${LIBUVDIR}/include/uv.h
${LIBUVDIR}/include/tree.h
${LIBUVDIR}/include/uv-errno.h
${LIBUVDIR}/include/uv-threadpool.h
${LIBUVDIR}/include/uv-version.h
${LIBUVDIR}/src/fs-poll.c
${LIBUVDIR}/src/heap-inl.h
${LIBUVDIR}/src/inet.c
${LIBUVDIR}/src/queue.h
${LIBUVDIR}/src/threadpool.c
${LIBUVDIR}/src/uv-common.c
${LIBUVDIR}/src/uv-common.h
${LIBUVDIR}/src/version.c
)
etc.
I then add add the list of libuv source files to the list of source files for my project and link the library with its dependencies:
include(libuv.cmake)
# Build library
set(SOURCE_FILES
<my sources>
${LIBUV_SOURCES})
add_library(databaseclient STATIC ${SOURCE_FILES})
set(CMAKE_THREAD_PREFER_PTHREAD 1)
set(THREADS_PREFER_PTHREAD_FLAG 1)
include(FindThreads)
target_link_libraries(databaseclient PUBLIC Threads::Threads)
But when I run make I get the following error:
Undefined symbols for architecture x86_64:
"_pthread_barrier_destroy", referenced from:
_uv_barrier_destroy in libdatabaseclient.a(thread.c.o)
"_pthread_barrier_init", referenced from:
_uv_barrier_init in libdatabaseclient.a(thread.c.o)
"_pthread_barrier_wait", referenced from:
_uv_barrier_wait in libdatabaseclient.a(thread.c.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [tests/unit_tests] Error 1
make[1]: *** [tests/CMakeFiles/unit_tests.dir/all] Error 2
make: *** [all] Error 2
unit_tests is my unit tests executable.
I think there's something I'm not linking against, just don't know what. Any clues?
Hi I just ran into this same problem, it seems pthread_barrier primitives are not implemented on MacOS despite it's claims of POSIX compliance. LibUV implements it's own versions using other threading primitives. If you add ${LIBUVDIR}/src/unix/pthread-barrier.c to your list of libuv c files it should link correctly.
Here is what I have in CMakeLists.txt for libuv
set(LIBUVDIR libuv)
include_directories(${LIBUVDIR}/include ${LIBUVDIR}/src)
set(SOURCES
${LIBUVDIR}/src/fs-poll.c
${LIBUVDIR}/src/inet.c
${LIBUVDIR}/src/threadpool.c
${LIBUVDIR}/src/uv-common.c
${LIBUVDIR}/src/version.c)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
add_definitions(-D_GNU_SOURCE)
set(SOURCES ${SOURCES}
${LIBUVDIR}/src/unix/linux-syscalls.c
${LIBUVDIR}/src/unix/linux-core.c
${LIBUVDIR}/src/unix/linux-inotify.c)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
add_definitions(-D_DARWIN_USE_64_BIT_INODE=1 -D_DARWIN_UNLIMITED_SELECT=1)
set(SOURCES ${SOURCES}
${LIBUVDIR}/src/unix/darwin.c
${LIBUVDIR}/src/unix/darwin-proctitle.c
${LIBUVDIR}/src/unix/fsevents.c
${LIBUVDIR}/src/unix/kqueue.c
${LIBUVDIR}/src/unix/pthread-barrier.c
${LIBUVDIR}/src/unix/proctitle.c)
endif()
include_directories(${LIBUVDIR}/src/unix)
set(SOURCES ${SOURCES}
${LIBUVDIR}/src/unix/async.c
${LIBUVDIR}/src/unix/core.c
${LIBUVDIR}/src/unix/dl.c
${LIBUVDIR}/src/unix/fs.c
${LIBUVDIR}/src/unix/getaddrinfo.c
${LIBUVDIR}/src/unix/getnameinfo.c
${LIBUVDIR}/src/unix/loop-watcher.c
${LIBUVDIR}/src/unix/loop.c
${LIBUVDIR}/src/unix/pipe.c
${LIBUVDIR}/src/unix/poll.c
${LIBUVDIR}/src/unix/process.c
${LIBUVDIR}/src/unix/signal.c
${LIBUVDIR}/src/unix/stream.c
${LIBUVDIR}/src/unix/tcp.c
${LIBUVDIR}/src/unix/thread.c
${LIBUVDIR}/src/unix/timer.c
${LIBUVDIR}/src/unix/tty.c
${LIBUVDIR}/src/unix/udp.c)
add_library(uv STATIC ${SOURCES})
target_link_libraries(uv pthread)

Cmake finds library but does not find symbols

I'm using cmake verison 2.8.11.2 to build a project using some external libraries and packages. However since I tried adding the library spglib I have been unable to compile the code. Despite CMake adding the correct .so file the resulting makefile does not correctly link to it.
I use the following CMake code in src/main/CMakeLists.txt to include the spglib .so file:
ADD_EXECUTABLE(PotFit ${SourceFiles})
SET_TARGET_PROPERTIES(PotFit PROPERTIES COMPILE_DEFINITIONS "${POTFIT_COMPILE_DEFINITIONS}")
TARGET_LINK_LIBRARIES(PotFit PotFitLibrary boost_timer boost_system)
set(CMAKE_PREFIX_PATH [path to]/src/.libs)
find_library(SPGLIBRARY NAMES spg spglib libsymspg symspg PATHS /home/staffana /CS/trunk/spglib-1.6.0/src/.libs [path to]/spglib-1.6.0/src)
target_link_libraries(PotFit "${SPGLIBRARY}")
Which seems to work. I check that CMake actually finds the library using
message("The value of SPGLibrary is")
message("${SPGLIBRARY}")
which correctly returns the path to libsymspg.so. However the compiler returns the error
CMakeFiles/PotFit.dir/Main.cpp.o: In function `main':
Main.cpp:(.text.startup+0x11a): undefined reference to `spg_get_spacegroup_type(int)'
collect2: error: ld returned 1 exit status
make[2]: *** [main/PotFit] Error 1
make[1]: *** [main/CMakeFiles/PotFit.dir/all] Error 2
make: *** [all] Error 2
*** Failure: Exit code 2 ***
I make sure that the libsymspg.so does contain this symbol by using
nm libsymspg.so | grep get_spacegroup_type
which returns
0000ad30 T spgdb_get_spacegroup_type
0000c240 T spg_get_spacegroup_type
So between Cmake and linking something is wrong. As far as I know I am using the "normal" way of doing things, so I am not sure where to begin looking for what is causing this problem. I have appended all whole CMakeLists.txt below since that might be useful and I can add the CMakeCache if needed (it's p. long...).
Full Cmake source
src/CMake.txt :
PROJECT(FitPot CXX C Fortran)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
OPTION(USE_OPENMP "Use OpenMP parallelization" "ON")
OPTION(ENABLE_DEBUG_CHECKS "Enable sanity checks in the code." "ON")
# Find and set up Qt4 library.
SET(QT_USE_QTOPENGL FALSE)
SET(QT_USE_QTGUI FALSE)
SET(QT_USE_QTXML TRUE)
SET(QT_USE_QTXMLPATTERNS FALSE)
SET(QT_MIN_VERSION "4.6.0")
# Use the Q_SIGNALS and Q_SLOTS macros to avoid name conflicts with Python.
ADD_DEFINITIONS(-DQT_NO_KEYWORDS)
FIND_PACKAGE(Qt4)
IF(NOT QT4_FOUND)
IF(QT4_INSTALLED_VERSION_TOO_OLD)
MESSAGE(FATAL_ERROR "The installed Qt version ${QTVERSION} is too old, at least version ${QT_MIN_VERSION} is required.")
ELSE(QT4_INSTALLED_VERSION_TOO_OLD)
MESSAGE(FATAL_ERROR "The Qt library or the qmake program was not found! Please install Qt manually and specify the path to the 'qmake' program by setting the QT_QMAKE_EXECUTABLE setting in CMake. You need at least version ${QT_MIN_VERSION}.")
ENDIF(QT4_INSTALLED_VERSION_TOO_OLD)
ENDIF(NOT QT4_FOUND)
INCLUDE(${QT_USE_FILE})
# Find Boost library.
#SET(BOOST_ROOT "/usr/local/boost_1_50_0/") # Might want to use this line, a modified version. !!MODIFYME
FIND_PACKAGE(Boost REQUIRED)
IF(NOT Boost_FOUND)
MESSAGE(FATAL_ERROR "Boost library not found. Reason: ${Boost_ERROR_REASON}")
ENDIF()
# Modify this line to point to the directory where the boost libraries are. !!MODIFYME
SET(Boost_LIBRARY_DIRS "/usr/local/boost_1_50_0/stage/lib")
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
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}")
endif()
MESSAGE("boost lib dirs: ${Boost_LIBRARY_DIRS}")
MESSAGE("boost include dirs: ${Boost_INCLUDE_DIRS}")
# Choose compiler flags for Fortran compiler.
GET_FILENAME_COMPONENT(Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)
IF(Fortran_COMPILER_NAME MATCHES "gfortran")
# gfortran:
SET(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE} -funroll-all-loops -fno-f2c -O3")
SET(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} -fno-f2c -O0 -g")
ELSEIF(Fortran_COMPILER_NAME STREQUAL "ifort")
# ifort:
# The '-fltconsistency' flag forces the compiler to "maintain floating point precision";
# replaces -mp option in older ifort versions.
# This is necessary to avoid hangups in the 'dpmeps' function in lbfgs.f on some systems.
SET(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE} -fltconsistency -f77rtl -O3")
SET(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} -fltconsistency -f77rtl -O0 -g")
ELSEIF(Fortran_COMPILER_NAME STREQUAL "g77")
# g77:
SET(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE} -funroll-all-loops -fno-f2c -O3 -m32")
SET(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} -fno-f2c -O0 -g -m32")
ELSE()
MESSAGE("Unknown Fortran compiler (${Fortran_COMPILER_NAME}), using default flags")
SET(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE} -O2")
SET(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} -O0 -g")
ENDIF()
SET(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS_RELEASE}")
MESSAGE("Fortran flags: ${CMAKE_Fortran_FLAGS}")
# Code files will reference header files relative to the root source directory.
INCLUDE_DIRECTORIES(".")
IF(ENABLE_DEBUG_CHECKS)
SET(POTFIT_COMPILE_DEFINITIONS "DEBUG_FITPOT")
ENDIF()
# Creates a sub CMakeList.txt in those two subdirectories where source files are defined.
ADD_SUBDIRECTORY(potfitlib)
ADD_SUBDIRECTORY(main)
MESSAGE("Build type: ${CMAKE_BUILD_TYPE}")
MESSAGE("cxx flags: ${CMAKE_CXX_FLAGS}")
MESSAGE("cxx linker flags: ${CMAKE_EXE_LINKER_FLAGS}")
MESSAGE("Library path is : ${CMAKE_LIBRARY_PATH}")
src/potfitlib/CMakeLists.txt
# List of source files that need to be compiled:
SET(SourceFiles
util/linalg/LinAlg.cpp
util/Debug.cpp
util/Dijkstra.cpp
job/FitJob.cpp
job/Fitting.cpp
job/Validation.cpp
potentials/Potential.cpp
potentials/TabulatedEAMPotential.cpp
potentials/SplineMEAMPotential.cpp
potentials/LennardJonesPotential.cpp
potentials/CDIPotential.cpp
potentials/HarmonicModelFullPhi.cpp
potentials/HarmonicModel.cpp
potentials/PairPotential.cpp
potentials/FourthOrderModel.cpp
potentials/functions/CubicSpline.cpp
potentials/functions/GridCubicSpline.cpp
potentials/functions/Functions.cpp
potentials/fourthOrderHelpers/Bond.cpp
potentials/fourthOrderHelpers/TupleFinder.cpp
potentials/fourthOrderHelpers/SymmetryOperator.cpp
potentials/fourthOrderHelpers/GeneralizedDirection.cpp
potentials/fourthOrderHelpers/ForceTuple.cpp
potentials/fourthOrderHelpers/OldEnergyAndForces.cpp
potentials/fourthOrderHelpers/ForceComputation.cpp
properties/FitProperty.cpp
properties/AtomVectorProperty.cpp
dof/DegreeOfFreedom.cpp
dof/AtomCoordinatesDOF.cpp
structures/FitObject.cpp
structures/StructureGroup.cpp
structures/AtomicStructure.cpp
structures/UserStructure.cpp
structures/NeighborList.cpp
structures/LatticeStructures.cpp
minimizer/SplitBregmanFunctionEvaluator.cpp
minimizer/Minimizer.cpp
minimizer/SplitBregmanMinimizer.cpp
minimizer/MinimizerParameters.cpp
minimizer/lbfgsb.f
util/xml/XMLParserUtilities.cpp
util/xml/XMLWriterUtilities.cpp
)
# Include a resource file in the library.
QT4_ADD_RESOURCES(ResourceFiles resources/resources.qrc)
ADD_LIBRARY(PotFitLibrary STATIC ${SourceFiles} ${ResourceFiles})
SET_TARGET_PROPERTIES(PotFitLibrary PROPERTIES COMPILE_DEFINITIONS "${POTFIT_COMPILE_DEFINITIONS}")
# Link with the Qt libraries.
TARGET_LINK_LIBRARIES(PotFitLibrary ${QT_LIBRARIES})
# Enable OpenMP parallelization when using a GNU C++ compiler.
IF(CMAKE_COMPILER_IS_GNUCXX AND USE_OPENMP)
SET_TARGET_PROPERTIES(PotFitLibrary PROPERTIES COMPILE_FLAGS "-fopenmp")
SET_TARGET_PROPERTIES(PotFitLibrary PROPERTIES LINK_FLAGS "-fopenmp")
ENDIF(CMAKE_COMPILER_IS_GNUCXX AND USE_OPENMP)
src/main/CMakeLists.txt
# List of source files that need to be compiled:
SET(SourceFiles
Main.cpp
)
INCLUDE_DIRECTORIES([path to]/spglib-1.6.0/src)
INCLUDE_DIRECTORIES([path to]/spglib-1.6.0/src/.libs)
LINK_DIRECTORIES([path to]/spglib-1.6.0/src/.libs )
ADD_EXECUTABLE(PotFit ${SourceFiles})
SET_TARGET_PROPERTIES(PotFit PROPERTIES COMPILE_DEFINITIONS "${POTFIT_COMPILE_DEFINITIONS}")
TARGET_LINK_LIBRARIES(PotFit PotFitLibrary boost_timer boost_system)
set(CMAKE_PREFIX_PATH [path_to]/spglib-1.6.0/src/.libs)
find_library(SPGLIBRARY NAMES spg spglib libsymspg symspg PATHS [path_to]/spglib-1.6.0/src/.libs [path_to]/spglib-1.6.0/src)
target_link_libraries(PotFit "${SPGLIBRARY}")
message("The value of SPGLibrary is")
message("${SPGLIBRARY}")
MESSAGE("Spglib link flags: ${SPGLIB_LINK_FLAGS}")
MESSAGE("Build type: ${CMAKE_BUILD_TYPE}")
MESSAGE("cxx flags: ${CMAKE_CXX_FLAGS}")
MESSAGE("cxx linker flags: ${CMAKE_EXE_LINKER_FLAGS}")
MESSAGE("Library path is : ${CMAKE_LIBRARY_PATH}")
Add to the include
extern "C"
{
#include "symspg.h" // or whatever
}

Problems setting up gsl with cmake

I was able to successfully configure and generate the build folder (KinectSLAM6D/build.). However, when I try to build it using make, I got an error saying gsl is not found. I am pretty sure this is just a configuration issue as I have gsl installed (they're in usr/local), but I am unable to configure it. I have tried adding the following lines to CMakeList:
include_directories(${GSL_INCLUDE_DIRS} ${GSLCBLAS_INCLUDE_DIRS})
set(LIBS ${LIBS} ${GSL_LIBRARIES} ${GSLCBLAS_LIBRARIES})
I have copied the pertinent output below. I have found a couple of answers to compiling with gsl (adding -lgsl). However, I have no clue where to put that in CMakeLists or the generated MakeList and MakeList2 files.
Here's the generated output.
....
[ 44%] Building CXX object CMakeFiles/Kinect6DSLAM.dir/src/GraphOptimizer_G2O.cpp.o
[ 45%] Building CXX object CMakeFiles/Kinect6DSLAM.dir/src/CKinect2DRawlog.cpp.o
Linking CXX executable Kinect6DSLAM
/usr/bin/ld: warning: libopencv_core.so.2.3, needed by /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libmrpt-base.so, may conflict with libopencv_core.so.2.4
/usr/bin/ld: warning: libopencv_imgproc.so.2.3, needed by /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libmrpt-base.so, may conflict with libopencv_imgproc.so.2.4
/usr/bin/ld: warning: libopencv_highgui.so.2.3, needed by /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libmrpt-base.so, may conflict with libopencv_highgui.so.2.4
../gicp/libgicp.a(gicp.o): In function `dgc::gicp::GICPPointSet::ComputeMatrices()':
gicp.cpp:(.text+0x462): undefined reference to `gsl_vector_alloc'
gicp.cpp:(.text+0x470): undefined reference to `gsl_vector_alloc'
... a bunch more undefined references to gsl
collect2: ld returned 1 exit status
make[2]: *** [Kinect6DSLAM] Error 1
make[1]: *** [CMakeFiles/Kinect6DSLAM.dir/all] Error 2
make: *** [all] Error 2
This if the full CMakeList.txt. I am trying to run Miguel Algaba's SLAM project.
PROJECT(KinectSLAM6D)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW) # Required by CMake 2.7+
endif(COMMAND cmake_policy)
# Set the output directory for the build executables
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/build)
#Add here your project dependencies
FIND_PACKAGE(MRPT REQUIRED hwdrivers maps graphslam) #Add here your project dependencies
FIND_PACKAGE(PCL REQUIRED)
FIND_PACKAGE(OpenCV REQUIRED )
INCLUDE_DIRECTORIES(${PCL_INCLUDE_DIRS})
LINK_DIRECTORIES(${PCL_LIBRARY_DIRS})
ADD_DEFINITIONS(${PCL_DEFINITIONS})
# Required by StanfordGICP
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
FIND_PACKAGE(GSL REQUIRED)
include_directories(${GSL_INCLUDE_DIRS} ${GSLCBLAS_INCLUDE_DIRS})
set(LIBS ${LIBS} ${GSL_LIBRARIES} ${GSLCBLAS_LIBRARIES})
FIND_PACKAGE(Boost COMPONENTS system program_options REQUIRED)
include_directories(${PROJECT_SOURCE_DIR}/gicp)
include_directories(${PROJECT_SOURCE_DIR}/gicp/ann_1.1.1/include/ANN)
# G2O library
# Set up the top-level include directories
SET( G2O_INCLUDE ${PROJECT_SOURCE_DIR}/EXTERNAL/g2o CACHE PATH "Directory of G2O")
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR} ${G2O_INCLUDE})
# Add g2o lib dir
LINK_DIRECTORIES( ${LINK_DIRECTORIES} "${G2O_INCLUDE}/lib" )
#Generate config.h
configure_file(g2o/trunk/config.h.in ${PROJECT_BINARY_DIR}/g2o/config.h)
include_directories(${PROJECT_BINARY_DIR})
INSTALL(FILES ${PROJECT_BINARY_DIR}/g2o/config.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/g2o)
# Include the subdirectories
ADD_SUBDIRECTORY(g2o/trunk)
INCLUDE_DIRECTORIES(${CSPARSE_INCLUDE_DIR}) #You can use CPARSE or CHOLMOD
INCLUDE_DIRECTORIES(${CHOLMOD_INCLUDE_DIR})
set(G2O_DIR ${PROJECT_SOURCE_DIR}/g2o/trunk)
include_directories(${G2O_DIR})
link_directories(${G2O_DIR}/lib)
# Declare the target (an executable)
ADD_EXECUTABLE(Kinect6DSLAM kinect6DSLAM.cpp
./include/KinectGrabber.h
./include/KinectGrabber_OpenNI.h
./src/KinectGrabber_OpenNI.cpp
./include/KinectGrabber_Rawlog.h
./src/KinectGrabber_Rawlog.cpp
./include/KinectGrabber_Rawlog2.h
./src/KinectGrabber_Rawlog2.cpp
./include/KinectGrabber_MRPT.h
./src/KinectGrabber_MRPT.cpp
./include/VisualFeatureDescriptorExtractor.h
./include/VisualFeatureDescriptorExtractor_SURF_GPU.h
./include/VisualFeatureDescriptorExtractor_Generic.h
./include/VisualFeatureDescriptorExtractor_ORB.h
./include/VisualFeatureMatcher.h
./include/VisualFeatureMatcher_Generic.h
./include/PointCloudViewer.h
./include/PointCloudViewer_MRPT.h
./src/VisualFeatureDescriptorExtractor_SURF_GPU.cpp
./src/VisualFeatureDescriptorExtractor_Generic.cpp
./src/VisualFeatureDescriptorExtractor_ORB.cpp
./src/VisualFeatureMatcher_Generic.cpp
./src/PointCloudViewer_MRPT.cpp
./include/Visual3DRigidTransformationEstimator.h
./include/Visual3DRigidTransformationEstimator_SVD.h
./src/Visual3DRigidTransformationEstimator_SVD.cpp
./include/Visual3DRigidTransformationEstimator_RANSAC.h
./src/Visual3DRigidTransformationEstimator_RANSAC.cpp
./include/ICPPoseRefiner.h
./include/ICPPoseRefiner_PCL.h
./src/ICPPoseRefiner_PCL.cpp
./include/ICPPoseRefiner_StanfordGICP.h
./src/ICPPoseRefiner_StanfordGICP.cpp
./include/Miscellaneous.h
./src/Miscellaneous.cpp
./include/FrameRGBD.h
./src/FrameRGBD.cpp
./include/PointCloudDownsampler.h
./src/PointCloudDownsampler.cpp
./include/KeyframeLoopDetector.h
./src/KeyframeLoopDetector.cpp
./include/GraphOptimizer.h
./include/GraphOptimizer_MRPT.h
./src/GraphOptimizer_MRPT.cpp
./include/GraphOptimizer_G2O.h
./src/GraphOptimizer_G2O.cpp
./include/CKinect2DRawlog.h
./src/CKinect2DRawlog.cpp
)
TARGET_LINK_LIBRARIES(Kinect6DSLAM ${MRPT_LIBS}
${PCL_LIBRARIES}
${OpenCV_LIBS}
${Boost_LIBRARIES}
#GICP
${GSL_LIBRARIES}
${PROJECT_SOURCE_DIR}/gicp/ann_1.1.1/lib/libANN.a
${PROJECT_SOURCE_DIR}/gicp/libgicp.a
#G2O
core math_groups types_slam3d
solver_csparse #You can use CPARSE or CHOLMOD
solver_cholmod ${CHOLMOD_LIBRARIES}
)
# Declare the target (an executable)
ADD_EXECUTABLE(PairwiseAlignmentSteps PairwiseAlignmentSteps.cpp
./include/KinectGrabber.h
./include/KinectGrabber_OpenNI.h
./src/KinectGrabber_OpenNI.cpp
./include/KinectGrabber_Rawlog.h
./src/KinectGrabber_Rawlog.cpp
./include/KinectGrabber_MRPT.h
./src/KinectGrabber_MRPT.cpp
./include/VisualFeatureDescriptorExtractor.h
./include/VisualFeatureDescriptorExtractor_SURF_GPU.h
./include/VisualFeatureDescriptorExtractor_Generic.h
./include/VisualFeatureDescriptorExtractor_ORB.h
./include/VisualFeatureMatcher.h
./include/VisualFeatureMatcher_Generic.h
./include/PointCloudViewer.h
./include/PointCloudViewer_MRPT.h
./src/VisualFeatureDescriptorExtractor_SURF_GPU.cpp
./src/VisualFeatureDescriptorExtractor_Generic.cpp
./src/VisualFeatureDescriptorExtractor_ORB.cpp
./src/VisualFeatureMatcher_Generic.cpp
./src/PointCloudViewer_MRPT.cpp
./include/Visual3DRigidTransformationEstimator.h
./include/Visual3DRigidTransformationEstimator_SVD.h
./src/Visual3DRigidTransformationEstimator_SVD.cpp
./include/Visual3DRigidTransformationEstimator_RANSAC.h
./src/Visual3DRigidTransformationEstimator_RANSAC.cpp
./include/ICPPoseRefiner.h
./include/ICPPoseRefiner_PCL.h
./src/ICPPoseRefiner_PCL.cpp
./include/ICPPoseRefiner_StanfordGICP.h
./src/ICPPoseRefiner_StanfordGICP.cpp
./include/Miscellaneous.h
./src/Miscellaneous.cpp
./include/FrameRGBD.h
./src/FrameRGBD.cpp
./include/PointCloudDownsampler.h
./src/PointCloudDownsampler.cpp
)
TARGET_LINK_LIBRARIES(PairwiseAlignmentSteps ${MRPT_LIBS}
${PCL_LIBRARIES}
${OpenCV_LIBS}
${Boost_LIBRARIES}
#GICP
${GSL_LIBRARIES}
${PROJECT_SOURCE_DIR}/gicp/ann_1.1.1/lib/libANN.a
${PROJECT_SOURCE_DIR}/gicp/libgicp.a
)
# Set optimized building:
IF(CMAKE_COMPILER_IS_GNUCXX AND NOT CMAKE_BUILD_TYPE MATCHES "Debug")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -mtune=native")
ENDIF(CMAKE_COMPILER_IS_GNUCXX AND NOT CMAKE_BUILD_TYPE MATCHES "Debug")
target_link_libraries(Kinect6DSLAM ${LIBS})
Something tells me, that adding target_link_libraries(Kinect6DSLAM ${LIBS}) will help you.
Also, instead of constructs like
set(VAR ${VAR} somethingelse)
you can use this:
list(APPEND VAR somethingelse)

Building library with cmake

I apologize for bothering you all, but I have a little compilation problem with cmake.
I have a CMakeLists.txt file I'm using to build a test executable, and a shared library. They both have dependency to another library (SFML).
I'm using cmake on window with MinGW.
I know the name of the lib I'm building is kinda confusing with the sfml one, but it's supposed to be a SFML wrapper, so, I didn't find a better name!
Here the CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(projectName)
set(EXECUTABLE_NAME testSFML)
set(LIBRARY_NAME SFMLwindow)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin/)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include /
${CMAKE_CURRENT_SOURCE_DIR}/../../include
)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../lib/)
file(
GLOB_RECURSE
SRC_FILES
src/*
)
file(
GLOB_RECURSE
INCLUDE_FILES
include/*
)
add_executable(
${EXECUTABLE_NAME}
main.cpp
${SRC_FILES}
${INCLUDE_FILES}
)
target_link_libraries(
${EXECUTABLE_NAME}
sfml-main
sfml-system
sfml-window
)
add_library(
${LIBRARY_NAME}
SHARED
${SRC_FILES}
)
And what I get in the terminal :
"C:\MinGW\bin\mingw32-make.exe"
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/iksemel/docs/WorkBench/programming/projets/TestSFML/cmake
Linking CXX shared library libSFMLwindow.dll
Creating library file: libSFMLwindow.dll.a
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x59):undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0xda): undefined reference to `_imp___ZN2sf6WindowC1ENS_9VideoModeERKSsjRKNS_15ContextSettingsE'
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x163): undefined reference to `_imp___ZN2sf6Window5closeEv'
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x1bd): undefined reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE'
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x1d8): undefined reference to `_imp___ZN2sf6Window7displayEv'
collect2: ld a retourné 1 code d'état d'exécution
mingw32-make.exe[2]: *** [libSFMLwindow.dll] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/SFMLwindow.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
If anybody have a clue on what's happening, I'd be very gratefull!
At a guess, your SFMLwindow library needs linked to some or all of sfml-main, sfml-system, sfml-window.
You could try changing the end of your CMakeLists.txt to:
add_library(
${LIBRARY_NAME}
SHARED
${SRC_FILES}
${INCLUDE_FILES}
)
add_executable(
${EXECUTABLE_NAME}
main.cpp
)
target_link_libraries(
${LIBRARY_NAME}
sfml-main
sfml-system
sfml-window
)
target_link_libraries(
${EXECUTABLE_NAME}
${LIBRARY_NAME}
)
As an aside, file(GLOB_RECURSE... is generally frowned upon as a way to gather a list of sources. From the docs for file:
We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.
Also, find_library should be preferred to link_directories in this case. From the docs for link_directories:
Note that this command is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths. Pass these absolute library file paths directly to the target_link_libraries() command. CMake will ensure the linker finds them.