编写或者查看一些开源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