CMake Variables: Master Common Variables and Environment Variables

CMake Variables: Master Common Variables and Environment Variables

1. CMake Variable Reference and Definition

(1) Use ${} to reference variables. In statements like IF, use the variable name directly without ${}.

(2) Custom variables can be defined implicitly and explicitly. For example, the PROJECT command implicitly defines <projectname>_BINARY_DIR and <projectname>_SOURCE_DIR. Explicitly defining variables can be done using the SET command.

For example:

SET(HELLO_SRC main.c)

You can reference this custom variable using ${HELLO_SRC}.

2. Common CMake Variables

(1) The three variables CMAKE_BINARY_DIR, PROJECT_BINARY_DIR, and <projectname>_BINARY_DIR refer to the same content. If compiling in source, they refer to the top-level directory of the project; if compiling out-of-source, they refer to the directory where the project compilation occurs. PROJECT_BINARY_DIR is slightly different from other commands but can be understood as consistent for now.

(2) The variables CMAKE_SOURCE_DIR, PROJECT_SOURCE_DIR, and <projectname>_SOURCE_DIR consistently refer to the top-level directory of the project, regardless of the compilation method. That is, during in-source compilation, they are consistent with variables like CMAKE_BINARY_DIR. PROJECT_SOURCE_DIR is slightly different from other commands but can be understood as consistent for now.

(3) CMAKE_CURRENT_SOURCE_DIR refers to the path of the currently processed CMakeLists.txt.

(4) CMAKE_CURRENT_BINARY_DIR, if compiling in-source, is consistent with CMAKE_CURRENT_SOURCE_DIR; if compiling out-of-source, it refers to the target compilation directory. You can change this variable’s value using ADD_SUBDIRECTORY(src bin). Using SET(EXECUTABLE_OUTPUT_PATH <new path>) will not affect this variable; it only modifies the path where the final target file is stored.

(5) CMAKE_CURRENT_LIST_FILE outputs the full path of the CMakeLists.txt that calls this variable.

(6) CMAKE_CURRENT_LIST_LINE outputs the line where this variable is located.

(7) The CMAKE_MODULE_PATH variable defines the path to your own CMake modules. If your project is complex, you might write some CMake modules that are released with your project. To let CMake find these modules while processing CMakeLists.txt, you need to set your CMake module path using the SET command.

For example:

SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

At this point, you can call your own modules using the INCLUDE command.

(8) EXECUTABLE_OUTPUT_PATH and LIBRARY_OUTPUT_PATH are used to redefine the storage directories for the final results.

(9) PROJECT_NAME returns the project name defined by the PROJECT command.

3. Calling Environment Variables in CMake

You can call system environment variables using $ENV{NAME}.

For example:

MESSAGE(STATUS "HOME dir: $ENV{HOME}")

Setting environment variables is done as follows:

SET(ENV{variable_name} value)

(1) CMAKE_INCLUDE_CURRENT_DIR automatically adds CMAKE_CURRENT_BINARY_DIR and CMAKE_CURRENT_SOURCE_DIR to the currently processed CMakeLists.txt. This is equivalent to adding the following to each CMakeLists.txt:

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}).

(2) CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE ensures that the project’s provided header file directories are always ahead of the system header file directories, which can help when your defined headers conflict with system headers.

(3) There are also CMAKE_INCLUDE_PATH and CMAKE_LIBRARY_PATH.

4. System Information

  1. CMAKE_MAJOR_VERSION, the major version number of CMake, e.g., 2 in 2.4.6.

  2. CMAKE_MINOR_VERSION, the minor version number of CMake, e.g., 4 in 2.4.6.

  3. CMAKE_PATCH_VERSION, the patch level of CMake, e.g., 6 in 2.4.6.

  4. CMAKE_SYSTEM, system name, e.g., Linux-2.6.22.

  5. CMAKE_SYSTEM_NAME, system name without version, e.g., Linux.

  6. CMAKE_SYSTEM_VERSION, system version, e.g., 2.6.22.

  7. CMAKE_SYSTEM_PROCESSOR, processor name, e.g., i686.

  8. UNIX is TRUE for all UNIX-like platforms, including OS X and Cygwin.

  9. WIN32 is TRUE for all Win32 platforms, including Cygwin.

5. Major Switch Options

  1. CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS controls the writing style of IF ELSE statements.

  2. BUILD_SHARED_LIBS controls the default library compilation method. If not set, libraries compiled without specifying a type using ADD_LIBRARY are static by default. If SET(BUILD_SHARED_LIBS ON) is set, dynamic libraries are generated by default.

  3. CMAKE_C_FLAGS sets C compilation options, which can also be added using the ADD_DEFINITIONS() command.

  4. CMAKE_CXX_FLAGS sets C++ compilation options, which can also be added using the ADD_DEFINITIONS() command.

Conclusion

This article introduces some commonly used CMake variables. These variables are just a small part of all CMake variables, and currently, English documentation for CMake is somewhat lacking. To learn more about CMake variables, a better approach is to read the CMake files of successful projects, such as the code of KDE4.

Welcome to follow my public account

Daily article updates

CMake Variables: Master Common Variables and Environment Variables

Long press the image above to recognize the QR code to follow

Leave a Comment