I am currently trying to compile a project on Mac using cmake, but running into trouble. I have already looked at this article [1], but am still running into some trouble. My CMakeLists.txt looks as follows.
project(tpch_framework)
# enable c++11
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_C_COMPILER "/usr/local/Cellar/llvm/10.0.0_3/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/local/Cellar/llvm/10.0.0_3/bin/clang++")
set(OPENMP_LIBRARIES "/usr/local/Cellar/llvm/10.0.0_3/lib")
set(OPENMP_INCLUDES "/usr/local/Cellar/llvm/10.0.0_3/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_FLAGS "-Xpreprocessor -fopenmp")
set(OpenMP_C_LIB_NAMES "omp")
set(OpenMP_omp_LIBRARY omp)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(OpenMP_CXX_FLAGS "-Xpreprocessor -fopenmp")
set(OpenMP_CXX_LIB_NAMES "omp")
set(OpenMP_omp_LIBRARY omp)
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} -libomp")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS} -libomp")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif(OPENMP_FOUND)
# Configure required Boost libraries
set(BOOST_ROOT "" CACHE PATH "Boost build root (useful on Windows)")
option(Boost_USE_STATIC_LIBS
"Search for static boost libs" OFF)
option(Boost_USE_MULTITHREADED
"Search for multithreaded boost libs" ON)
option(Boost_USE_STATIC_RUNTIME
"Search for boost libs linked against static C++ runtime" OFF)
find_package(Boost 1.47.0 REQUIRED filesystem system)
# ensure that dependant libraries not explicitly specified here
# are found by the linker:
link_directories(${Boost_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIRS})
set(LIBS ${LIBS} ${Boost_LIBRARIES})
#Bring the headers into the project
include_directories(include)
FILE(GLOB_RECURSE INC_ALL "include/*.hpp")
#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")
add_library(tpch_framework ${SOURCES})
add_executable(framework main.cpp ${INC_ALL})
target_link_libraries(framework tpch_framework)
#target_link_libraries(framework stdc++fs)
target_link_libraries(framework ${LIBS})
When I execute this, I get the following output.
-- The C compiler identification is AppleClang 11.0.0.11000033
-- The CXX compiler identification is AppleClang 11.0.0.11000033
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenMP_C: -Xpreprocessor -fopenmp
-- Found OpenMP_CXX: -Xpreprocessor -fopenmp
-- Found OpenMP: TRUE
-- Found Boost: /usr/local/lib/cmake/Boost-1.72.0/BoostConfig.cmake (found suitable version "1.72.0", minimum required is "1.47.0") found components: filesystem system
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/...
However, when I then use make, I get the following error.
/Users/myname/Desktop/Uni/MHD/tpch_framework_challenge_mhd_2020/src/task4.cpp:48:5: error: use of undeclared identifier 'omp_set_num_threads'
omp_set_num_threads(4);
Which I am guessing comes, as I get this warning: clang-10: warning: -libomp: 'linker' input unused [-Wunused-command-line-argument].
Does anybody have any ideas? I have been stuck with this for way too long and would appreciate any tips.
Kind regards,
Moritz
If you can use a recent version of CMake, this build should work.
# NOTE: Every top-level CMakeLists.txt must start with these two lines
cmake_minimum_required(VERSION 3.16)
project(tpch_framework)
## Enable C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
## Find dependencies
# OpenMP
find_package(OpenMP REQUIRED)
# Boost
set(BOOST_ROOT "" CACHE PATH "Boost build root (useful on Windows)")
option(Boost_USE_STATIC_LIBS
"Search for static boost libs" OFF)
option(Boost_USE_MULTITHREADED
"Search for multithreaded boost libs" ON)
option(Boost_USE_STATIC_RUNTIME
"Search for boost libs linked against static C++ runtime" OFF)
find_package(Boost 1.47.0 REQUIRED COMPONENTS filesystem system)
## Add main project targets
# NOTE: This is completely wrong and will break incremental builds after
# doing a "git pull" or similar. You should never glob for source
# files, but instead list them explicitly.
file(GLOB SOURCES "src/*.cpp")
add_library(tpch_framework ${SOURCES})
target_include_directories(tpch_framework PUBLIC BUILD_INTERFACE:include)
target_link_libraries(tpch_framework
PUBLIC
OpenMP::OpenMP_CXX
Boost::boost
Boost::filesystem
Boost::system)
add_executable(framework main.cpp)
target_link_libraries(framework PRIVATE tpch_framework)
There is so much bad CMake advice here on SO and in tutorials, it's a real shame.
Related
I'm on windows 10 and I want to use curlpp library. The building of curlpp depends on the availability of curl and curl is by default present in my system but when i try to run the cmake, it gives the following output
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.19043.
-- The C compiler identification is MSVC 19.29.30145.0
-- The CXX compiler identification is MSVC 19.29.30145.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- curlpp version=[0.8.1]
-- curlpp version num=[000801]
-- Looking for CURL
-- Could NOT find CURL (missing: CURL_LIBRARY CURL_INCLUDE_DIR)
CMake Error at C:/Program Files/CMake/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find CURL (missing: CURL_LIBRARY CURL_INCLUDE_DIR)
Call Stack (most recent call first):
C:/Program Files/CMake/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
C:/Program Files/CMake/share/cmake-3.22/Modules/FindCURL.cmake:181 (find_package_handle_standard_args)
CMakeLists.txt:60 (find_package)
Here is the CmakeLists.txt of curlpp
project(curlpp)
# In response to CMake 3.0 generating warnings regarding policy CMP0042,
# the OSX RPATH settings have been updated per recommendations found
# in the CMake Wiki:
# http://www.cmake.org/Wiki/CMake_RPATH_handling#Mac_OS_X_and_the_RPATH
if(POLICY CMP0042)
cmake_policy(SET CMP0042 NEW) # MACOSX_RPATH
set(CMAKE_MACOSX_RPATH TRUE)
endif()
# for unix platform, define install directories.
include(GNUInstallDirs)
if(WIN32)
# cmake 3.4 is required for CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS
cmake_minimum_required(VERSION 3.4)
# c++ 11 support from cmake 3.4 or newer
set(CMAKE_CXX_STANDARD 11) # C++11...
set(CMAKE_CXX_STANDARD_REQUIRED ON) #...is required...
set(CMAKE_CXX_EXTENSIONS OFF) #...without compiler extensions like gnu++11
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
else()
if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} VERSION_LESS 3.1)
cmake_minimum_required(VERSION 2.8)
# c++11 support for cmake 2.8.12 - 3.0.x
#
# for non-windows platform we try to keep cmake 2.8 support
# since entreprise distribution tends to have 2.8 version.
add_compile_options(-std=c++11)
else()
# c++ 11 support from cmake 3.1 or newer
set(CMAKE_CXX_STANDARD 11) # C++11...
set(CMAKE_CXX_STANDARD_REQUIRED ON) #...is required...
set(CMAKE_CXX_EXTENSIONS OFF) #...without compiler extensions like gnu++11
endif()
endif()
# Conan.io integration
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/conanbuildinfo.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
endif()
# extra (pkg-config-related files)
add_subdirectory(extras)
#########################################################################################
# Look for dependencies
# Documented at https://cmake.org/cmake/help/v3.0/module/FindCURL.html?highlight=curlpp
# Seems simple.
message(STATUS "Looking for CURL")
include(FindCURL)
find_package(CURL REQUIRED)
if(CURL_FOUND)
message(STATUS "Found CURL version: ${CURL_VERSION_STRING}")
message(STATUS "Using CURL include dir(s): ${CURL_INCLUDE_DIRS}")
message(STATUS "Using CURL lib(s): ${CURL_LIBRARIES}")
else()
message(FATAL_ERROR "Could not find CURL")
endif()
# All following targets should search these directories for headers
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/include
${CURL_INCLUDE_DIRS}
)
#########################################################################################
# Define Targets
# If building on windows, install path will be in build/winbuild
if(CMAKE_SYSTEM MATCHES "Windows")
set(CMAKE_INSTALL_PREFIX "winbuild")
endif()
file(GLOB_RECURSE HeaderFileList "${CMAKE_CURRENT_SOURCE_DIR}/include/*")
file(GLOB_RECURSE SourceFileList "${CMAKE_CURRENT_SOURCE_DIR}/src/*")
add_library(${PROJECT_NAME} SHARED ${HeaderFileList} ${SourceFileList})
target_link_libraries(${PROJECT_NAME} ${CURL_LIBRARIES} ${CONAN_LIBS})
set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION 1 VERSION 1.0.0)
add_library(${PROJECT_NAME}_static STATIC ${HeaderFileList} ${SourceFileList})
# Make sure that on unix-platforms shared and static libraries have
# the same root name, but different suffixes.
#
# (solution taken from https://cmake.org/Wiki/CMake_FAQ#How_do_I_make_my_shared_and_static_libraries_have_the_same_root_name.2C_but_different_suffixes.3F)
#
# Making shared and static libraries have the same root name, but different suffixes
SET_TARGET_PROPERTIES(${PROJECT_NAME}_static PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
# Now the library target "curlpp_static" will be named "curlpp.lib" with MS tools.
# This conflicts with the "curlpp.lib" import library corresponding to "curlpp.dll",
# so we add a "lib" prefix (which is default on other platforms anyway):
SET_TARGET_PROPERTIES(${PROJECT_NAME}_static PROPERTIES PREFIX "lib")
target_link_libraries(${PROJECT_NAME}_static ${CURL_LIBRARIES} ${CONAN_LIBS})
# install headers
install(DIRECTORY include/utilspp/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/utilspp")
install(DIRECTORY include/curlpp/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/curlpp")
install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_static
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
I've also downloaded curl from GitHub and had successfully build it but i don't know how to tell the cmake to find the curl
Any help would be appreciated. thanks
I've been struggling to understand why CMake does not compile my program anymore. I made a container for cross-compile a C++ program for Windows. The environment:
Fedora 31
cmake 3.17.4
x86_64-w64-mingw32-g++ 9.2.1
boost 1.69
The following code is my current cmake toolchain file for mingw:
set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PFX x86_64-w64-mingw32)
set(Boost_ARCHITECTURE -x64)
set(Boost_DEBUG ON)
set(Boost_INCLUDE_DIR /usr/${TOOLCHAIN_PFX}/sys-root/mingw/include)
set(Boost_LIBRARY_DIR /usr/${TOOLCHAIN_PFX}/sys-root/mingw/lib)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.69 REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/usr/local/include -L/usr/local/lib")
set(CMAKE_C_COMPILER ${TOOLCHAIN_PFX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PFX}-g++)
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PFX})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
The building process interrupts when linking the executable. It produces:
/usr/lib/gcc/x86_64-w64-mingw32/9.2.1/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lboost_thread-mt
/usr/lib/gcc/x86_64-w64-mingw32/9.2.1/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lboost_log-mt
/usr/lib/gcc/x86_64-w64-mingw32/9.2.1/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lboost_system
What shocks me the most is that it used to build (no changes have been made to the environment) and boost is installed from RPM. Funny things is that if I omit -lboost_thread-mt, off course, it complains.
You are overwriting many variables that should be correctly set by find_package() such as Boost_LIBRARY_DIR.
Also, you don't specify any components with find_package, as would be the standard way, unless you just want to use the header-only part of boost.
Thus, you are using find_package boost in unsupported/unspecified ways which might have worked in the past, by chance, but broke later.
What definitely works on Fedora 36 is the following:
cmake_minimum_required(VERSION 3.0...3.22.1)
project(some_project CXX)
# work-around: FindBoost: Boost_ARCHITECTURE not detected on MinGW
# https://gitlab.kitware.com/cmake/cmake/-/issues/20587
if(MINGW)
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(Boost_ARCHITECTURE "-x64")
else()
set(Boost_ARCHITECTURE "-x32")
endif()
endif()
# works with/without
set(Boost_USE_STATIC_LIBS ON)
# set(Boost_DEBUG ON)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.54
REQUIRED COMPONENTS
system
filesystem) # just an example, add what you need
add_executable(somebinary main.cc sometranslationunit.cc)
target_link_libraries(somebinary ${Boost_LIBRARIES})
Build with:
mkdir build-cross
cd build-cross
mingw64-cmake -DCMAKE_BUILD_TYPE=Debug .. # or whatever build type
mingw64-make somebinary
Windows build 18362
CLion 2019.1.2
Boost 1.67
Toolchain mingw-64
I'm trying to setup a simple project in Clion using Boost. The problem is when I include a boost header #include <boost/log/trivial.hpp>, CLion cannot find the header file.
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.14)
project(BoostTest)
set(CMAKE_CXX_STANDARD 14)
add_executable(BoostTest main.cpp)
set(BOOST_ROOT "$ENV{HOMEPATH}/.local/share/boost")
set(Boost_ARCHITECTURE "-x64")
find_package(Boost REQUIRED COMPONENTS log)
message(STATUS "Boost_INCLUDE_DIR: ${Boost_INCLUDE_DIR}")
include_directories(${Boost_INCLUDE_DIR})
target_link_libraries(BoostTest ${Boost_LIBRARIES})
The output of CMake seems to indicate that everything is found correctly:
C:\Users\michael\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\191.6707.69\bin\cmake\win\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\michael\projects\sml\BoostTest
-- The C compiler identification is GNU 8.1.0
-- The CXX compiler identification is GNU 8.1.0
-- Check for working C compiler: C:/mingw/mingw64/bin/gcc.exe
-- Check for working C compiler: C:/mingw/mingw64/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/mingw/mingw64/bin/g++.exe
-- Check for working CXX compiler: C:/mingw/mingw64/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE
-- Boost version: 1.67.0
-- Found the following Boost libraries:
-- log
-- date_time
-- log_setup
-- system
-- filesystem
-- thread
-- regex
-- chrono
-- atomic
-- Boost_INCLUDE_DIR: /Users/michael/.local/share/boost/include/boost-1_67
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/michael/projects/sml/BoostTest/cmake-build-debug
But still CLion cannot find the header file in the include statement. The header files are indeed present at the location specified in Boost_INCLUDE_DIR. I'm new to Windows and CLion , and I probably missing some really basic, but I cannot see it. Any clues?
After getting this to work from the command line, I've come to the conclusion that is a CLion bug. If the include directory does not include the drive designation (i.e., C:/), CLion is not finding the header files.
Here is a CMakeLists.txt that works for CLion:
cmake_minimum_required(VERSION 3.14)
project(boost-test)
set(CMAKE_CXX_STANDARD 14)
add_executable(boost-test main.cpp)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost REQUIRED COMPONENTS log)
if (Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
include_directories("C:${Boost_INCLUDE_DIRS}")
target_link_libraries(boost-test ${Boost_LIBRARIES})
endif()
This same file works from the command line without the hacky C: added to the include_directories.
Note that even without the C:, CLion will build and run the code just fine. So the issue seems to be solely with finding the header files based on include_directories().
I just tried doing this from Ubuntu, and no need for any hack to get this working.
find_package(Boost 1.40 COMPONENTS system filesystem program_options log REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
Not sure it may help, but at least it works somewhere.
I'm trying to compile two different git projects via CMake.
I have Visual Studio 2017 Community, MinGW and CodeBlocks installed and CMake throws an error with all of them. Keep in mind I have not used CMake before.
With "Visual Studio 15 2017 selected I get this error:
The C compiler identification is MSVC 19.12.25835.0
The CXX compiler identification is MSVC 19.12.25835.0
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx86/x86/cl.exe
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx86/x86/cl.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx86/x86/cl.exe
Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx86/x86/cl.exe -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Detecting CXX compile features
Detecting CXX compile features - done
Build type: Release
Performing Test COMPILER_SUPPORTS_CXX11
Performing Test COMPILER_SUPPORTS_CXX11 - Failed
Performing Test COMPILER_SUPPORTS_CXX0X
Performing Test COMPILER_SUPPORTS_CXX0X - Failed
CMake Error at CMakeLists.txt:26 (message):
The compiler C:/Program Files (x86)/Microsoft Visual
Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx86/x86/cl.exe has
no C++11 support. Please use a different C++ compiler.
With "MinGW Makefiles" selected I get a cc1.exe errors about a bunch of ddls. Sadly the popup error messages are in german even though I've set everything in Win10 to english so I dont know what they say exactly.
With "CodeBlocks - MinGW Makefiles" I get the same as above.
Link to one of the git projects Im trying to compile:
https://github.com/raulmur/ORB_SLAM2
https://github.com/YutaItoh/3D-Eye-Tracker
By the way, I tried the same with VS 2015 previously.
Content of the CMakeLists file:
cmake_minimum_required(VERSION 2.8)
project(ORB_SLAM2)
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
ENDIF()
MESSAGE("Build type: " ${CMAKE_BUILD_TYPE})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -march=native ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -march=native")
# Check C++11 or C++0x support
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_definitions(-DCOMPILEDWITHC11)
message(STATUS "Using flag -std=c++11.")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
add_definitions(-DCOMPILEDWITHC0X)
message(STATUS "Using flag -std=c++0x.")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
find_package(OpenCV 3.0 QUIET)
if(NOT OpenCV_FOUND)
find_package(OpenCV 2.4.3 QUIET)
if(NOT OpenCV_FOUND)
message(FATAL_ERROR "OpenCV > 2.4.3 not found.")
endif()
endif()
find_package(Eigen3 3.1.0 REQUIRED)
find_package(Pangolin REQUIRED)
include_directories(
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/include
${EIGEN3_INCLUDE_DIR}
${Pangolin_INCLUDE_DIRS}
)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
add_library(${PROJECT_NAME} SHARED
src/System.cc
src/Tracking.cc
src/LocalMapping.cc
src/LoopClosing.cc
src/ORBextractor.cc
src/ORBmatcher.cc
src/FrameDrawer.cc
src/Converter.cc
src/MapPoint.cc
src/KeyFrame.cc
src/Map.cc
src/MapDrawer.cc
src/Optimizer.cc
src/PnPsolver.cc
src/Frame.cc
src/KeyFrameDatabase.cc
src/Sim3Solver.cc
src/Initializer.cc
src/Viewer.cc
)
target_link_libraries(${PROJECT_NAME}
${OpenCV_LIBS}
${EIGEN3_LIBS}
${Pangolin_LIBRARIES}
${PROJECT_SOURCE_DIR}/Thirdparty/DBoW2/lib/libDBoW2.so
${PROJECT_SOURCE_DIR}/Thirdparty/g2o/lib/libg2o.so
)
# Build examples
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/Examples/RGB-D)
add_executable(rgbd_tum
Examples/RGB-D/rgbd_tum.cc)
target_link_libraries(rgbd_tum ${PROJECT_NAME})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/Examples/Stereo)
add_executable(stereo_kitti
Examples/Stereo/stereo_kitti.cc)
target_link_libraries(stereo_kitti ${PROJECT_NAME})
add_executable(stereo_euroc
Examples/Stereo/stereo_euroc.cc)
target_link_libraries(stereo_euroc ${PROJECT_NAME})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/Examples/Monocular)
add_executable(mono_tum
Examples/Monocular/mono_tum.cc)
target_link_libraries(mono_tum ${PROJECT_NAME})
add_executable(mono_kitti
Examples/Monocular/mono_kitti.cc)
target_link_libraries(mono_kitti ${PROJECT_NAME})
add_executable(mono_euroc
Examples/Monocular/mono_euroc.cc)
target_link_libraries(mono_euroc ${PROJECT_NAME})
The C++11 capability check will always fail with MSVC. Use CMake's CMAKE_CXX_STANDARD variable.
I like to automate the cross compiling for several projects on a Windows build server. I have some problems with the arm cross compiler. Setting it up seems a bit difficult. The cmake compiler check fails with a linking error(see below).
My experimental project looks like:
main.cpp - a test programm
CMakeLists.txt - the cmake build script
cmake/STM32F2xx.cmake - the special settings for the ARM STM32F2xx-series
I am using 3 Packages:
cmake-3.8.0-win64-x64.msi -cmake for windows
mingw-w64-install.exe - mingw 64 for windows
gcc-arm-none-eabi-6-2017-q1-update-win32.exe - arm x compiler
The main.cpp contains:
int main(void) {
return 0;
}
The CMakeLists.txt contains:
cmake_minimum_required (VERSION 3.0)
# module name
set(PrjName testPrj)
project (${PrjName})
# module version
set(PCK_MAJOR 1)
set(PCK_MINOR 0)
set(PCK_REVISION 0)
#####################
# Features switches
#####################
#####################
# architecture
#####################
# possible values:
# NATIVE
# STM32F2xx
SET(Arch NATIVE CACHE STRING "Architecture")
#############################
# software stack search path
############################
SET(SWstackPath NONE CACHE STRING "software stack search path")
#############################
# software stack handling
############################
if (${SWstackPath} STREQUAL NONE)
else ()
include_directories(${SWstackPath})
endif()
#############################
# package creation
#############################
SET(PackageFormat "DEFAULT" CACHE STRING "the format of the installation package")
########################
# define module content
########################
# MetaData header
set(testApp "testApp")
set(${testApp}_VERSION_MAJOR 1)
set(${testApp}_VERSION_MINOR 2)
set(${testApp}_VERSION_REVISION 3)
set(${testApp}_SOVERSION 4)
########################
# architecture handling
########################
if(${Arch} STREQUAL NATIVE)
MESSAGE("Architecture is set to NATIVE")
else()
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/${Arch}.cmake)
endif()
if (${PackageFormat} STREQUAL "DEFAULT")
if (UNIX)
include(GNUInstallDirs)
set(CPACK_GENERATOR "DEB")
else ()
set(CPACK_GENERATOR "ZIP")
set(CMAKE_INSTALL_FULL_BINDIR "bin")
set(CMAKE_INSTALL_FULL_LIBDIR "lib")
set(CMAKE_INSTALL_FULL_LIBDIRSTATIC "lib")
set(CMAKE_INSTALL_FULL_INCLUDEDIR "include")
set(CMAKE_INSTALL_FULL_MANDIR "doc")
endif()
elseif(${PackageFormat} STREQUAL "ZIP")
set(CPACK_GENERATOR "ZIP")
set(CMAKE_INSTALL_FULL_BINDIR "bin")
set(CMAKE_INSTALL_FULL_LIBDIR "lib")
set(CMAKE_INSTALL_FULL_LIBDIRSTATIC "lib")
set(CMAKE_INSTALL_FULL_INCLUDEDIR "include")
set(CMAKE_INSTALL_FULL_MANDIR "doc")
else()
message( FATAL_ERROR "The PackageFormat is unknown")
endif()
################################
# compile of the module content
################################
list(APPEND testApp_sources
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)
add_executable(${testApp}
${testApp_sources}
)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set_target_properties(${testApp} PROPERTIES
LINKER_LANGUAGE CXX
)
################################
# installation
################################
if(EXISTS "${CMAKE_ROOT}/Modules/CPack.cmake")
# install app
install(
TARGETS ${testApp}
RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}/static
)
set(CPACK_PACKAGE_DESCRIPTION "test")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "test")
set(CPACK_PACKAGE_VENDOR "xxx")
set(CPACK_PACKAGE_CONTACT "xxx")
set(CPACK_PACKAGE_VERSION_MAJOR "${PCK_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${PCK_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${PCK_REVISION}")
set(CPACK_PACKAGE_FILE_NAME "${PrjName}_${PCK_MAJOR}.${PCK_MINOR}.${PCK_REVISION}")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PrjName}_${PCK_MAJOR}.${PCK_MINOR}.${PCK_REVISION}")
include(CPack)
endif()
The cmake/STM32F2xx.cmake contains:
set(CMAKE_C_FLAGS_INIT "--cpu Cortex-M3")
set(CMAKE_CXX_FLAGS_INIT "--cpu Cortex-M3")
set(CMAKE_C_LINK_FLAGS_INIT "--zt")
I created an toolchain file for the arm compiler:
# generated cmake toolchain file for
include (CMakeForceCompiler)
# We are cross compiling so we don't want compiler tests to run, as they will fail
set(CMAKE_SYSTEM_NAME Generic)
# Set processor type
set(CMAKE_SYSTEM_PROCESSOR arm)
SET(CMAKE_CROSSCOMPILING 1)
set(CMAKE_ASM_COMPILER "arm-none-eabi-as.exe")
set(CMAKE_C_COMPILER "arm-none-eabi-gcc.exe")
set(CMAKE_CXX_COMPILER "arm-none-eabi-g++.exe")
set(CMAKE_LINKER "arm-none-eabi-gcc.exe")
set(CMAKE_MAKE_PROGRAM "mingw32-make.exe")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --specs=nosys.specs")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --specs=nosys.specs")
When compiling on cmd with:
cd D:\git\myTestBuild\build
PATH=C:\Program Files\CMake\bin;C:\Program Files (x86)\GNU Tools ARM Embedded\6 2017-q1-update\bin;C:\Program Files\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev2\mingw64\bin;%PATH%
"C:\Program Files\CMake\bin\cmake.exe" -DArch="STM32F2xx" -G"MinGW Makefiles" -DPackageFormat=ZIP -DSWstackPath="D:\git\myTestBuild\SWstack" -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE="D:\git\myTestBuild\dump\toolchain.cmake" ..
CMake Error at C:/Program Files/CMake/share/cmake-3.8/Modules/CMakeTestCCompiler.cmake:51 (message):
The C compiler "C:/Program Files (x86)/GNU Tools ARM Embedded/6
2017-q1-update/bin/arm-none-eabi-gcc.exe" is not able to compile a simple
test program.
It fails with the following output:
Change Dir: D:/git/myTestBuild/build/CMakeFiles/CMakeTmp
Run Build Command:"C:/Program
Files/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64/bin/mingw32-make.exe"
"cmTC_8c052/fast"
C:/Program
Files/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64/bin/mingw32-make.exe
-f CMakeFiles\cmTC_8c052.dir\build.make CMakeFiles/cmTC_8c052.dir/build
mingw32-make.exe[1]: Entering directory
'D:/git/myTestBuild/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_8c052.dir/testCCompiler.c.obj
"C:\Program Files (x86)\GNU Tools ARM Embedded\6
2017-q1-update\bin\arm-none-eabi-gcc.exe" -o
CMakeFiles\cmTC_8c052.dir\testCCompiler.c.obj -c
D:\git\myTestBuild\build\CMakeFiles\CMakeTmp\testCCompiler.c
Linking C executable cmTC_8c052
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_link_script
CMakeFiles\cmTC_8c052.dir\link.txt --verbose=1
"C:\Program Files (x86)\GNU Tools ARM Embedded\6
2017-q1-update\bin\arm-none-eabi-gcc.exe"
CMakeFiles/cmTC_8c052.dir/testCCompiler.c.obj -o cmTC_8c052
c:/program files (x86)/gnu tools arm embedded/6
2017-q1-update/bin/../lib/gcc/arm-none-eabi/6.3.1/../../../../arm-none-eabi/lib\libc.a(lib_a-exit.o):
In function `exit':
exit.c:(.text.exit+0x2c): undefined reference to `_exit'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\cmTC_8c052.dir\build.make:96: recipe for target 'cmTC_8c052'
failed
mingw32-make.exe[1]: *** [cmTC_8c052] Error 1
mingw32-make.exe[1]: Leaving directory
'D:/git/myTestBuild/build/CMakeFiles/CMakeTmp'
Makefile:125: recipe for target 'cmTC_8c052/fast' failed
mingw32-make.exe: *** [cmTC_8c052/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:5 (project)
It fails. The compiler can't link the simple demo program because it does not find the "_exit" symbol. This problem should be overcome by adding "--specs=nosys.specs". Seems that it does not work.
Any ideas?
Turning my comment into an answer
I've given a reduced version of your an example a try. I could reproduce your initial error and could fix the compiler detection by setting CMAKE_TRY_COMPILE_TARGET_TYPE to STATIC_LIBRARY. But then building an "executable" would still fail.
So you can set CMAKE_EXE_LINKER_FLAGS_INIT to --specs=nosys.specs (since those are linker flags). Just make sure to start from an empty binary output directory if you are using ..._INIT flags, since the resulting CMAKE_EXE_LINKER_FLAGS is cached.
I prefer to force the result CMAKE_EXE_LINKER_FLAGS variable (INTERNAL does imply FORCE).
CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(testApp)
file(WRITE main.cpp "int main(void) { return 0; }")
set(testApp "testApp")
list(
APPEND testApp_sources
main.cpp
)
add_executable(
${testApp}
${testApp_sources}
)
toolchain.cmake
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER "arm-none-eabi-gcc.exe")
set(CMAKE_CXX_COMPILER "arm-none-eabi-g++.exe")
set(CMAKE_EXE_LINKER_FLAGS "--specs=nosys.specs" CACHE INTERNAL "")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
Does:
build> cmake -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE:PATH="..\toolchain.cmake" ..
-- The C compiler identification is GNU 6.3.1
-- The CXX compiler identification is GNU 6.3.1
-- Check for working C compiler: C:/Program Files (x86)/GNU Tools ARM Embedded/6 2017-q1-update/bin/arm-none-eabi-gcc.exe
-- Check for working C compiler: C:/Program Files (x86)/GNU Tools ARM Embedded/6 2017-q1-update/bin/arm-none-eabi-gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/GNU Tools ARM Embedded/6 2017-q1-update/bin/arm-none-eabi-g++.exe
-- Check for working CXX compiler: C:/Program Files (x86)/GNU Tools ARM Embedded/6 2017-q1-update/bin/arm-none-eabi-g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: build
build> cmake --build .
Scanning dependencies of target testApp
[ 50%] Building CXX object CMakeFiles/testApp.dir/main.obj
[100%] Linking CXX executable testApp
[100%] Built target testApp
References
cmake cross-compile with specific linker doesn't pass arguments to armlink
What's the CMake syntax to set and use variables?