CMake use and CMakeLists.txt to write notes

Learn the writing of CMakeLists.txt through examples

# Specify the minimum CMake version
required cmake_minimum_required(VERSION 3.12)
# Specify the project name
project(demo)

# Set compilation options
# CMAKE_C_FLAGS is the gcc compiler option
# CMAKE_CXX_FLAGS is the g++ compiler option
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS “-std=c++11 ${CMAKE_CXX_FLAGS}”)

# Set the compilation mode
SET (CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")

SET(CMAKE_CXX_FLAGS_RELEASE “$ENV{CXXFLAGS} -O3 -Wall”)
# Set Debug mode for debugging
# CMAKE_BUILD_TYPE is cmake compilation mode variable
set(CMAKE_BUILD_TYPE Debug)
# Set Release mode
set(CMAKE_BUILD_TYPE Release)

# Quoting sub cmake files, generally used to define your own cmake commands and find dependent libraries
include(${CMAKE_SOURCE_DIR}/cmake/Utils.cmake)

# find_package is used to find dependent third-party libraries
# Here we take opencv as an example
if (UNIX)
set(OpenCV_DIR “/usr/local/include/opencv3.2.0/share/OpenCV”)
endif()
find_package( OpenCV REQUIRED)

Define the compilation control variable
if (OpenCV_FOUND)
message(STATUS “Found OpenCV: ${OpenCV_VERSION}”)
defines an OPENCV control variable.
One of the usages is to control the handling of the two cases where opencv is found and opencv not found in the code.

ifdef OPNECV
/* code */
endif

For more usage of add_definitions, please check cmake official document
add_definitions(-DOPENCV)
endif()

# set for custom variables
# Here is to list the source file names for the convenience of later use
set(SRCS
example01.cpp
example02.cpp
)
# set add definitions by appending
set(SRCS ${SRCS} example03.cpp)

# list is used to append or delete the value of a variable
list(APPEND SRC_LIST test.cpp)
list(REMOVE_ITEM SRC_LIST main.cpp)

# include_directories Specify the include_directories search directory for header files
(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
${OpenCV_INCLUDE_DIRS}
)

# link_directories Specify the link library search directory
link_directories(
${CMAKE_CURRENT_SOURCE_DIR}/libs
)

# Use if… else… to configure different platforms
# Here is to specify the executable file name according to different platforms
if (UNIX OR APPLE)
set(EXE_NAME “${SOURCE_NAME}.bin”)
elseif (WIN32)
set(EXE_NAME “demo” )
endif()

# message print information
message("exe name: "${EXE_NAME})

# FATAL_ERROR: CMake error, stop running and
build # SEND_ERROR: CMake error, continue to run but will not generate results
# WARNING: CMake warning, continue to run
# AUTHOR_WARNING: CMake warning (dev), continue to run (ps: I don’t understand dev meaning)

# DEPRECATION: If the variable CMAKE_ERROR_DEPRECATED or
# CMAKE_WARN_DEPRECATED is enabled, an error or warning will be reported

# STATUS: 给我们看的 Main prompt information
# There are a few more links that are too long... message document
message(STATUS "message print message: "${EXE_NAME})

# add_executable is used to generate executable files
add_executable(${EXE_NAME} ${SRCS})

** Specify header file linking rules. Compared with include_directories, cmake officially recommends target_include_directories**
** Convenient modular compilation**
target_include_directories(${LIB_NAME}
PUBLIC
${CMAKE_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/ src
${CMAKE_SOURCE_DIR}/3rd/arcsoft/include
${CMAKE_SOURCE_DIR}/3rd/libuv/include
${OpenCV_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)

# target_link_libraries is used to specify the link library
# Here I link myself with the pthread system library and the opencv third-party library
target_link_libraries(${EXE_NAME} pthread ${OpenCV_LIBS})

cmake common parameters

CMAKE_INSTALL_PREFIX is used to specify the installation path of library files and header files, usually used :

cmake -DCMAKE_INSTALL_PREFIX=/usr/local .

Guess you like

Origin blog.csdn.net/VOlsenBerg/article/details/107001150