Click on the “3D Vision Workshop” above and select “Star”
Get valuable content delivered promptly
The 62nd Article of 3D Vision Workshop
This article summarizes the method of writing CMakeLists.txt files on the linux platform.
1 General Modules at the Beginning
1.1 CMake Version Requirement
cmake_minimum_required( VERSION 2.8 )
# Project filename loop_closure, can be any name
project( loop_closure )
1.2 Build Mode
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
ENDIF()
MESSAGE(“Build type: ” ${CMAKE_BUILD_TYPE})
Of course, if you add the statement before this:
set(CMAKE_BUILD_TYPE debug)
This indicates that it is set to debug mode compilation.
1.3 Check C++ Version
Check the version of C++.
# Check C++11 or C++0x supportinclude(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()
2 Project File Configuration Module
Next, we start configuring each library module.
2.1 If only including the OpenCV library
set(OpenCV_DIR “/usr/local/include/opencv3.2.0/share/OpenCV“) find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
# Generate executable file
add_executable(${PROJECT_NAME} src/loop_closure.cpp )
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
Note: The OpenCV include directory here is the path containing OpenCVConfig.cmake.
As shown in the figure below:
2.2 If including a third-party library, which is in source form (.h and .cpp), not a dynamic library
This example adds the vocabulary library DBoW2, where DBoW2 is included in source form, meaning it has not been compiled into a .so file.
2.2.1 Include header files of the third library
# Set the path corresponding to the .h file
set( DBoW2_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/ThirdParty/DBow-master/include/)
#Include .h file paths
include_directories( ${OpenCV_INCLUDE_DIRS} ${DBoW2_INCLUDE_DIRS} ${DBoW2_INCLUDE_DIRS}/DBoW2/)
Note: Why use two paths ${DBoW2_INCLUDE_DIRS} and ${DBoW2_INCLUDE_DIRS}/DBoW2/?
To prevent not finding in the include layer, continue to check in the next layer path include/DBoW2/ to ensure that the header file search does not fail due to inconsistent include paths.
2.2.2 Include cpp files of the third-party library
set(DBoW2_SRCS “${PROJECT_SOURCE_DIR}/ThirdParty/DBow-master/src“)
#Generate executable file
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 )
Among them, src/loop_closure.cpp src/run_main.cpp are implemented by myself in this project, while for other cpp files, just add the path.
Another way is to add the cpp file path more simply and conveniently.
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})
2.3 If including a third-party library in library form (here taking static library as an example(.a suffix))
In the above project, there is also the DBoW3 library, which has been compiled and installed in the computer memory by default. We can add the DBoW3 library as follows.
1)Include header files
set( DBoW3_INCLUDE_DIRS “/usr/local/include“)
2) Add library
set( DBoW3_LIBS “/usr/local/lib/libDBoW3.a“)
target_link_libraries(${PROJECT_NAME}${OpenCV_LIBS}${DBoW3_LIBS})
Through the above method, it can be called.
3 A Simple CMakeLists.txt File Demo
This demo includes the DLib library in addition to the libraries summarized above.
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 supportinclude(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(${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 directoryset( 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})
4 Conclusion
This is just a simple usage summary of CMakeLists.txt. If there are any shortcomings, please criticize and correct. There are still many insights to explore for managing large projects, which will not be displayed here due to space limitations. Welcome to join our learning circle “3D Vision Technology” for further discussion, exchange, and learning.
Learn core technologies of 3D vision, scan to check the introduction, unconditional refund within 3 days
There are high-quality tutorial materials in the circle, and you can ask questions and get answers, helping you solve problems efficiently