[CMake entry and advanced (6)] Some commonly used variables in CMakeLists.txt (with usage code)

        Variables are also an important part of cmake. cmake provides many built-in variables, and each variable has its own meaning. Through this link address cmake-variables(7) — CMake 3.5.2 Documentation, you can query all built-in variables and their The corresponding introduction is as follows:

         In this document, variables are classified into: variables that provide information, variables that change behavior, variables that describe the system, variables that control compilation, etc. The author also introduces some basic and commonly used variables according to this classification. variable.

informative variable

        As the name suggests, this kind of variable can provide some kind of information, so we usually only need to read the variable without modifying it:

variable illustrate
PROJECT_SOURCE_DIR The top-level directory of the project, that is, the directory where the top-level CMakeLists.txt source code is located
PROJECT_BINARY_DIR Project BINARY_DIR, which is the BINARY_DIR of the top-level CMakeLists.txt source code
CMAKE_SOURCE_DIR Equivalent to PROJECT_SOURCE_DIR
CMAKE_BINARY_DIR Equivalent to PROJECT_BINARY_DIR
CMAKE_CURRENT_SOURCE_DIR The path where the current source code is located
CMAKE_CURRENT_BINARY_DIR BINARY_DIR of the current source code
CMAKE_MAJOR_VERSION The major version number of cmake
CMAKE_MINOR_VERSION The minor version number of cmake
CMAKE_VERSION The version number of cmake (major+minor+revision)
PROJECT_VERSION_MAJOR The major version number of the project
PROJECT_VERSION_MINOR The minor version number of the project
PROJECT_VERSION project version number
CMAKE_PROJECT_NAME project name
PROJECT_NAME Project name, equivalent to CMAKE_PROJECT_NAME

        PROJECT_SOURCE_DIR 和 PROJECT_BINARY_DIR

        The PROJECT_SOURCE_DIR variable indicates the top-level directory of the project, which is the directory where the top-level CMakeLists.txt file is located; the PROJECT_BINARY_DIR variable indicates the BINARY_DIR of the project, which is the BINARY_DIR (output file directory) corresponding to the top-level CMakeLists.txt source code. For example, the project directory structure is as follows:

├── build
├── CMakeLists.txt
└── main.c

        The content of the CMakeLists.txt file is as follows:

# CMakeLists.txt
cmake_minimum_required("VERSION" "3.5")
project(HELLO)

message(${PROJECT_SOURCE_DIR})
message(${PROJECT_BINARY_DIR})

        In CMakeLists.txt, we printed the PROJECT_SOURCE_DIR and PROJECT_BINARY_DIR variables, entered the build directory, and executed cmake:

         From the printed information, we can see that PROJECT_SOURCE_DIR refers to the path where the top-level CMakeLists.txt source code of the project is located, and PROJECT_BINARY_DIR refers to the directory where we execute the cmake command, which is also the BINARY_DIR of the top-level CMakeLists.txt source code.

  • CMAKE_SOURCE_DIR 和 CMAKE_BINARY_DIR

        ditto

  • CMAKE_CURRENT_SOURCE_DIR 和 CMAKE_CURRENT_BINARY_DIR        

        It refers to the path of the current source code and the BINARY_DIR of the current source code. Let’s take a look through an example. For example, the project directory structure is as follows:

├── build
├── CMakeLists.txt
├── main.c
└── src
  └── CMakeLists.txt

        The top-level CMakeLists.txt file loads the CMakeLists.txt under the subdirectory src through add_subdirectory, and the content of the CMakeLists.txt file under the src directory is as follows:

# src 下的 CMakeLists.txt
message(${PROJECT_SOURCE_DIR})
message(${PROJECT_BINARY_DIR})
message(${CMAKE_CURRENT_SOURCE_DIR})
message(${CMAKE_CURRENT_BINARY_DIR})

        Print these variables through the message, compare them, enter the build directory, and execute cmake:

  • CMAKE_VERSION、CMAKE_MAJOR_VERSION 和 CMAKE_MINOR_VERSION

         Record the version number of cmake, as follows

# CMakeLists.txt
message(${CMAKE_VERSION})
message(${CMAKE_MAJOR_VERSION})
message(${CMAKE_MINOR_VERSION})

        The print information is as follows:

  •  PROJECT_VERSION、PROJECT_VERSION_MAJOR 和 PROJECT_VERSION_MINOR

        Record the version number of the project. In fact, you can set a version number for the project and set it through the project() command, as follows:

# CMakeLists.txt
cmake_minimum_required("VERSION" "3.5")
project(HELLO VERSION 1.1.0) #设置工程版本号为 1.1.0

# 打印
message(${PROJECT_VERSION})
message(${PROJECT_VERSION_MAJOR})
message(${PROJECT_VERSION_MINOR})

        The print information is as follows:

  • CMAKE_PROJECT_NAME 和 PROJECT_NAME

        Both are equivalent, recording the name of the project:

# CMakeLists.txt
cmake_minimum_required("VERSION" "3.5")
project(HELLO VERSION 1.1.0) #设置工程版本号为 1.1.0

# 打印工程名字
message(${CMAKE_PROJECT_NAME})
message(${PROJECT_NAME})

        The print information is as follows: 

 Variables that change behavior

         As the name implies, it means that these variables can change certain behaviors, so we can change the behavior by setting these variables.

variable illustrate
BUILD_SHARED_LIBS Control whether cmake generates dynamic libraries
CMAKE_BUILD_TYPE Specify the build type of the project, release or debug
CMAKE_SYSROOT corresponding to the compiler's --sysroot option
CMAKE_IGNORE_PATH Set a list of directories ignored by find_xxx commands
CMAKE_INCLUDE_PATH A list of directories specifying the search path for the find_file() and find_path() commands
CMAKE_INCLUDE_DIRECTORIES_BEFORE Used to control the behavior of the include_directories() command
CMAKE_LIBRARY_PATH A list of directories specifying the search path for the find_library() command
CMAKE_MODULE_PATH A list of directories specifying the search path for CMake modules to be loaded by the include() or find_package() commands
CMAKE_PROGRAM_PATH A list of directories specifying the search path for the find_program() command
  • BUILD_SHARED_LIB

        For the add_library() command, when the dynamic library is not explicitly specified (SHARED option), the static library is generated by default; in fact, we can control the behavior of the add_library() command through the BUILD_SHARED_LIBS variable, which means when the variable is set to on If the dynamic library is enabled, add_library() will generate a dynamic library file by default; when the variable is set to off or not set, add_library() will generate a static library file by default. The test is as follows:

        For example, the project directory structure is as follows:

├── build
├── CMakeLists.txt
├── hello
│ └── hello.c
└── world
  └── world.c

        The top-level CMakeLists.txt file looks like this:

# 顶层 CMakeLists.txt
cmake_minimum_required("VERSION" "3.5")
project(HELLO VERSION 1.1.0)

set(BUILD_SHARED_LIBS on)
add_library(hello hello/hello.c)
add_library(world world/world.c)

        Go to the build directory, execute cmake and make to build and compile, and the dynamic library files libhello.so and libworld.so will be generated.

  • CMAKE_BUILD_TYPE

        Set the compile type to Debug or Release. The debug version will generate relevant debugging information, which can be debugged with GDB; the release version will not generate debugging information:

# Debug 版本
set(CMAKE_BUILD_TYPE Debug)

# Release 版本
set(CMAKE_BUILD_TYPE Release)
  • CMAKE_SYSROOT

        cmake will pass this variable to the compiler --sysroot option, which is usually used when setting up cross compilation.

  • CMAKE_INCLUDE_PATH

        A list of directories specifying the search path for the find_file() and find_path() commands. They are used to find files and paths respectively. We need to pass in a file name, and the find_file() command will return the full path of the file to us; while the find_path() command will return the directory where the file is located.

        Where do these two commands find files? That is, it is specified through the CMAKE_INCLUDE_PATH variable. CMAKE_INCLUDE_PATH specifies a directory list, and find_file() and find_path() will search for files in this directory list. Next we test.

        For example, the project directory structure is as follows:

├── build
├── CMakeLists.txt
└── src
  └── 1.c

        The content of the top-level CMakeLists.txt file is as follows:

# CMakeLists.txt
cmake_minimum_required("VERSION" "3.5")
project(HELLO VERSION 1.1.0) #设置工程版本号为 1.1.0

find_file(P_VAR 1.c)
message(${P_VAR})

        Find the 1.c file through the find_file command, and record the path information in the P_VAR variable; now we have not set the CMAKE_INCLUDE_PATH variable to see if we can find the 1.c file, and the cmake print information is as follows:

         Obviously the prompt is not found, now we set the CMAKE_INCLUDE_PATH variable as follows:

# CMakeLists.txt
cmake_minimum_required("VERSION" "3.5")
project(HELLO VERSION 1.1.0) #设置工程版本号为 1.1.0

# 设置 CMAKE_INCLUDE_PATH 变量
set(CMAKE_INCLUDE_PATH ${PROJECT_SOURCE_DIR}/src)

# 查找文件
find_file(P_VAR 1.c)
message(${P_VAR})

        At this time, the printed information is:

         This time, the hello.c file was found successfully, and the full path of the file was returned to us.

  • CMAKE_LIBRARY_PATH

        A list of directories specifying the search path for the find_library() command. The find_library() command is used to search for library files, and find_library() will search from the directory list set by the CMAKE_LIBRARY_PATH variable.

  • CMAKE_MODULE_PATH

        A list of directories specifying the search path for CMake modules to be loaded by the include() or find_package() commands.

  • CMAKE_INCLUDE_DIRECTORIES_BEFORE

        This variable, mentioned earlier, can change the behavior of the include_directories() command. The include_directories() command will add directories to the back of the list by default. If CMAKE_INCLUDE_DIRECTORIES_BEFORE is set to on, the include_directories() command will add directories to the front of the list; similarly, if CMAKE_INCLUDE_DIRECTORIES_BEFORE is set to off or this variable is not set, include_directories() will append directories to the list.

  • CMAKE_IGNORE_PATH

        A list of directories ignored by the find_program(), find_library(), find_file() and find_path() commands. Indicates that these commands will not search in the list of directories specified by the CMAKE_IGNORE_PATH variable.


To be continued...

Guess you like

Origin blog.csdn.net/cj_lsk/article/details/131175508