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

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

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


include指令

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

include指令的结构为:

include(<file|module> [OPTIONAL] [RESULT_VARIABLE <var>]
                      [NO_POLICY_SCOPE])

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

include(file|module)

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

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

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

什么是宏?什么是函数?


MACRO宏和function函数

宏和函数的定义

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

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

function(<name> [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实战–include命令和macro宏
cmake使用教程(八)-macro和function
CMake中include指令用法介绍

好的,下面为您介绍 CMake macrofunction。 ## 1. macro macro 是一种 CMake定义机制,可以将一组 CMake 命令封装起来,用一个名字来调用,从而简化 CMakeLists.txt 的代码量。 macro 的定义格式如下: ``` macro(macro_name [arg1 [arg2 [arg3 ...]]) # command1 # command2 # ... endmacro() ``` 其macro_name 是的名称,arg1、arg2、arg3...是的参数列表,可以有 0 至多个参数。 下面是一个简单的例子,定义了一个名为 print_info 的,该接受两个参数,分别是 name 和 age,然后输出一条信息: ``` macro(print_info name age) message("Name: ${name}, Age: ${age}") endmacro() print_info("Alice" 18) ``` 执行上述代码后,输出结果为: ``` Name: Alice, Age: 18 ``` ## 2. function functionmacro 的作用类似,也是将一组 CMake 命令封装起来,用一个名字来调用,但 function 有明确的返回值。function 的定义格式如下: ``` function(func_name [arg1 [arg2 [arg3 ...]]) # command1 # command2 # ... return(value) endfunction() ``` 其,func_name 是函数的名称,arg1、arg2、arg3...是函数的参数列表,可以有 0 至多个参数。value 是函数的返回值。 下面是一个简单的例子,定义了一个名为 add 的函数,该函数接受两个参数,分别是 a 和 b,然后返回它们的和: ``` function(add a b) set(result ${a}+${b}) return(${result}) endfunction() set(sum 0) math(EXPR sum "${sum} + $(add(1 2))") message("Sum: ${sum}") ``` 执行上述代码后,输出结果为: ``` Sum: 3 ``` 在 function ,我们使用 set 命令定义了一个变量 result,然后用 return 返回了这个变量的值。在调用 add 函数时,我们使用了 $() 将函数调用结果作为表达式的一部分,然后再用 math 命令计算出 sum 的值。 以上就是 CMake macrofunction 的介绍,希望对您有所帮助。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值