cmake study notes

  • 2020-05-26 09:44:53
  • OfStack

Recently I came into contact with some engineering code, which was compiled with cmake. Every time I looked at CMakeLists.txt, I was confused and decided to learn from 0

1 set


set(var hello)
message(${var})

The output

hello

It's not just the output of hello, it's a lot of other information, it generates a lot of files

files

2 CMAKE_C(XX)_FLAGS

The CMAKE_C_FLAGS variable is passed to the C compiler for all compiled configurations. If you want to be valid for only one particular configuration, you can set CMAKE_C_FLAGS_ < Compile the configuration > , such as CMAKE_C_FLAGS_RELEASE, CMAKE_C_FLAGS_DEBUG.
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -march=native -Wno-reorder")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -march=native -Wno-reorder")

The compile option is -Wall O3 optimization

Other internal variables:

The & # 8226; CMAKE_C_COMPILER: specifies the C compiler
The & # 8226; CMAKE_CXX_COMPILER:
The & # 8226; CMAKE_C_FLAGS: options for compiling C files, such as -g; You can also add the compile option through add_definitions
The & # 8226; EXECUTABLE_OUTPUT_PATH: path to the executable file
The & # 8226; LIBRARY_OUTPUT_PATH: library file path
The & # 8226; CMAKE_BUILD_TYPE: : build type (Debug, Release,...) .
•CMAKE_BUILD_TYPE=Debug
The & # 8226; BUILD_SHARED_LIBS: Switch between shared and static libraries

Use of built-in variables:

The & # 8226; Specified in CMakeLists.txt, using set
The & # 8226; Used in the cmake command, such as cmake-DBUILD_SHARED_LIBS =OFF

3 CHECK_CXX_COMPILER_FLAG

Check that the CXX compiler supports a given flag
Must first include (CheckCXXCompilerFlag)
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG( < flag > < var > )
e.g.


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()

The code above is straightforward
You are checking that the current compiler supports c++11
CHECK_CXX_COMPILER_FLAG assigned var is of type bool

4 add_definitions

Add compilation parameters
add_definitions(-DDEBUG)

The DEBUG macro definition will be added to the gcc command line so that you can manipulate the DEBUG macro in your source file

5 ENV

Grammar:
$ENV(VAR)# reads the environment variable VAR, which can also be assigned by set
eg:
IF(DEFINED ENV{ARM_ARCHITECTURE})# if arm machine

6 list

List operation


list(LENGTH <list> <output variable>)
list(GET <list> <element index> [<element index> ...]
   <output variable>)
list(APPEND <list> [<element> ...])
list(FIND <list> <value> <output variable>)
list(INSERT <list> <element_index> <element> [<element> ...])
list(REMOVE_ITEM <list> <value> [<value> ...])
list(REMOVE_AT <list> <index> [<index> ...])
list(REMOVE_DUPLICATES <list>)
list(REVERSE <list>)
list(SORT <list>)

APPEND appends elements, you get the idea, these are all list operations

7 CMAKE_MODULE_PATH

cmake searches modules list, which is 1 list

8 find_package

This is a little bit more complicated, but let's go straight to the official documentation
And this

9 include_directories

Adding the search path to the header file is equivalent to specifying the -I parameter for gcc
include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...])

10 add_library

add_library( < name > [STATIC | SHARED | MODULE]
[EXCLUDE_FROM_ALL]
source1 [source2 ...])

Add the directory of the library

11 target_link_libraries

target_link_libraries( < target > [item1 [item2 [...]]]
[[debug|optimized|general] < item > ] ...)

The directive target_link_libraries() is used to specify the libraries that target needs to link to, and you can have different options.
e.g.


target_link_libraries(myapp
  debug -labc
  optimized -lxyz
  )

myapp links libabc.a at debug build, libxyz.a at release build, their directories are added by the add_library command

12 add_executable

Add executable (from source)
add_executable( < name > [WIN32] [MACOSX_BUNDLE]
[EXCLUDE_FROM_ALL]
source1 [source2 ...])

e.g.


add_executable(stereo_euroc Examples/Stereo/stereo_euroc.cc)
target_link_libraries(stereo_euroc ${PROJECT_NAME})

stereo_euroc is the executable file to be generated, the source is the following.cc file, links to the library behind


Related articles: