1.cmake 简介
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake。
2.CMake 操作流程
CMake的所有的语句都写在一个叫:CMakeLists.txt的文件中。当CMakeLists.txt文件确定后,可以用ccmake命令对相关 的变量值进行配置。这个命令必须指向CMakeLists.txt所在的目录。配置完成之后,应用cmake命令生成相应的makefile(在Unix like系统下)或者 project文件(指定用window下的相应编程工具编译时)。
其基本操作流程为:
$ ccmake directory
$ cmake directory
$ make
其中directory为CMakeList.txt所在目录;
- 第一条语句用于配置编译选项,如VTK_DIR目录 ,一般这一步不需要配置,直接执行第二条语句即可,但当出现错误时,这里就需要认为配置了,这一步才真正派上用场;
- 第二条命令用于根据CMakeLists.txt生成Makefile文件;
- 第三条命令用于执行Makefile文件,编译程序,生成可执行文件;
CMake的执行就是这么简单,其难点在于如何编写CMakeLists.txt文件
3.如何编写CMakeLists.txt文件
3.1 开头通用模块
- make版本要求
cmake_minimum_required( VERSION 2.8 )
- 工程文件名-可任取
project(project)
- 编译模式
# 设置为 Release 模式
SET(CMAKE_BUILD_TYPE Release)
# 设置为 debug 模式
SET(CMAKE_BUILD_TYPE debug)
# 打印设置的编译模型信息
MESSAGE("Build type: " ${CMAKE_BUILD_TYPE})
- 检查C++版本
# 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()
3.2 项目文件配置模块
如果仅包含OpenCV库时
- 备注:这里的OpenCV包含目录为含有OpenCVConfig.cmake的路径。
set(OpenCV_DIR "/usr/local/include/opencv3.2.0/share/OpenCV")
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
- 生成可执行文件
ADD_EXECUTABLE( 目标文件(可执行文件) 依赖文件(.cpp))
add_executable(${PROJECT_NAME} src/loop_closure.cpp )
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
- 包含第三库的头文件
#设置.h文件对应的路径
set( DBoW2_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/ThirdParty/DBow-master/include/)
#包含.h文件路径
include_directories( ${OpenCV_INCLUDE_DIRS}
${DBoW2_INCLUDE_DIRS} ${DBoW2_INCLUDE_DIRS}/DBoW2/)
- 包含第三方库的cpp文件
set(DBoW2_SRCS "${PROJECT_SOURCE_DIR}/ThirdParty/DBow-master/src")
#生成可执行文件 -- 使用空格分割cpp文件路径
add_executable(${PROJECT_NAME} src/loop_closure.cpp src/run_main.cpp ${DBoW2_SRCS}/BowVector.cpp ${DBoW2_SRCS}/FBrief.cpp ${DBoW2_SRCS}/FeatureVector.cpp ${DBoW2_SRCS}/FORB.cpp ${DBoW2_SRCS}/FSurf64.cpp ${DBoW2_SRCS}/QueryResults.cpp ${DBoW2_SRCS}/ScoringObject.cpp)
3.3 一个简单的CMakeLists.txt文件demo
cmake_minimum_required( VERSION 2.8 )
project( loop_closure )
#set(CMAKE_BUILD_TYPE Debug)
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
ENDIF()
MESSAGE("Build type: " ${CMAKE_BUILD_TYPE})
# 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()
#opencv
#set(OpenCV_DIR "/usr/local/include/opencv3.2.0/share/OpenCV")
set(OpenCV_DIR "/opt/ros/kinetic/share/OpenCV-3.3.1-dev")
find_package(OpenCV REQUIRED)
set( DBoW3_INCLUDE_DIRS "/usr/local/include")
set( DBoW2_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/ThirdParty/DBow-master/include/)
message(${DBoW2_INCLUDE_DIRS})
#important
#file(GLOB DBoW2_SRCS ${PROJECT_SOURCE_DIR}/ThirdParty/DBow-master/src/*.cpp)
#message(${DBoW2_SRCS})
set(DBoW2_SRCS "${PROJECT_SOURCE_DIR}/ThirdParty/DBow-master/src")
message(${DBoW2_SRCS})
find_package(DLib QUIET
PATHS ${DEPENDENCY_INSTALL_DIR})
if(${DLib_FOUND})
message("DLib library fo NDENCY_DIR}
GIT_REPOSITORY http://github.com/dorian3d/DLib
GIT_TAG master
INSTALL_DIR ${DEPENDENCY_INSTALL_DIR}
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>)
add_custom_target(Dependencies ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR} DEPENDS DLib)
else()
message(SEND_ERROR "Please, activate DOWNLOAD_DLib_dependency option or download manually")
endif(${DOWNLOAD_DLib_dependency})
endif(${DLib_FOUND})
include_directories( ${OpenCV_INCLUDE_DIRS}
${DBoW3_INCLUDE_DIRS} ${DBoW2_INCLUDE_DIRS} ${DBoW2_INCLUDE_DIRS}/DBoW2/)
message("DBoW3_INCLUDE_DIRS ${DBoW3_INCLUDE_DIRS}")
message("DBoW2_INCLUDE_DIRS ${DBoW2_INCLUDE_DIRS}")
message("opencv ${OpenCV_VERSION}")
# dbow3
# dbow3 is a simple lib so I assume you installed it in default directory
set( DBoW3_LIBS "/usr/local/lib/libDBoW3.a")
add_executable(${PROJECT_NAME} src/loop_closure.cpp src/run_main.cpp
${DBoW2_SRCS}/BowVector.cpp ${DBoW2_SRCS}/FBrief.cpp ${DBoW2_SRCS}/FeatureVector.cpp
${DBoW2_SRCS}/FORB.cpp ${DBoW2_SRCS}/FSurf64.cpp ${DBoW2_SRCS}/QueryResults.cpp ${DBoW2_SRCS}/ScoringObject.cpp
)
message(${DBoW2_SRCS}/BowVector.cpp)
target_link_libraries(${PROJECT_NAME}
${OpenCV_LIBS}
${DLib_LIBS}
${DBoW3_LIBS}
)
sdsasddsasda
回复测试