Author: Wu Qi
This one-minute tutorial mainly takes you through the relevant knowledge of CMakeLists. When we usually receive a SLAM source code, we often rely on the following commands:
mkdir build && cd build
cmake ..
make -j
Or:
catkin_make
All these compilation commands rely on a special file in the source code called CMakeLists. For beginners in SLAM, many students just blindly type a few commands, resulting in a bunch of errors without knowing why. This time, the Bubble Source Code Team will look at how to set up third-party libraries in CMakeLists, starting with the classic SLAM code ORB_SLAM2’s CMakeLists.
CMakeLists of ORB_SLAM
cmake_minimum_required(VERSION 2.8)
project(ORB_SLAM2)
IF(NOT CMAKE_BUILD_TYPE_)
SET(CMAKE_BUILD_TYPE RELEASE)
ENDIF
MESSAGE("Build type: " ${CMAKE_BUILD_TYPE})
...
find_package(Eigen3 3.1.0 REQUIRED)
find_package(Pangolin REQUIRED)
find_package(OpenCV 3.0 QUIET)
include_directories(
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/include
${EIGEN3_INCLUDE_DIR}
${Pangolin_INCLUDE_DIRS}
...
)
...
target_link_libraries(
${PROJECT_NAME}
${EIGEN3_LIBS}
${Pangolin_LIBRARIES}
${PROJECT_SOURCE_DIR}/Thirdparty/DBoW2/lib/libDBoW2.so
${PROJECT_SOURCE_DIR}/Thirdparty/g2o/lib/libg2o.so
)
From the CMakeLists in ORB_SLAM2, we can find that generally, linking third-party libraries using CMakeLists usually involves three commands:
// The QUIET parameter suppresses warning messages when the package is not found, REQUIRED indicates that if the package is not found, the cmake process stops
find_package(third_party_package_name minimum_required_version REQUIRED/QUIET)
// Find the package header files
include_directories(${package_INCLUDE_DIRS})
// Link the package's third-party libraries (whether to use ${package_LIBS} or ${package_LIBRARIES} depends on the specific package)
target_link_libraries(${package_LIBS} or ${package_LIBRARIES})
This completes the connection of a typical third-party library, but some students may have two versions of OpenCV installed on their computers. How can we specify which version to connect to in CMakeLists?
CMakeLists Advanced – Finding Packages
First, determine the installation locations of different versions of OpenCV on your computer. For example, mine is under /usr/local with different version numbers:
Here we can see two different versions of OpenCV under /usr/local, opencv2.4.13 and opencv3.4.8. Now let’s see how to modify our CMakeLists:
// Specify the search path for find_package
set(OpenCV_DIR /usr/local/opencv2.4.13/share/OpenCV)
find_package(OpenCV 2.4 REQUIRED)
// Tell the system where the header files are
include_directories(SYSTEM ${OpenCV_INCLUDE_DIRS} /usr/local/opencv2.4.13/include)
// Tell where the third-party library files are
link_directories(${OpenCV_LIBRARY_DIRS} /usr/local/opencv2.4.13/lib)
// Link the library files with the project
target_link_libraries(${project_name} ${OpenCV_LIBRARY_DIRS})
Here we have successfully specified the version number of OpenCV in the project, and we can happily proceed with development!~ This one-minute tutorial ends here~ This is the first push of Bubble Tips in a minute. If you have any good suggestions or want to learn about certain programming knowledge, feel free to leave a message in the comments section. We will continue to bring you more exciting content in the future!