C++ 附加到 CMAKE_C_FLAGS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29901352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Appending to CMAKE_C_FLAGS
提问by TobiSF
I'm using CMake for a project that comes in two versions, one of which requires -lglapi and the other does not.
我将 CMake 用于一个有两个版本的项目,其中一个需要 -lglapi,另一个不需要。
So far the lines we used look like that:
到目前为止,我们使用的行看起来像这样:
SET(CMAKE_C_FLAGS "-O3 -xSSE3 -restrict -lpthread -lX11 -ldrm")
SET(CMAKE_CXX_FLAGS "-O3 -xSSE3 -restrict -lpthread -lX11 -ldrm")
I added an if statement in my CMakeList.txt exactly after those lines:
我在 CMakeList.txt 中的这些行之后添加了一个 if 语句:
if(SINGLE_MODE)
SET(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} " -lglapi")
SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} " -lglapi")
endif(SINGLE_MODE)
The SINGLE_MODE variable is defined a little up. When I use the message command to display the content of the flag variables it looks alright:
SINGLE_MODE 变量的定义稍微高一些。当我使用 message 命令显示标志变量的内容时,它看起来没问题:
-O3 -xSSE3 -restrict -lpthread -lX11 -ldrm -lglapi
But when I start compiling I am running into a compile error. Using the verbose mode I realized that in the compiler call it looks like that:
但是当我开始编译时,我遇到了编译错误。使用详细模式我意识到在编译器调用中它看起来像这样:
-O3 -xSSE3 -restrict -lpthread -lX11 -ldrm; -lglapi
I.e. somehow a semicolon got added before adding the -lglapi to the list.
即在将 -lglapi 添加到列表之前以某种方式添加了分号。
Did anyone here encounter a similar issue and knows a way to fix this issue? I've googled quite a while and studied the CMake manual but couldn't see what I did wrong here.
这里有没有人遇到过类似的问题并且知道解决这个问题的方法?我已经用谷歌搜索了一段时间并研究了 CMake 手册,但看不到我在这里做错了什么。
Thanks, Tobias
谢谢,托比亚斯
回答by jpo38
Try to do this instead:
尝试这样做:
if(SINGLE_MODE)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -lglapi")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lglapi")
endif(SINGLE_MODE)
Then, you are sure you append -lglapi
to the existing ${CMAKE_CXX_FLAGS}
string. Else, looks like something like a CMake list is being created.
然后,您确定附加-lglapi
到现有${CMAKE_CXX_FLAGS}
字符串。否则,看起来像正在创建 CMake 列表。
回答by Benoit Blanchon
Since CMake 3.4you do:
从CMake 3.4 开始,您可以:
string(APPEND CMAKE_CXX_FLAGS " -lglapi")
This very handy when you want to set the flags only for one language (C++ in the example above), but if you want to set the same flags for all languages, you can simply do:
当您只想为一种语言(上例中的 C++)设置标志时,这非常方便,但如果您想为所有语言设置相同的标志,您可以简单地执行以下操作:
add_compile_options(-lglapi)
Both commands change the flags for the whole directory, if you want to set the flags for only one target, do:
这两个命令都会更改整个目录的标志,如果您只想为一个目标设置标志,请执行以下操作:
target_compile_options(my_lib PUBLIC -lglapi)
Flags on a target can either be PUBLIC, PRIVATE or INTERFACE, allowing to transitively forward the flags from one target to the other.
目标上的标志可以是PUBLIC、PRIVATE 或 INTERFACE,允许将标志从一个目标传递到另一个目标。