CMAKE learning notes

CMAKE official documents: https://cmake.org/cmake/help/latest/genindex.html#

CMAKE common instructions

CMake minimum version requirements

CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )

entry name

PROJECT(  entry name  )  

The defined project name is stored in the variable ${PROJECT_NAME}.
Contains cmakelists The directory path of the folder where TXT is located is stored in the variable ${PROJECT_SOURCE_DIR}.
Use ${} to reference variables

Set compilation mode

SET( CMAKE_BUILD_TRPE Release )

There are several compilation methods:
● Debug: Debug mode, output debugging information without optimization
● Release: Release mode, no debugging information, fully optimized
● relewithdeblnfo: similar to Release, but including debugging information
● minsizerelease: a special Release mode that optimizes the size of the library

Setting flags for compiling CXX

SET ( CMAKE_CXX_FLAGS " -Wall  " )

Configurations that need to be optimized when compiling CXX, such as:
● - std=c++11: support C++11
● - Wall: compilation process output warning
● - O3: optimize and improve code speed
Statement overrides cmakelists Txt. If you want to append the configuration, you need to append the previous variable ${CMAKE_CXX_FLAGS} in double quotation marks:

SET ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall" )

Search external library

//Find system OpenCV3 or above

FIND_PACKAGE( External library version EXACT/QUIET/REQUIRED )
FIND_PACKAGE( OpenCV 3 REQUIRED )

Parameter meaning:
● act: the version number must match exactly.
● QUIET: disable the warning message when it is not found.
● REQUIRED: the version is at least above this. If the package is not found, CMake terminates and outputs a warning message.
After the package is found, the following variables will be initialized automatically:
● ${< name > _found}: displays the tag of whether the library is found.
● ${< name > _include_dirs} or ${< name > _includes}: the path of the header file (the include of the corresponding library).
● ${< name > _libs} or ${< name > _libraries}: library files
FIND_PACKAGE has two search modes:

  1. Module mode: search findxxx under the path specified by ${CMAKE_MODULE_PATH} Cmake file, execute the change file to find the XXX library. Among them, the specific operations of finding the library and assigning values to ${XXX_INCLUDE_DIRS} and ${XXX_LIBRARIES} are carried out by findxxx Cmake complete.
  2. Config mode: search xxxconfig under the path specified by ${XXX_DIR} Cmake file to find XXX library. The specific operations of finding the library and assigning values to ${XXX_INCLUDE_DIRS} and ${XXX_LIBRARIES} are performed by xxxconfig Cmake complete. If xxxconfig is not found under ${XXX_DIR} Cmake file, you will find whether it exists in / usr/local/lib/cmake/XXX /. If the library is not installed in the system directory during installation, xxxconfig cannot be found automatically Cmake, you need to add the search path manually:
SET( XXX_DIR /home/nvidia/opencv/build )

● note: the two modes adopt Module mode by default. If the library is not found, the Config mode will be adopted.

Add source file subdirectory

ADD_SUBDIRECTORY( Subdirectory )

A directory can be either a relative path or an absolute path.
After that, ${CMAKE_SOURCE_DIR} is the absolute path where the source file is located.

? Find source file

AUX_SOURCE_DIRECTORY( Directory file name )

Generate executable

ADD_EXECUTABLE( Executable name source file )

After {execute path, $} executable path.

Generate link library file

ADD_LIBRARY( Library file name STATIC/SHARED  source file )

STATIC stands for STATIC link library and SHARED stands for SHARED link library.
After that, ${LIBRARY_OUTPUT_PATH} is the output path of the library file.

Link libraries for executables

TARGET_LINK_LIBRARIES( Executable library )

Specifies the header file search path

INCLUDE_DIRECTORIES( /usr/local/include )

When the. cpp file refers to the header file, search under the set directory.

SET definition variable

SET( Variable value )
SET( SRC_LST main.cpp other.cpp )

Replace the value with a variable. The above statement means to use the variable SRC_LST replaces the last two CPPs

LIST operation

LIST( parameter <list> content )

Common parameters are:
● APPEND: for additional contents
● FILTER
● GET
● LENGTH
● JOIN
● SUBLIST
● FIND
● INSERT

Judgment statement

IF
ELSEIF
ENDIF

Same as C language.

Circular statement

FOREACH( <loop_var> <items> )
FOREACH( i 0 1 2 3 )
    message(STATUS "current is ${i} " )
ENDFOREACH(i)

Precautions for use

● there are multiple directories in the project, and one cmakelists can be placed in each directory Txt, or just one cmakelists txt.
● instructions are not case sensitive

example

An image de distortion function with the following structure:
● include/base.h(opencv required)
● src/undistort.cpp(base.h opencv required)
● test/main.cpp
The function in undistort is called in main, and undistort needs to link opencv.
CMakeLists.txt as follows:

# CMake minimum version number requirements
cmake_minimum_required( VERSION 2.8 )

# entry name
project( image_undistort )

# Set compilation mode
set( CMAKE_BUILD_TYPE Release )

#Support C++11
set( CMAKE_CXX_FLAGS "-std=c++11" )

# Find OpenCV in the system
find_package( OpenCV 3 REQUIRED )

# Add project include path
include_directories( 
	${PROJECT_SOURCE_DIR}/include/
	${OpenCV_INCLUDE_DIRS} 
	)

message("PROJECT_SOURCE_DIR = " ${PROJECT_SOURCE_DIR})
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH  ${CMAKE_BINARY_DIR}/lib)
message("CMAKE_BINARY_DIR = " ${CMAKE_BINARY_DIR})
message("CMAKE_SOURCE_DIR = " ${CMAKE_SOURCE_DIR})

# Generates a link library file from the specified source file. STATIC is a STATIC link library and SHARED is a SHARED link library
add_library(lib_undistort ${PROJECT_SOURCE_DIR}/source/undistort.cpp)

# Add library links to libraries or executables
target_link_libraries( lib_undistort
	${OpenCV_LIBS}
	)

# Use the specified source file to add an executable file for the project with the name of undo_ image
add_executable( ${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/testBed/main.cpp )

# Add library links to libraries or executables
target_link_libraries( ${PROJECT_NAME}
	${OpenCV_LIBS}
	lib_undistort 
	)

First, the first part, cmake version, project name, compilation method, CXX_FLAG
The second part is to find the third-party library, set the include path, and set the bin or lib file output path
The third part is to generate library, link to library, generate executable file and link to executable file

Added by Donovan on Mon, 07 Feb 2022 10:43:52 +0200