How to Write CMakeLists.txt Files?

Click on the “Computer Vision Life” above to select “Star”

Quickly get the latest content

This article is adapted from 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 Requirements

cmake_minimum_required( VERSION 2.8 )

# Project file name 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 at the front:

set(CMAKE_BUILD_TYPE debug)

This indicates setting it to debug mode compilation.

1.3 CheckC++ Version

Check C++ version

# Check C++11 or C++0x supportinclude(CheckCXXCompilerFlag)CHECK_CXX_COMPILER_FLAG(“-std=c++11COMPILER_SUPPORTS_CXX11)CHECK_CXX_COMPILER_FLAG(“-std=c++0xCOMPILER_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, start configuring various library modules.

2.1 If Only Including OpenCV Libraries

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 include directory for OpenCV here is the path containing OpenCVConfig.cmake.

As shown in the figure below:

How to Write CMakeLists.txt Files?

How to Write CMakeLists.txt Files?

2.2 If Including Third-Party Libraries in Source Form (.h and .cpp), Not Dynamic Libraries

How to Write CMakeLists.txt Files?

Here we take adding the bag-of-words library DBoW2 as an example, where DBoW2 is included in source form, meaning it has not been compiled into a .so file.

How to Write CMakeLists.txt Files?

2.2.1 Including Header Files of Third-Party Libraries

# 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/?

How to Write CMakeLists.txt Files?

How to Write CMakeLists.txt Files?

To prevent not finding in the include layer, continue to search in the next layer path include/DBoW2/ to avoid header file lookup errors caused by inconsistent include paths for certain files.

2.2.2 Including cpp Files of Third-Party Libraries

How to Write CMakeLists.txt Files?

How to Write CMakeLists.txt Files?

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 directly.

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 Third-Party Libraries in Library Form (Here Taking Static Libraries as an Example(.a Suffix)

In the above project, it also includes 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})

By the above method, you can call.

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++11COMPILER_SUPPORTS_CXX11)CHECK_CXX_COMPILER_FLAG(“-std=c++0xCOMPILER_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 dependencyON) 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 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

The above is just a simple overview of the usage of CMakeLists.txt. If there is anything lacking, please criticize and correct. There are many insights to be explored in managing large projects, and due to space limitations, I will not display them here. You are welcome to join our learning circle “3D Vision Technology” for further discussion, communication, and learning.

Learn core technologies of 3D vision SLAM from scratch, scan to view the introduction, unconditional refund within 3 days

How to Write CMakeLists.txt Files?Learning is an advantage, avoid doing it alone, there are tutorial materials, exercises, Q&A, etc., a quality learning circle helps you take fewer detours and quickly get started!

Group Chat

Welcome to join the reader group of the public account to communicate with peers. Currently, there are WeChat groups for SLAM, Algorithm Competition, Detection, Segmentation, Recognition, 3D Vision, Medical Imaging, Autonomous Driving, Computational Photography etc. (will gradually be subdivided), please scan the WeChat ID below to join the group, and note: “nickname + school/company + research direction”, for example: “Zhang San + Shanghai Jiao Tong University + Visual SLAM”. Please follow the format for remarks, otherwise you will not be approved. After successful addition, you will be invited to relevant WeChat groups according to your research direction. Please do not send advertisements in the group, otherwise, you will be removed from the group. Thank you for your understanding~

How to Write CMakeLists.txt Files?

Submissions are also welcome to contact:[email protected]

How to Write CMakeLists.txt Files?

Long press to follow Computer Vision Life

Recommended Reading

Offline communication meeting (SLAM introductory video)

Start learning SLAM from scratch | Why learn SLAM?

Start learning SLAM from scratch | What do you need to learn for SLAM?

Start learning SLAM from scratch | What is the use of SLAM?

Start learning SLAM from scratch | Should I learn new features of C++?

Start learning SLAM from scratch | Why use homogeneous coordinates?

Start learning SLAM from scratch | Rotation of rigid bodies in 3D space

Start learning SLAM from scratch | Why do we need Lie groups and Lie algebras?

Start learning SLAM from scratch | Camera imaging model

Start learning SLAM from scratch | How to truly understand the epipolar constraint without pushing formulas?

Start learning SLAM from scratch | The magic of the homography matrix

Start learning SLAM from scratch | Hello, point cloud

Start learning SLAM from scratch | Adding a filter to the point cloud

Start learning SLAM from scratch | Point cloud smoothing normal estimation

Start learning SLAM from scratch | Evolution from point cloud to mesh

Start learning SLAM from scratch | Understanding graph optimization, step by step to understand g2o code

Start learning SLAM from scratch | Mastering g2o vertex programming routines

Start learning SLAM from scratch | Mastering g2o edge coding routines

Start learning SLAM from scratch | ICP principles and applications

Start learning SLAM from scratch | Aligning IMU and image frames using quaternion interpolation

Visualize and understand quaternions, may you no longer lose hair

Overview of Visual SLAM Technology

Research SLAM, how high are the programming requirements?

What RGB-D SLAM is currently open source?

Detailed explanation | SLAM loop detection problem

Summary | Comprehensive list of datasets related to SLAM, reconstruction, and semantics

Exhaustively organized | Which excellent companies are there in the SLAM direction in China?

Strongest team | Summary of top global laboratories in 3D vision and SLAM directions

Which big V can be followed on WeChat public accounts, Zhihu, and blogs in the SLAM direction?

Summary | The most comprehensive SLAM open-source datasets

Overview | SLAM loop detection methods

Summary of dry goods | Common interview questions and reference answers for SLAM
2019 Latest SLAM, positioning, and mapping job sharing, feeling like a rookie after watching!
2019 Summer Computer Vision Internship Application Summary

2018 SLAM, 3D vision job experience sharing

Experience sharing | SLAM, 3D vision written test and interview questions

Latest AI dry goods, I am watching How to Write CMakeLists.txt Files?

Leave a Comment