Cmake中使用opencv、Eigen、ceres库

转载请注明出处。

Cmake介绍

Cmake 允许开发者编写一种平台无关的 CMakeList.txt 文件来定制整个编译流程,然后再根据目标用户的平台进一步生成所需的本地化 Makefile 和工程文件,如 Unix 的 Makefile 或 Windows 的 Visual Studio 工程。从而做到“Write once, run everywhere”。

使用流程

在 linux 平台下使用 CMake 生成 Makefile 并编译的流程如下:

  1. 编写 CMake 配置文件 CMakeLists.txt 。
  2. 执行命令 cmake PATH 或者 ccmake PATH 生成 Makefile 。其中, PATH 是 CMakeLists.txt 所在的目录。
  3. 使用 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
在这里插入图片描述

转载请注明出处。

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值