CMake Learning (Four) - Using variables

CMake can also use variables, such as when a file or need to generate too much reliance on complicated projects, you can use a variable unified management, but also facilitate conditional compilation later.

First, the definition of variables

CMake points define the variable explicit and implicit two.
That is explicitly defined using the set and other statements, you can customize the variable name.
Is implicitly defined variables are automatically created when you use other statements, such as the name of the project is to define the project, but at the same time define the name, <projectname> _BINARY_DIR and <projectname> _SOURCE_DIR two variables are implicitly defined.
For example:
only execute the following statement

project(test_4)

test_4_BINARY_DIR and test_4_SOURCE_DIR two variables is implicitly defined.

set statement syntax

set(<variable> <value>...

Such as:

set(USER_KEY "Hello World")

Multi-value can be assigned to a variable:

set(USER_KEY 
	"Hello World"
	"Hello CMake"
	)

Second, the reference variable

Use $ {variable} for references to variables. In if statements used directly without passing through the variable name $ {variable} values.
For example CMake learning (a) in CMakeLists.txt content:

# CMakeLists.txt
# CMake最低版本要求
cmake_minimum_required(VERSION 3.5)

# 项目名称
project(test_1)

# 生成可执行文件,test_1是可执行文件的名字,hello.c是源文件名称,如有其他源文件,可在后面添加
add_executable(test_1 hello.c) 

Be amended as follows:

# CMakeLists.txt
# CMake最低版本要求
cmake_minimum_required(VERSION 3.5)

# 项目名称
project(test_1)

set(SOURCE hello.c)

# 生成可执行文件,test_1是可执行文件的名字,hello.c是源文件名称,如有其他源文件,可在后面添加
add_executable(test_1 ${SOURCE}) 

Reference:
https://www.cnblogs.com/linuxAndMcu/p/10670591.html
https://cmake.org/cmake/help/v3.14/command/set.html
https://blog.csdn.net/ LaineGates / article / details / 89847726

Published 72 original articles · 87 won praise · Views 200,000 +

Guess you like

Origin blog.csdn.net/maizousidemao/article/details/104096417