windows--cmake与c++的使用教程(16)

1 概述

本节目标: macro编写与函数编写

2 macro 与 function

  • 可类比C语言中的宏定义与函数
  • CMake function传递参数时,不用传递参数类型
  • cmake macro宏只是对字符串的简单替换。和define类似。

3 macro 语法

  • macro需 与 endmacro配对使用
macro(<name> [<arg1> ...])
  <commands>
endmacro()

5 function 语法

  • function需与endfunction配对使用,
  • function可结合return()使用, 也就是说, 函数体可以写 return()
function(<name> [<arg1> ...])
  <commands>
endfunction()

6 一个例子帮助理解macro和function的区别

6.1 cmake脚本

set(var_demo "ABC")

# 宏定义
macro(macro_demo arg)
  message("arg = ${arg}")
  set(arg "123")
  message("# after change the value of arg.")
  message("arg = ${arg}")
endmacro()

message("")
message("=== call macro ===")
macro_demo(${var_demo})
message("")
message("")

# 函数定义
function(func_demo arg)
  message("arg = ${arg}")
  set(arg "123")
  message("# after change the value of arg.")
  message("arg = ${arg}")
endfunction()

message("")
message("=== call function ===")
func_demo(${var_demo})
message("")
message("")
message("")

6.2 输出结果

=== call macro ===
arg = ABC
# after change the value of arg.
arg = ABC



=== call function ===
arg = ABC
# after change the value of arg.
arg = 123

7 function 中使用return

  • 类似C语言函数return语句。

7.1 一个例子

function(func_demo_return )
    message("11111")
    return()
    message("22222")
endfunction(func_demo_return )

func_demo_return()

7.2 输出结果

11111

因为执行了return(), 所以不会输出 22222

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值