文章目录
开头部分-指定输出路径
中间部分-指定工程依赖
结尾部分-指定输出文件 CMakeLists.txt链接库文件的方式
区分link_directories, link_libraries , target_link_libraries
CMakeLists.txt文件链接有两种方式
转自:https://blog.csdn.net/CSDNhuaong/article/details/80895679
注:以下尽量以slam14/project/0.4的CMakeLists.txt书写为例
开头部分-指定输出路径
- 指定 cmake版本
- 指定 project name
- 指定编译模式和标准
- 指定生成文件的输出目录
cmake_minimum_required( VERSION 2.8 )
project ( myslam )
set( CMAKE_CXX_COMPILER "g++" )
set( CMAKE_BUILD_TYPE "Release" )
set( CMAKE_CXX_FLAGS "-std=c++11 -march=native -O3" )
list( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules )
set( EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin )
set( LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib )
这样Cmake生成的的输出路径是 在该项目文件夹下有bin ,lib,cmake_modules,三个子文件夹,分别存放可执行文件,库文件和一些引用库的cmake文件
中间部分-指定工程依赖
- 指定工程需要用到的一些系统包和库
- 指定依赖的库文件
# Eigen
include_directories( "/usr/include/eigen3" )
# OpenCV
find_package( OpenCV 3.1 REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
# Sophus
find_package( Sophus REQUIRED )
include_directories( ${Sophus_INCLUDE_DIRS} )
# G2O
find_package( G2O REQUIRED )
include_directories( ${G2O_INCLUDE_DIRS} )
set( THIRD_PARTY_LIBS
${OpenCV_LIBS}
${Sophus_LIBRARIES}
g2o_core g2o_stuff g2o_types_sba
)
include_directories( ${PROJECT_SOURCE_DIR}/include )
CMakeLists.txt链接库文件的方式
1. include_directories(添加头文件目录)
INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}/include )
2. link_directories(添加需要链接的库文件目录)
LINK_DIRECTORIES( ${PROJECT_SOURCE_DIR}/lib)
3. target_link_libraries(设置要链接的库文件的名称)
# 与G2O和OpenCV链接
target_link_libraries( g2o_curve_fitting
${OpenCV_LIBS}
g2o_core g2o_stuff
)
结尾部分-指定输出文件
## slam14/project/0.4中
add_subdirectory( src )
add_subdirectory( test )
## slam14/ch6中
add_executable( g2o_curve_fitting main.cpp )
# 与G2O和OpenCV链接
target_link_libraries( g2o_curve_fitting
${OpenCV_LIBS}
g2o_core g2o_stuff
)
## rgbd_slam/part6中
ADD_SUBDIRECTORY( ${PROJECT_SOURCE_DIR}/src )