• 主页
  • 系列总集
  • OpenCV
  • CMake
  • iOS
  • Java
  • 前端
所有文章 关于我

  • 主页
  • 系列总集
  • OpenCV
  • CMake
  • iOS
  • Java
  • 前端

CMake05-函数和宏的对比

2019-12-05

CMake的函数和宏

CMake本身整理逻辑可以通过 宏(macro) 和 函数(function),声明十分类似

官方文档说明

在宏的官方文档中有提到两者不同的问题,提出了两点主要的差别

  1. 宏是类似C语言的预处理,大部分是字符串替换,不会有函数内的变量
  2. 遇到return()宏不会停止,函数会直接返回
1
2
3
4
5
6
The macro command is very similar to the function() command. ......

In a function, ARGN, ARGC, ARGV and ARGV0, ARGV1, … are true variables in the usual CMake sense. In a macro, they are not, they are string replacements much like the C preprocessor would do with a macro. .....

Another ..... .... return() in a macro body does not just terminate execution of the macro; ......

更重要的区别Scope不同

例如我们想输入一个变量获得结果,有两种方式,但是经过对比可以发现 load_message_func 并不能正确输出

原因是因为func拥有单独的scope, variable 和外部的并不通用,C语言里一般会采用传入指针来解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 声明Macro
macro(load_message_macro variable)
set(${variable} "message_one")
endmacro()

# 声明Func
function(load_message_func variable)
set(${variable} "message_two")
endfunction()

# 使用Macro
load_message_macro(MESSAGE_ONE)
message("load_message_macro : " ${MESSAGE_ONE})

# 使用Func
load_message_func(MESSAGE_TWO)
message("load_message_func : " ${MESSAGE_TWO})

CMake里的函数是没有返回值的,也就是说需要有方法做到类似JS的作用域提升

1
2
3
4
5
6
7
8
9
10
11
12
13
# 声明Func,并采用作用域提升
function(load_message_parent variable)
set(${variable} "message_three")
# 变量提升
# ${variable} 被展开成 MESSAGE_THREE
# ${${variable}} 被展开成 message_three 即 MESSAGE_THREE 的值
# PARENT_SCOPE 指父作用域,是set方法的参数
set(${variable} ${${variable}} PARENT_SCOPE)
endfunction()

# 使用了变量提升的Func
load_message_parent(MESSAGE_THREE)
message("load_message_parent : " ${MESSAGE_THREE} "\n")
赏

请问老板还招人么(/ω\)

支付宝
微信
  • CMake
  • Tutorial

扫一扫,分享到微信

微信分享二维码
CMake06-加载文件进行复用
CMake04-添加第三方库
© 2021 Alan Li
Hexo Theme Yilia by Litten
  • 所有文章
  • 关于我

tag:

  • iOS
  • Java
  • Collection
  • Python
  • Shell
  • CMake
  • Memory
  • JavaScript
  • Architecture
  • AnchorPoint
  • Android
  • Web
  • Annotation
  • AFNetworking
  • Window
  • ViewController
  • AutoLayout
  • Dozer
  • CoreAnimation
  • Cycle Retain
  • Block
  • UI
  • IDE
  • FrontEnd
  • CSS
  • Category
  • TableViewCell
  • Security
  • Net
  • JSP
  • Spring
  • C
  • MyBatis
  • Date
  • React
  • GCD
  • UITouch
  • Gesture
  • UIControl
  • Git
  • HTML
  • HTTPS
  • HTTP
  • Servlet
  • Server
  • DataBase
  • MySQL
  • Linux
  • Tutorial
  • Ajax
  • Type
  • JQuery
  • JSON
  • Exception
  • Parameter
  • Reflect
  • Thread
  • Sort
  • KVO
  • MKMapKit
  • Overlay
  • Maven
  • Configure
  • Tips
  • Transaction
  • Swift
  • NavigationBar
  • Nginx
  • Runtime
  • OpenCV
  • Property
  • Playground
  • Protocol
  • Redux
  • ScrollView
  • Session
  • Cookie
  • Shiro
  • Error
  • Singleton
  • RegEx
  • StackView
  • StatusBar
  • Base64
  • Socket
  • TCP
  • IP
  • TextField
  • CALayer
  • UILabel
  • View
  • Animation
  • Xcode
  • Hexo
  • Terminal
  • OC
  • Device
  • Log
  • Image
  • JUnit
  • Oval
  • Archive
  • XSS
  • Compiler
  • Aspect
  • Responder
  • Class
  • FireWall
  • RetainCount
  • Const
  • Frame
  • String
  • Symbols
  • Framework
  • CocoaPods
  • Unity
  • Message
  • Button
  • AuthorizationStatus
  • Struct
  • XCTest
  • NSNotification
  • Contact

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

我写的,大概率是错的。。。。。