cmake中-D选项的间距 [英] spacing in -D option in cmake

查看:678
本文介绍了cmake中-D选项的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-D CMAKE_C_COMPILER 是我用来选择我的编译器。但是,如果我有像$ code> USEIPHONEFLAG 打开/关闭的CMake选项,我需要做 -DUSEIPHONEFLAG = 1 -D USEIPHONEFLAG = 1 无法正常工作。我想知道 -D 之后的空间在CMake中的工作方式。

-D CMAKE_C_COMPILER is what I use to choose my compiler. However, if I have CMake options that are turned on/off like USEIPHONEFLAG, I need to do -DUSEIPHONEFLAG=1, -D USEIPHONEFLAG=1 does not work. I was wondering how the space after -D works in CMake.

推荐答案

这个问题可能取决于传递参数的顺序。

This issue is probably down to the order in which you pass the arguments.

在内部,CMake遍历命令行参数两次。第一次查找非缓存参数,并跳过 -D 开头的任何开头。任何不适合正确的参数列表的都被假定为CMakeLists.txt文件(或目录,或CMakeCache.txt)的路径。

Internally, CMake iterates over the command line arguments twice. The first time it's looking for non-cache arguments and skips any beginning with -D. Any that don't fit into the list of proper args are assumed to be the path to the CMakeLists.txt file (or the directory, or the CMakeCache.txt).

假设将只有一个路径通过,并且没有验证该假设。这里存在的问题。如果你已经通过了 -D Foo = 1 ,那么 -D 被视为一个完整的参数,和 Foo = 1 被视为路径。

It's assuming that there will be only one path passed, and does nothing to validate that assumption. Herein lies the problem. If you've passed -D Foo=1, then -D is seen as a complete argument and is skipped, and Foo=1 is seen as a path.

在通过args的第二次迭代中,通过 -D 给出的值,但在此运行它正确处理 -D 后的空格。因此,它理解 -D Foo = 1 Foo 设置为 1

On the second iteration through the args, it now grabs the values given via -D, but on this run it correctly handles spaces after the -D. So it understands that -D Foo=1 is setting Foo to 1.

所以,命令行中的路径位置在这里非常重要。

So, the position of the path in your command line is all-important here.

cmake -D Foo=1 MyProject/CMakeLists.txt  # --> Works
cmake MyProject/CMakeLists.txt -D Foo=1  # --> Fails

要进一步复杂的事情,你可以将空格换成引号,并让CMake解析它,变量名包含空格,并且在CMakeLists文件中不可用。

To complicate things further, you can wrap the space in quotes and have CMake parse it, but the variable name then includes the space and is unusable in the CMakeLists file.

cmake MyProject/CMakeLists.txt "-D Foo=1"  # --> Defines the unusable var ${ Foo}

另一个不一致是,其他命令行标志可用于或不使用空格(例如 -G ),其他需要一个空格(例如 -E )。

Another inconsistency is that other command line flags work with or without a space (e.g. -G), and others require a space (e.g. -E).

我自己的建议是总是避免在标志之后添加一个空格,除非它是必需的。我想总是通过最后的路径也会帮助,虽然如果你不添加额外的空间,这不应该需要。

My own advice would be to always avoid adding a space after the flag unless it's required. I guess always passing the path last would help too, although that shouldn't be required if you don't add extra spaces.

这篇关于cmake中-D选项的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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