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

        Continue to learn common variables in CMakeLists.txt

variables that describe the system

        As the name suggests, these variables describe some information about the system:

variable illustrate
CMAKE_HOST_SYSTEM_NAME The name of the operating system running cmake (actually uname -s)
CMAKE_HOST_SYSTEM_PROCESSOR The processor name of the operating system running cmake (uname -p)
CMAKE_HOST_SYSTEM Operating system running cmake (compound information)
CMAKE_HOST_SYSTEM_VERSION The version number of the operating system running cmake (uname -r)
CMAKE_HOST_UNIX If the operating system running cmake is UNIX and UNIX-like, this variable is true, otherwise it is a null value
CMAKE_HOST_WIN32 If the operating system running cmake is Windows, this variable is true, otherwise it is empty
CMAKE_SYSTEM_NAME The name of the target host operating system
CMAKE_SYSTEM_PROCESSOR The processor name of the target host
CMAKE_SYSTEM Operating system of the target host (composite information)
CMAKE_SYSTEM_VERSION The version number of the target host OS
ENV for accessing environment variables
UNIX Equivalent to CMAKE_HOST_UNIX
win32 Equivalent to CMAKE_HOST_WIN32
  • CMAKE_HOST_SYSTEM_NAME、CMAKE_HOST_SYSTEM_PROCESSOR 、 CMAKE_HOST_SYSTEM 和 CMAKE_HOST_SYSTEM_VERSION

        These four variables describe the information related to the host running cmake, we can print it out directly to see:

# 打印信息
message(${CMAKE_HOST_SYSTEM_NAME})
message(${CMAKE_HOST_SYSTEM_PROCESSOR})
message(${CMAKE_HOST_SYSTEM})
message(${CMAKE_HOST_SYSTEM_VERSION})

        The corresponding print information is as follows:

         Everyone will know it by comparing it for themselves, so I won't say more.

  • CMAKE_SYSTEM_NAME 、 CMAKE_SYSTEM_PROCESSOR 、 CMAKE_SYSTEM 和 CMAKE_SYSTEM_VERSION

        These 4 variables are used to describe the information related to the target host. The target host refers to the host on which the executable file runs, such as our ARM development board.

# 打印信息
message(${CMAKE_SYSTEM_NAME})
message(${CMAKE_SYSTEM_PROCESSOR})
message(${CMAKE_SYSTEM})
message(${CMAKE_SYSTEM_VERSION})

        The cmake print information is as follows:

         Because we have not configured cross-compilation for cmake, the compilation tools of the Ubuntu system (the host running cmake) will be used by default, so the generated target files (executable files or library files) can only run in the Ubuntu system, so these 4 The first variable records the information of the Ubuntu host.

  • ENV

        This variable can be used to access environment variables, the usage is very simple $ENV{VAR}

# 访问环境变量
message($ENV{XXX})

        Access the XXX environment variable through $ENV{XXX}, let’s test it, first use the export command to export the XXX environment variable under the Ubuntu system:

export XXX="Hello World!"
cd build/
cmake ..

        The print information is as follows:

         It can be seen from the printed information that the ENV variable can indeed access the environment variable of the Linux system.

Variables that control compilation

        These variables control the compilation process as follows:

variable illustrate
EXECUTABLE_OUTPUT_PATH The output path of the executable program
LIBRARY_OUTPUT_PATH The output path of the library file

        We have used these two variables before, and they are used to set the output directory of the executable file and the output directory of the library file respectively. Next, we will conduct a simple test. For example, the project directory structure is as follows:

├── build
├── CMakeLists.txt
├── hello
│ ├── hello.c
│ └── hello.h
└── main.c

        hello.c will be compiled into a dynamic library file libhello.so, and main.c will be compiled into an executable program. The main.c source code calls the functions provided by hello.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

# 设置可执行文件和库文件输出路径
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

# 头文件包含
include_directories(hello)

# 动态库目标
add_library(hello SHARED hello/hello.c)

# 可执行程序目标
add_executable(main main.c)
target_link_libraries(main PRIVATE hello) #链接库

        Enter the build directory, execute cmake and make to build and compile, and finally the executable file main and library file libhello.so will be generated. The directory structure is as follows:

├── build
│ ├── bin
│ │ └── main
│ ├── lib
│ └── libhello.so
├── CMakeLists.txt
├── hello
│ ├── hello.c
│ └── hello.h
└── main.c

        This is because we set EXECUTABLE_OUTPUT_PATH and LIBRARY_OUTPUT_PATH so that the generated executable program is in the build/bin directory and the generated library file is in the build/lib directory. If these two lines are commented out, then the generated file is in the build directory, because by default, the output directory of the final object file is the BINARY_DIR of the source code.

Guess you like

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