CMake文件在脚本模式下继承变量 [英] CMake file in script mode inheriting variables

查看:506
本文介绍了CMake文件在脚本模式下继承变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从脚本模式(-P)调用cmake文件从其他cmake文件,所以这个cmake孩子知道其父的所有变量?因为,如果我有很多变量,孩子需要,我必须写很多-D选项,我想避免它。

How can I call to a cmake file in script mode (-P) from other cmake file, so this "cmake child" knows all variable of its parent? Because, if I have a lot of variables the child needs, I have to write many -D options, and I want to avoid it.

示例:

// CMakeLists.txt
cmake_minimum_required(VERSION 2.8)

set(teststr "Hello World!")

add_custom_command(test
   ${CMAKE_COMMAND} -Dteststr=${teststr} -P test.cmake
)

// test.cmake
message("${teststr}")

$ cmake .
$ make test
Hello world!
Built target test

工作正常!但是,没有-Dteststr:

Works fine!. But, without "-Dteststr":

// CMakeLists.txt
cmake_minimum_required(VERSION 2.8)

set(teststr "Hello World!")

add_custom_command(test
   ${CMAKE_COMMAND} -P test.cmake
)

// test.cmake
message("${teststr}")

$ cmake .
$ make test

Built target test

当然,没有-D选项,test.cmake中的teststr变量未设置,因此输出为空。

Of course, without -D option, the "teststr" variable, in test.cmake, is unset, and thus, the output is empty.

调用test.cmake的任何选项遗产模式或类似的东西?

Any option to call test.cmake in "heritage mode", or something like that?

推荐答案

您可以传递参数到您调用的脚本 cmake -P
如果调用:

You can pass arguments to a script you call with cmake -P. If you call:

cmake -P <script-file> <arg3> <arg4> <arg5> ...

然后变量 CMAKE_ARGC CMAKE_ARGV0 CMAKE_ARGV1 ,...将可用于脚本。
请参阅 CMAKE_ARGC CMAKE_ARGV0

then the variables CMAKE_ARGC, CMAKE_ARGV0, CMAKE_ARGV1, ... will be available for the script. See documentation for CMAKE_ARGC and CMAKE_ARGV0.

另一种方法是以定义变量,就像使用非脚本 cmake 命令。有一件事:你需要在 -P 之前定义变量:

The other way is to define variables, just like with the non-script cmake command. There's one thing: you need to define the variables before -P:

cmake -DVAR=VALUE -DFOO=BAR -P <script-file> <arg5> <arg6> ...

此外,脚本文件后的args编号。

Also, not that the numbering of the args after the script-file will be shifted accordingly.

这篇关于CMake文件在脚本模式下继承变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆