CMake 中的 set_target_properties 是否会覆盖 CMAKE_CXX_FLAGS?

发布于 2024-10-19 01:55:47 字数 495 浏览 2 评论 0原文

在我的 CMake 项目开始时,我在变量 CMAKE_CXX_FLAGS 中设置通用编译标志,稍后

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

,我需要附加其他特定于配置的编译标志(存储在 BUILD_FLAGS 中)。我可以使用以下命令来实现此目的:

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 覆盖?

At the beginning of my CMake project, I'm setting general compilation flags in the variable CMAKE_CXX_FLAGS, like

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

Later on, I need to append additional configuration-specific compilation flags (stored in BUILD_FLAGS). Can I use the following command for this:

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

or do I have to add the CMAKE_CXX_FLAGS manually:

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

to prevent CMAKE_CXX_FLAGS being overriden by BUILD_FLAGS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜妞爱困 2024-10-26 01:55:47

已接受的答案仍然有效,但自 2013 年以来已过时。
此答案基于 CMake v2.8.12、v3.3 和 v3.13 中的新函数。

自 CMake-2.8.12 (2013) 以来,

有两个用于设置 CMAKE_CXX_FLAGS 的新命令:

cmake-2.8.12以来,上一个版本的文档没有太大变化:

在您的情况下,您可以使用:

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

或者如果您只有一个目标:

add_compile_options(${BUILD_FLAGS})

更多示例

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

已弃用 COMPILE_FLAGS

cmake-3.0 文档COMPILE_FLAGS 标记为已弃用:

COMPILE_FLAGS

编译此目标的源代码时要使用的其他标志。

COMPILE_FLAGS 属性设置用于
在目标内构建源。使用COMPILE_DEFINITIONS来传递
其他预处理器定义。

此属性已弃用。使用 COMPILE_OPTIONS 属性或
改为使用 target_compile_options 命令。

如果您仍然想使用 set_target_properties(),您可以使用 COMPILE_OPTIONS 而不是 COMPILE_FLAGS

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

自 CMake-3.3 (2015)

Anton Petrov 建议使用 生成器表达式,如 ar31 的答案< /a>.

CMake 生成器表达式将您的 ${BUILD_FLAGS} 应用到:

  • 使用 $ 的 C++ 语言(也可以是 CCUDA...)
  • 使用 $
    的 Clang 编译器
    (也可以是 gccGNU,或者 Visual C++ 的 MSVC...请参阅 完整列表)
    (如果语言是 C,则使用 $ 代替)
  • 以及更多支持的 C++ 功能编译器版本...(请参阅文档

在您的情况下,您可以使用:

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

或关于编译器:

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

自 CMake-3.13 (2018) 起

新函数 target_link_options() 允许将选项传递给链接器,如Craig Scott所述。

C 和 C++ 文件的不同选项

最好的方法是使用两个不同的目标来区分 C 文件和 C++ 文件。

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)

Two new commands to set CMAKE_CXX_FLAGS:

The documentation of last version has not changed a lot since cmake-2.8.12:

In you case you can use:

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

Or simply if you have a single target:

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.0 documentation flags COMPILE_FLAGS as deprecated:

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.

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

If you still want to use set_target_properties() you may use COMPILE_OPTIONS instead of COMPILE_FLAGS:

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

Since CMake-3.3 (2015)

Anton Petrov suggests to use generator expressions as presented in an answer of ar31.

The CMake generator expressions applies your ${BUILD_FLAGS} to:

  • C++ language using $<COMPILE_LANGUAGE:CXX> (can also be C, CUDA...)
  • Clang compiler using $<CXX_COMPILER_ID:Clang>
    (can also be GNU for gcc, or MSVCfor Visual C++... see full list)
    (use $<C_COMPILER_ID:Clang> instead if language is C)
  • and more as supported C++ feature or compiler version... (see documentation)

In you case you can use:

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

or about compilers:

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

Since CMake-3.13 (2018)

A new function target_link_options() allow to pass options to the linker, as mentioned by Craig Scott.

Different options for C and C++ files

The best way is to distinguish C files and C++ files using two different targets.

初见你 2024-10-26 01:55:47

使用第一个:

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

编译 TARGET 源代码时,BUILD_FLAGS 中存储的标志将附加在 CMAKE_CXX_FLAGS 之后。文档暗示了这一点,但我刚刚尝试过以确保这一点。

COMPILE_FLAGS

 编译此目标源代码时使用的附加标志。 
   
   COMPILE_FLAGS 属性设置其他编译器标志,用于
   在目标内构建源。使用 COMPILE_DEFINITIONS 来
   传递额外的预处理器定义。

完整的命令行相当于:

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

正如 Ramon 所说,您可以随时使用 make VERBOSE=1 进行检查。

Use the first one:

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

The flags stored in BUILD_FLAGS are appended after CMAKE_CXX_FLAGS when compiling the sources of TARGET. The documentation hints at this, but I've just tried it to make sure.

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.

The full command line will be the equivalent of:

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

And as Ramon said, you can always check with make VERBOSE=1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文