CMake4-指令2-1:include【将需要在很多CMakeLists文件中复用的语句抽取出来放入.cmake文件中(如:MACRO宏、function函数)】【清单文件中的命令会被立即处理】

说到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(<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函数

1、宏和函数的定义

先看一下关键字: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传入的实际参数的个数,也就是调用函数是传入的参数个数

2、宏和函数的区别

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

其实和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

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

3、参数

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

更换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设置的变量,必须通过${}取出内容
  • 在宏的定义过程中,对变量进行的操作必须通过${}取出内容,而函数就没有这个必要

三、代码

test_cmake.cmake

#107cmake_include/cmake/test_cmake.cmake
message("in cmake/test_cmake.cmake")
```


CMakeLists.txt
```bash
#107cmake_include/CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project("cmake_include")
message("begin include")

include("cmake/test_cmake.cmake")
include("cmake/test_cmake.cmake")
include("cmake/test_cmake1.cmake" OPTIONAL) #OPTIONAL 可选,文件不存在不报错
include("cmake/test_cmake1.cmake" OPTIONAL  RESULT_VARIABLE ret) # RESULT_VARIABLE 返回值,是否引用成功
message("RESULT_VARIABLE ret= ${ret}") #NOTFOUND
include("cmake/test_cmake.cmake" OPTIONAL  RESULT_VARIABLE ret)
message("RESULT_VARIABLE ret= ${ret}") #导入文件的绝对路径

message("end include")

编译打印:

[main] Building folder: 107cmake_include 
[main] The folder containing the CMake cache is missing. The cache will be regenerated.
[main] Configuring project: 107cmake_include 
[proc] Executing command: /usr/bin/cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE "-S/home/wyr/cpp_study/cmake_study/2-第2章CMake常用功能/26cmake include分析和示例/107cmake_include" "-B/home/wyr/cpp_study/cmake_study/2-第2章CMake常用功能/26cmake include分析和示例/107cmake_include/build" -G Ninja
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 11.3.0
[cmake] -- The CXX compiler identification is GNU 11.3.0
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: /usr/bin/cc - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: /usr/bin/c++ - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] begin include
[cmake] in cmake/test_cmake.cmake
[cmake] in cmake/test_cmake.cmake
[cmake] RESULT_VARIABLE ret= NOTFOUND
[cmake] in cmake/test_cmake.cmake
[cmake] RESULT_VARIABLE ret= /home/wyr/cpp_study/cmake_study/2-第2章CMake常用功能/26cmake include分析和示例/107cmake_include/cmake/test_cmake.cmake
[cmake] end include
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: /home/wyr/cpp_study/cmake_study/2-第2章CMake常用功能/26cmake include分析和示例/107cmake_include/build
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build "/home/wyr/cpp_study/cmake_study/2-第2章CMake常用功能/26cmake include分析和示例/107cmake_include/build" --config Debug --target all --
[build] ninja: no work to do.
[driver] Build completed: 00:00:00.021
[build] Build finished with exit code 0

【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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值