C++-CMake指令:include指令【.cmake文件/MACRO宏/function函数】
admin
2024-02-01 03:06:23

说到cmake,可能最先想到的就是CmakeLists.txt文件,但是在很多情况下,也会看到.cmake文件。也许,你会诧异,.cmake文件是干什么的,甚至会想.cmake文件是不是cmake的正统文件,而CmakeLists.txt并不是。

但其实,CmakeLists.txt才是cmake的正统文件,而.cmake文件是一个模块文件,可以被include到CMakeLists.txt中。
 

一、include指令

include指令一般用于语句的复用,也就是说,如果有一些语句需要在很多CMakeLists.txt文件中使用,为避免重复编写,可以将其写在.cmake文件中,然后在需要的CMakeLists.txt文件中进行include操作就行了。

include指令的结构为:

include( [OPTIONAL] [RESULT_VARIABLE ][NO_POLICY_SCOPE])

虽然,有不少的可选参数,但是一般情况下,都是直接写:

include(file|module)

注意,为了使CMakeLists.txt能够找到该文件,需要指定文件完整路径(绝对路径或相对路径),当然如果指定了CMAKE_MODULE_PATH,就可以直接include该目录下的.cmake文件了。

.cmake文件里面通常是什么信息呢?

.cmake文件里包含了一些cmake命令和一些宏/函数,当CMakeLists.txt包含该.cmake文件时,当编译运行时,该.cmake里的一些命令就会在该包含处得到执行,并且在包含以后的地方能够调用该.cmake里的一些宏和函数。

什么是宏?什么是函数?

二、MACRO宏和function函数

1、宏和函数的定义

先看一下关键字:cmake的宏是MACRO,函数是function。它们的用法是:

macro( [arg1 [arg2 [arg3 ...]]])COMMAND1(ARGS ...)            # 命令语句COMMAND2(ARGS ...)...
endmacro()function( [arg1 [arg2 [arg3 ...]]])COMMAND1(ARGS ...)            # 命令语句COMMAND2(ARGS ...)...
function()

定义一个名称为name的宏(函数),arg1...是传入的参数。我们除了可以用${arg1}来引用变量以外,系统为我们提供了一些特殊的变量:

变量说明
argv##是一个下标,0指向第一个参数,累加
argv所有的定义时要求传入的参数
argn定义时要求传入的参数以外的参数
argc传入的实际参数的个数,也就是调用函数是传入的参数个数

宏和函数的区别

那么宏和函数之间的区别是什么呢?

其实和C/C++里面宏和函数之间的区别差不多,宏就是字符串替换,函数就是使用变量,在命令中途可以对改变量进行修改。

以StackOverflow的例子来了解一下区别:

首先创建一个CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
include(test.cmake)

在同目录下创建文件test.cmake:

set(var "ABC")macro(Moo arg)message("arg = ${arg}")set(arg "abc")message("# After change the value of arg.")message("arg = ${arg}")
endmacro()
message("=== Call macro ===")
Moo(${var})function(Foo arg)message("arg = ${arg}")set(arg "abc")message("# After change the value of arg.")message("arg = ${arg}")
endfunction()
message("=== Call function ===")
Foo(${var})

运行cmake:

mkdir build && cd build
cmake ..

运行后的输出结果是:

-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
=== Call macro ===
arg = ABC
# After change the value of arg.
arg = ABC
=== Call function ===
arg = ABC
# After change the value of arg.
arg = abc
-- Configuring done
-- Generating done
-- Build files have been written to: /home/yngzmiao/test/build

从这里可以看出,宏实现的仅仅是字符串替换,宏定义的过程中是无法进行修改的,而函数却是可以的。

蛋疼的参数

一般情况下,从上面的例子就能看出宏和函数的用法了,但很多情况下,我们自以为的“懂了”都是假懂。比如一不小心,就会出错。

更换test.cmake为下面的内容,并运行:

set(var "ABC")macro(Moo arg)message("arg = ${arg}")set(arg "abc")message("# After change the value of arg.")message("arg = ${arg}")
endmacro()
message("=== Call macro ===")
Moo(var)function(Foo arg)message("arg = ${arg}")set(arg "abc")message("# After change the value of arg.")message("arg = ${arg}")
endfunction()
message("=== Call function ===")
Foo(var)

运行后的输出结果是:

-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
=== Call macro ===
arg = var
# After change the value of arg.
arg = var
=== Call function ===
arg = var
# After change the value of arg.
arg = abc
-- Configuring done
-- Generating done
-- Build files have been written to: /home/yngzmiao/test/build

对比两段程序可以看出其中的区别:无论是宏还是函数,当调用的时候如果使用的是set出来的变量,都必须通过${}将变量的内容传递进去,而不能只写上变量名。

这是将实参传递给形参时的注意点,但在宏和函数的实现过程中,还有需要注意的内容。

例子:

set(var "ABC")macro(Moo arg)if (arg STREQUAL "ABC")message("arg1 = ${arg}")endif()if (${arg} STREQUAL "ABC")message("arg2 = ${arg}")endif()
endmacro()
message("=== Call macro ===")
Moo(${var})function(Foo arg)if (arg STREQUAL "ABC")message("arg1 = ${arg}")endif()if (${arg} STREQUAL "ABC")message("arg2 = ${arg}")endif()
endfunction()
message("=== Call function ===")
Foo(${var})

运行后的输出结果是:

-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
=== Call macro ===
arg2 = ABC
=== Call function ===
arg1 = ABC
arg2 = ABC
-- Configuring done
-- Generating done
-- Build files have been written to: /home/yngzmiao/test/build

可以看出,在宏和函数的实现过程中,宏的参数由于不是传统意义上的变量,而是字符串替换,因此需要通过${}取出内容。而函数却不一定需要这样。

也就是说,对于macro宏而言:

if(argv0)                         # 错误用法
if(${argv0})                      # 正确用法
if(defined argv0)                 # 错误用法
if(defined ${argv0})              # 正确用法

也就是说,对于宏和函数的参数而言:

  • 当宏和函数调用的时候,如果传递的是经set设置的变量,必须通过${}取出内容;
  • 在宏的定义过程中,对变量进行的操作必须通过${}取出内容,而函数就没有这个必要。

【CMake】cmake中的include指令(.cmake文件/MACRO宏/function函数)_Yngz_Miao的博客-CSDN博客

CMake中include指令介绍_liitdar的博客-CSDN博客_cmake include 

cmake(三十五)Cmake之include指令_wzj_110的博客-CSDN博客 

CMake中include指令介绍_liitdar的博客-CSDN博客_cmake include 

相关内容

热门资讯

山水皆有情!丽江两山一江的千年... 去过丽江、走过虎跳峡的人,大概率都听过哈巴雪山和金沙江的传说。很多人站在峡谷之下,望着奔腾不息的金沙...
活力中国调研行丨“殷”你而来 ... 9月12日,游客在殷墟博物馆“甲骨文展厅”内参观。位于河南省安阳市的殷墟博物馆新馆是首个全景式展现商...
浦江之夜赴美好相逢,第37届上... 9月12日晚,黄浦江畔灯火璀璨,第37届上海旅游节大巡游在外滩中山东一路上演。不少观众早早便来到外滩...
美丽中国丨秋韵紫鹊界 梯田入画... 这是9月11日拍摄的紫鹊界梯田(无人机照片)。 金秋九月,湖南省娄底市新化县紫鹊界梯田稻谷渐次成熟。...
中国之巅奏响《共同的节日》,千... 千吨阻尼器,“迎世赛、游上海旅游节”。9月13日,由上海市文化和旅游局指导推出上海旅游·会客│巅峰之...