点击上方“计算机视觉life”,选择“星标”
快速获得最新干货
本文转自3D视觉工坊。本文以linux平台下CMakeLists.txt文件书写方法总结。
cmake_minimum_required( VERSION 2.8 )
#工程文件名loop_closure,可任取
project( loop_closure )
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
ENDIF()
MESSAGE("Build type: " ${CMAKE_BUILD_TYPE})
当然如果此处在前面加上语句:
set(CMAKE_BUILD_TYPE debug)
即表示设置为debug模式编译。
检查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()
接下来,便开始配置各个库模块。
set(OpenCV_DIR "/usr/local/include/opencv3.2.0/share/OpenCV")
find_package(OpenCV REQUIRED)include_directories( ${OpenCV_INCLUDE_DIRS} )
#生成可执行文件
add_executable(${PROJECT_NAME} src/loop_closure.cpp )
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
备注:这里的OpenCV包含目录为含有OpenCVConfig.cmake的路径。
如下图:
此处以在工程中添加词袋库DBoW2为例,这里的DBoW2是以源码形式包含,也即未编译成.so文件。
#设置.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/)
注:上述为啥要用两条路径${DBoW2_INCLUDE_DIRS}和${DBoW2_INCLUDE_DIRS}/DBoW2/?
为了防止在include层找不到,继续往下一层路径include/DBoW2/路径下查找,这是为了防止某些文件的包含路径不一致导致头文件查找错误。
set(DBoW2_SRCS "${PROJECT_SOURCE_DIR}/ThirdParty/DBow-master/src")
#生成可执行文件
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)
其中,src/loop_closure.cpp src/run_main.cpp 为本工程中我自己实现的,而对于其他的cpp文件,则直接添加路径即可。
另一种方式,添加cpp文件路径更为简洁方便。
file(GLOB DBoW2_SRCS ${PROJECT_SOURCE_DIR}/ThirdParty/DBow-master/src/*.cpp)
add_executable(${PROJECT_NAME} src/loop_closure.cpp src/run_main.cpp ${DBoW2_SRCS})
在上述工程下,同时还包含有DBoW3库,此库已经按默认路径编译安装到计算机内存中。我们可以按如下方式添加DBoW3库。
1)包含头文件
set( DBoW3_INCLUDE_DIRS "/usr/local/include")
2)添加库
set( DBoW3_LIBS "/usr/local/lib/libDBoW3.a")
target_link_libraries(${PROJECT_NAME}
${OpenCV_LIBS}
${DBoW3_LIBS}
)
通过上述方式,即可调用。
该demo中相较于上述总结添加的库,还额外包含了DLib库。
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 found, using it from the system")
include_directories(${DLib_INCLUDE_DIRS})
add_custom_target(Dependencies)
else(${DLib_FOUND})
message("DLib library not found in the system, it will be downloaded on build")
option(DOWNLOAD_DLib_dependency "Download DLib dependency" ON)
if(${DOWNLOAD_DLib_dependency})
ExternalProject_Add(DLib
PREFIX ${DEPENDENCY_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}
)
以上仅仅为CMakeLists.txt的一个简单用法梳理,如有不到之处,还请批评指正。对于大型工程的管理,还有很多学问可以挖掘,此处限于篇幅便不再展示,欢迎加入我们的学习圈「3D视觉技术」进一步探讨、交流、学习。
从零开始学习三维视觉核心技术SLAM,扫描查看介绍,3天内无条件退款
早就是优势,学习切忌单打独斗,这里有教程资料、练习作业、答疑解惑等,优质学习圈帮你少走弯路,快速入门!
欢迎加入公众号读者群一起和同行交流,目前有SLAM、算法竞赛、检测分割识别、三维视觉、医学影像、自动驾驶、计算摄影等微信群(以后会逐渐细分),请扫描下面微信号加群,备注:”昵称+学校/公司+研究方向“,例如:”张三 + 上海交大 + 视觉SLAM“。请按照格式备注,否则不予通过。添加成功后会根据研究方向邀请进入相关微信群。请勿在群内发送广告,否则会请出群,谢谢理解~
投稿也欢迎联系:simiter@126.com
长按关注计算机视觉life
从零开始一起学习SLAM | 不推公式,如何真正理解对极约束?
从零开始一起学习SLAM | 理解图优化,一步步带你看懂g2o代码
从零开始一起学习SLAM | 用四元数插值来对齐IMU和图像帧
最新AI干货,我在看