CMake中的ARGC ARGV 和ARGN参数意义

编写或者查看一些开源cmake文件中经常遇到在宏(macro)和函数中经常会遇到ARGC ARGV 和ARGN等参数,该参数为cmake中专用变量,分别表示宏或者函数参数中的特殊意义。

ARGC

ARGC代表的是函数或者宏传递的参数个数。查看cmake官方文档原文表述:

Number of command line arguments passed to CMake in script mode.

When run in -P script mode, CMake sets this variable to the number of command line arguments. See also CMAKE_ARGV0, 1, 2 …

ARGV

ARGV代表所有传递的参数,使用list表示,其中如果函数有多个参数,要取得某个参数可以使用ARGV0,ARGV1,ARGV2等。

Command line argument passed to CMake in script mode.

When run in -P script mode, CMake sets this variable to the first command line argument. It then also sets CMAKE_ARGV1, CMAKE_ARGV2, … and so on, up to the number of command line arguments given. See also CMAKE_ARGC.

ARGN 

包含传入参数的list, 与ARGV不同的是并不是代表所有参数,而是指宏或者函数声明的参数之后的所有参数。

可以看下cmake官方问题说明:

 This facilitates creating functions with optional arguments. Additionally ARGV holds the list of all arguments given to the function and ARGN holds the list of arguments past the last expected argument.

用例

可以使用简单用例来说明:

cmake_minimum_required(VERSION 3.4.3)

macro(arg_test para1 para2)
        MESSAGE(STATUS ARGC=${ARGC})
        MESSAGE(STATUS ARGV=${ARGV})
        MESSAGE(STATUS ARGN=${ARGN})
        MESSAGE(STATUS ARGV0=${ARGV0})
        MESSAGE(STATUS ARGV1=${ARGV1})
        MESSAGE(STATUS ARGV2=${ARGV2})
endmacro()

arg_test(para_1, para_2,para_3,para_4)
                                

运行结果:

 

-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.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
-- ARGC=2
-- ARGV=para_1,para_2,para_3,para_4
-- ARGN=
-- ARGV0=para_1,
-- ARGV1=para_2,para_3,para_4
-- ARGV2=
-- Configuring done
-- Generating done
-- Build files have been written to: /home/hzk/test/cmake

 

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
CMake,$符号通常用作变量的引用符号。它用于引用变量的值或进行字符串替换。在CMakeLists.txt文件,可以使用$符号来引用变量的值,例如${VAR_NAME}。这样可以在CMake配置过程使用变量的值来进行条件判断、路径拼接等操作。此外,$符号还可以用于字符串替换,例如将一个字符串的某个子字符串替换为另一个字符串。在CMake,可以使用字符串替换操作来修改文件路径、修改编译选项等。总之,$符号在CMake具有重要的意义,用于引用变量的值和进行字符串替换。 #### 引用[.reference_title] - *1* [CMake(九):生成器表达式](https://blog.csdn.net/jjjstephen/article/details/122467789)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [CMakemacro的使用](https://blog.csdn.net/fengbingchun/article/details/127145153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [CMakeARGC ARGVARGN参数意义](https://blog.csdn.net/weixin_42730667/article/details/107317483)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Huo的藏经阁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值