English 中文(简体)
Does CM CMAKE_CXX_FLAGS?
原标题:Does set_target_properties in CMake override CMAKE_CXX_FLAGS?

在我的CMake项目开始时,Im在MCMAKE_CXX_FLAGS等变量中设定了一般汇编旗帜。

set(CMAKE_CXX_FLAGS "-W -Wall ${CMAKE_CXX_FLAGS}")

稍后,我需要补充具体组合的汇编旗帜(储存在BUILD_FLAGS)。 Can 我为此使用了以下指挥:

set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})

或者,我不得不人工添加CMAKE_CXX_FLAGS:

set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BUILD_FLAGS}")

预防 CMAKE_CXX_FLAGS BUILD -FLAGS

最佳回答

第一种:

set_target_properties(${TARGET} PROPERTIES CompILE_FLAGS ${BUILD_FLAGS})

注册号为FLAGS的旗帜在CMAKE_CXX_FLAGS汇编TARGET来源后附。 这些文件是背后的,但我只是试图确保。

CompILE_FLAGS

   Additional flags to use when compiling this target s sources. 
   
   The CompILE_FLAGS property sets additional compiler flags used to
   build sources within the target.  Use COMPILE_DEFINITIONS to
   pass additional preprocessor definitions.

整个指挥线将相当于:

${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} ${CompILE_FLAGS} -o foo.o -c foo.cc

正如Ramon所说,你总是可以检查make VERBOSE=1

问题回答

The accepted answer is still working but outdated since 2013.
This answer is based and new functions from CMake v2.8.12, v3.3 and v3.13.

Since CMake-2.8.12 (2013)

设置<条码>CMAKE_CXX_FLAGS的两个新指挥部:

最新版本的文件自cmake-2.8.12以来没有变化:

您可以使用:

target_compile_options(${TARGET} PRIVATE ${BUILD_FLAGS})

或者简单地说,如果你有一个目标:

add_compile_options(${BUILD_FLAGS})

More examples

target_compile_options(mylib PRIVATE   -O2) # only internal
target_compile_options(mylib INTERFACE -gl) # only external
target_compile_options(mylib PUBLIC    -g)  # same as PRIVATE + INTERFACE

# multiple targets and flags
target_compile_options(mylib1 mylib2 PRIVATE -Wall -Wextra)

target_compile_options(    mylib PUBLIC -DUSEXX)  # Bad
target_compile_definitions(mylib PUBLIC -DUSEXX)  # OK

add_compile_options(-Wall -Wextra) # for all targets in current directory
add_compile_options(-DUSEXX)       # Bad
add_definitions(-DUSEXX)           # OK

Deprecated COMPILE_FLAGS

cmake-3.0document signsCOMPILE_FLAGS deprecated:

COMPILE_FLAGS

在汇编这一目标来源时,还要使用更多的旗帜。

The COMPILE_FLAGS property sets additional compiler flags used to build sources within the target. Use COMPILE_DEFINITIONS to pass additional preprocessor definitions.

This property is deprecated. Use the COMPILE_OPTIONS property or the target_compile_options command instead.

如果您仍然希望使用<代码>_target_properties()。 您可使用<条码>。

set_target_properties(${TARGET} PROPERTIES COMPILE_OPTIONS ${BUILD_FLAGS})

Since CMake-3.3 (2015)

rel=“noreferer”>>,如在上所示。

CMake generator expression 适用于${BUILD_FLAGS}/code>:

您可以使用:

target_compile_options(${TARGET} PRIVATE
          $<$<COMPILE_LANGUAGE:CXX>:${BUILD_FLAGS_FOR_CXX}>
          $<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>)

或大约汇编者:

target_compile_options(${TARGET} PRIVATE
          $<$<CXX_COMPILER_ID:Clang>:${BUILD_FLAGS_FOR_CLANG}>
          $<$<CXX_COMPILER_ID:GNU>:${BUILD_FLAGS_FOR_GCC}>
          $<$<CXX_COMPILER_ID:MSVC>:${BUILD_FLAGS_FOR_VISUAL}>)

Since CMake-3.13 (2018)

新的职能target_link_options (

Different options for C and C++ files

最好的办法是利用两个不同的目标来区分C文档和C++文档。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?