在项目中更改CMAKE_CXX_FLAGS [英] Changing CMAKE_CXX_FLAGS in project
问题描述
我在我的CMakeLists.txt中有以下内容:
项目(Matfile)
SET(CMAKE_CXX_FLAGS-std = c ++ 0x)
set(SOURCES
foo.cpp
bar.cpp
)
add_library(
Matfile
$ {SOURCES}
)
$ b b
你可以想象,我想做的是使用标志-std = c ++ 0x(我使用gcc和我需要C ++ 11功能)编译我的C ++源。不幸的是,这不工作,在我使用cmake生成makefiles的意义上,变量CMAKE_CXX_FLAGS是完全无效的。
如何设置此变量项目文件?
这似乎是一个非常愚蠢的问题,但我只花了不少于两个houres试图找出这一点。
最直接的解决方案应该是使用 add_compile_options 如果您使用的是2.8.12或更高版本。
对于较旧版本,您可以滥用 add_definitions()
。虽然它只是意味着添加-D标志,它也适用于任何其他编译器标志。
add_compile_options(-std = c ++ 0x)#CMake 2.8.12或更新的
或
add_definitions(-std = c ++ 0x)#CMake 2.8.11或更早版本
从CMake 3.3开始,您也可以使用奇怪的生成器表达式语法使此标志仅应用于特定语言(例如,仅C或C ++):
<$ c $ p>
add_compile_options($< $< COMPILE_LANGUAGE:CXX>: - std = c ++ 14> $< $< COMPILE_LANGUAGE:C& = c99>)
但这将不能使用Visual studio generator ,因此请务必仅在Make / Ninja生成器中使用它,或使用 target_compile_options()在每个目标范围上进行设置。
I have the following content in my CMakeLists.txt:
project( Matfile )
SET ( CMAKE_CXX_FLAGS "-std=c++0x" )
set ( SOURCES
"foo.cpp"
"bar.cpp"
)
add_library(
Matfile
${SOURCES}
)
As you may imagine, what I want to do is to compile my C++ sources using the flag -std=c++0x (I'm using gcc and I need the C++11 features). Unfortunately, this does not work, in the sense that, when I use cmake to generate the makefiles, the variable CMAKE_CXX_FLAGS is completely void.
How can I set this variable in the project file?
It seems to be a very stupid question, but I just spent not less than two houres trying to figure this out.
The most straightforward solution should be using add_compile_options if you are using version 2.8.12 or newer.
For older versions you can "abuse" add_definitions()
. While it is only meant for add -D flags, it also works with any other compiler flag. However, I think it is not meant to be used that way and could break in a future version.
add_compile_options(-std=c++0x) # CMake 2.8.12 or newer
or
add_definitions(-std=c++0x) # CMake 2.8.11 or older
Starting with CMake 3.3 you can also make this flag only apply to a specific language (e.g. only C or C++) using the strange generator expressions syntax:
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-std=c++14> $<$<COMPILE_LANGUAGE:C>:-std=c99>)
However this will not work with the Visual studio generator, so make sure to only use this for Make/Ninja generators or use target_compile_options() to set it on a per-target scope.
这篇关于在项目中更改CMAKE_CXX_FLAGS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!