对于Cmake,可以使用`add_compiler_flags()`命令修改发布/调试编译器标志吗? [英] For Cmake, can you modify the release/debug compiler flags with `add_compiler_flags()` command?

查看:134
本文介绍了对于Cmake,可以使用`add_compiler_flags()`命令修改发布/调试编译器标志吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

add_compile_options()的手册页中,我没有提到如何修改Release / Debug编译器标志。 可以使用 add_compiler_options()来修改Release / Debug编译器标志吗?如果是,怎么办?



如果不是,建议的规范方法是修改发布/调试cmake变量[1],如此处所述



[1]
,即设置cmake变量CMAKE_< LANG> _FLAGS_< TYPE> (对于lang c / c ++,它将是:CMAKE_CXX_FLAGS_RELEASE,CMAKE_CXX_FLAGS_DEBUG,CMAKE_C_FLAGS_RELEASE,CMAKE_C_FLAGS_DEBUG)。

解决方案

通过几个项目的编译器设置,或者需要在C和C ++之间进行区分,我建议使用 CMAKE_C_FLAGS / CMAKE_CXX_FLAGS 工具链文件的$ c>变体对于每个受支持的编译器(请参见例如此处此处 )。



但是如果您只需要在项目中使用一些其他C ++编译器选项,请使用 add_compile_options() target_compile_options() target_compile_features() 是一种解决方法。



是的,您可以在其中区分 DEBUG RELEASE



示例


  1. The add_compile_options()命令确实需要发电机表达式

      add_compile_options( $< $< CONFIG:DEBUG>:/ MDd>)

      add_compile_options(
    $ < $< CONFIG:RELEASE> ;: -std = gnu99>
    $< $< CONFIG:DEBUG>:-std = gnu99 -g3>


  2. 更好地检查编译器ID:

      add_compile_options( $< $< AND:$< CXX_COMPILER_ID:MSVC>,$< CONFIG:DEBUG>>:/ MDd>)

      if(MSVC )
    add_compile_options( $< $< CONFIG:DEBUG>:/ MDd>)
    endif()


  3. 最好让CMake决定为您提供正确的编译器选项。因此,您可以设置 目标所需的CXX_STANDARD


      set_property(目标tgt属性CXX_STANDARD 11)


    或输入编译器功能您的目标需要 target_compile_features()


      add_library(mylib require_constexpr.cpp)
    #cxx_constexpr是一个使用需求
    target_compile_features(mylib PUBLIC cxx_constexpr)



参考




  • CMake CMAKE_CXX_FLAGS启用了优化

  • 在CMake中更改CMAKE_CXX_FLAGS_DEBUG和朋友的默认值

  • CMake生成器表达式,区分C / C ++代码


  • In the man page for add_compile_options() I don't see any mention of how to modify the Release/Debug compiler flags. Can you use add_compiler_options() to modify the Release/Debug compiler flags? If yes, how?

    If no, is the recommended canonical method to modify the release/debug cmake variables[1] as described here ?

    [1] i.e. set the cmake variables CMAKE_<LANG>_FLAGS_<TYPE> (for lang c/c++ it would be: CMAKE_CXX_FLAGS_RELEASE, CMAKE_CXX_FLAGS_DEBUG, CMAKE_C_FLAGS_RELEASE, CMAKE_C_FLAGS_DEBUG).

    解决方案

    If you want to reuse your compiler settings through several of your projects or you need to differentiate the compiler options between C and C++, I would recommend the CMAKE_C_FLAGS/CMAKE_CXX_FLAGS variant with a toolchain file for each of your supported compilers (see e.g. here or here).

    But if you just need some additional C++ compiler options in your project, taking add_compile_options(), target_compile_options() or target_compile_features() is the way to go.

    And yes, you can differentiate between DEBUG and RELEASE there.

    Examples

    1. The add_compile_options() command does take generator expressions:

      add_compile_options("$<$<CONFIG:DEBUG>:/MDd>")
      

      or

      add_compile_options(
          "$<$<CONFIG:RELEASE>:-std=gnu99>"
          "$<$<CONFIG:DEBUG>:-std=gnu99 -g3>"
      )
      

    2. Better to check also the compiler id:

      add_compile_options("$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<CONFIG:DEBUG>>:/MDd>")
      

      or

      if (MSVC)
          add_compile_options("$<$<CONFIG:DEBUG>:/MDd>")
      endif()
      

    3. Even better to let CMake decide the correct compiler options for you. So you can either set the CXX_STANDARD needed for your target:

      set_property(TARGET tgt PROPERTY CXX_STANDARD 11)
      

      or give the compiler feature your target needs with target_compile_features()

      add_library(mylib requires_constexpr.cpp)
       # cxx_constexpr is a usage-requirement
       target_compile_features(mylib PUBLIC cxx_constexpr)
      

    References

    这篇关于对于Cmake,可以使用`add_compiler_flags()`命令修改发布/调试编译器标志吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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