cmake列表追加编译器标志会产生伪造的结果? [英] cmake list append for compiler flags yields bogus results?

查看:61
本文介绍了cmake列表追加编译器标志会产生伪造的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在CMake文件(CMake 2.8.10.2)的C和C ++编译行中添加各种标志.我看到有些人使用 add_definitions ,但是从我看到的内容来看,它们是用于预处理器标志( -D )的.我有一些我不想传递给预处理器的标志.

I need to add various flags to my C and C++ compile lines in my CMake files (CMake 2.8.10.2). I see some people use add_definitions but from what I can see that is intended for preprocessor flags (-D). I have some flags that I don't want passed to the preprocessor.

所以我一直在尝试修改 CMAKE_C_FLAGS CMAKE_CXX_FLAGS .我看到有人在使用类似的东西:

So I've been trying to modify CMAKE_C_FLAGS and CMAKE_CXX_FLAGS. I see that some people were using something like:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -new -flags -here")

,但是随后我在cmake文档中读到,这效率较低,而正确的方法是使用 list(APPEND ...),如下所示:

but then I read in the cmake docs that this is less efficient, and the right way to do it is to use list(APPEND ...), like this:

list(APPEND CMAKE_C_FLAGS -new -flags -here)

但是,当我这样做时,我的编译行包含用分号分隔的标志,并且是语法错误.我读到现在列表是在内部存储的,但是我想当我使用该变量时,可以通过cmake来解决.这似乎很基础;难道我做错了什么?我的意思是,如果不能使用它们,除非您碰巧想要一个用分号分隔的值列表(除了我猜想Windows%PATH%设置之外,还有谁想要的),这些列表有什么用?即使文档显示它的效率/适用性较低,我也应该使用引用的版本吗?

However, when I do this my compile line contains the flags separated by semicolons and is a syntax error. I read that this is now lists are stored internally, but I figured this would be taken care of by cmake when I used the variable. This seems so basic; am I doing something wrong? I mean, what the heck good are these lists if they can't be used unless you happen to want a semicolon-separated list of values (and who wants that, other than I guess Windows %PATH% settings or something)? Should I be using the quoted version even though the docs suggest it's less efficient/appropriate?

推荐答案

在这种情况下,您实际上通常确实会使用 set(CMAKE_C_FLAGS"$ {CMAKE_C_FLAGS} -new -flags -here")技术.

In this case, you would indeed normally use the set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -new -flags -here") technique.

您是正确的,在大多数其他情况下,CMake可以将用分号分隔的列表翻译"成对编译器有意义的内容(例如,可执行文件中的源文件列表),但是在这种情况下,CMake可以使用标志作为一个完整的字符串传递给编译器/链接器.

You're right in that in most other contexts CMake can "translate" a semi-colon separated list into something meaningful for the compiler (e.g. the list of source files in an executable), but in this case, CMake takes the flags as a single, complete string to pass to the compiler/linker.

如果您真的想将标志列表保留为CMake列表,则可以,但是在退出CMakeLists.txt之前,您可以自己将列表翻译"成单个字符串值,即 CMAKE_C_FLAGS ,但是看到这种情况并不常见.

You could if you really wanted keep the list of flags as a CMake list, but then before exiting the CMakeLists.txt, you could yourself "translate" the list into a single string value of CMAKE_C_FLAGS, but it's unusual to see this.

这篇关于cmake列表追加编译器标志会产生伪造的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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