转载请注明出处。
Cmake介绍
Cmake 允许开发者编写一种平台无关的 CMakeList.txt 文件来定制整个编译流程,然后再根据目标用户的平台进一步生成所需的本地化 Makefile 和工程文件,如 Unix 的 Makefile 或 Windows 的 Visual Studio 工程。从而做到“Write once, run everywhere”。
使用流程
在 linux 平台下使用 CMake 生成 Makefile 并编译的流程如下:
- 编写 CMake 配置文件 CMakeLists.txt 。
- 执行命令 cmake PATH 或者 ccmake PATH 生成 Makefile 。其中, PATH 是 CMakeLists.txt 所在的目录。
- 使用 make 命令进行编译。
编写CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(text1)
set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#Ceres绝对路径
set(Ceres_DIR /home/wang/ceres-solver/include/Ceres)
#找ceres库并且添加头文件
find_package(Ceres REQUIRED)
include_directories(${CERES_INCLUDE_DIRS})
#找opencv库并且添加头文件
find_package( OpenCV 3.4.1 REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
#Eigen库只需要添加头文件
include_directories("/usr/include/eigen3")
#add_executable要在target_link_libraries
add_executable(text01 ./src/main.cpp)
#target_link_libraries中只需要添加ceres和opencv
target_link_libraries(text01 ${OpenCV_LIBS} ${CERES_LIBRARIES})
工程文件关系
├── build
├── bin
└── text01
├──src
└── main.c
└── CMakeLists.txt
编译过程
1.cd进入build文件夹下进行 cmake..
2.make
3.进入bin文件夹下进行 ./text01
运行结果
iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time
0 1.250000e+01 0.00e+00 5.00e+00 0.00e+00 0.00e+00 1.00e+04 0 5.79e-05 1.96e-04
1 1.249750e-07 1.25e+01 5.00e-04 5.00e+00 1.00e+00 3.00e+04 1 1.61e-04 4.63e-04
2 1.388518e-16 1.25e-07 1.67e-08 5.00e-04 1.00e+00 9.00e+04 1 3.19e-05 5.25e-04
Ceres Solver Report: Iterations: 3, Initial cost: 1.250000e+01, Final cost: 1.388518e-16, Termination: CONVERGENCE
x : 5 -> 10
参考程序
程序参考Ceres Solver官网中Tutorial中的Hello World
http://www.ceres-solver.org/tutorial.html
转载请注明出处。