更改CMake中的CMAKE_CXX_FLAGS_DEBUG和朋友的默认值 [英] Change default value of CMAKE_CXX_FLAGS_DEBUG and friends in CMake

查看:2777
本文介绍了更改CMake中的CMAKE_CXX_FLAGS_DEBUG和朋友的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改CMake中 CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG 的默认值。基本上,我有一些项目默认值,只是稍微不同于CMake的(例如发布),我不应该问自己哦,当他们的-O3或我们-O2优先,添加add_compile_options / p>

现在,我知道如何设置这些值,但我不知道如何使他们用户可编辑的两种通常的方式:使用 - DCMAKE_CXX_FLAGS_DEBUG = yourflags 在命令行或通过配置ccmake或CMakeSetup。



问题是CMAKE设置和缓存自己的默认值对于这些,如果您尝试覆盖变量而不使用FORCE,默认值永远不会更改。如果我在我的设置命令中使用FORCE: set(CMAKE_CXX_FLAGS_DEBUG blah CACHE STRINGFORCE),它将在每次脚本运行时覆盖它,



我设法通过执行以下操作来破解它与CCMAKE一起工作,但是这仍然不能与 cmake -DCMAKE_CXX_FLAGS_DEBUG ,因为它完成后会覆盖用户更改:

 设置(DEFAULTS_SET FALSE CACHE BOOL)
set(CMAKE_CXX_FLAGS_DEBUG-this - CACHE STRINGFORCE)
set(DEFAULTS_SET TRUE CACHE BOOLFORCE)
pre>

显然,这是一个讨厌的黑客,并不完全工作(在cmake -Dwhatever = thisorthat的情况下)。我可以添加其他的构建类型,但我不知道为什么只是需要改变一些简单的事情。



编辑3月1日,2015:



我创建了一个有效的解决方案,虽然我对我的工作还是不满意。我看到其他的意见,解决了设置 CMAKE_CXX_FLAGS_DEBUG 和朋友的问题,而没有让他们被破坏的问题,但是这本来是没有为我工作,因为我也试图选择它们基于正在使用的编译器。然而,编译器没有确定,直到它已经填充我的变量。我使用的技巧如下。您必须在项目命令之前将flags变量设置为特殊的。

  set(CMAKE_CXX_FLAGS_DEBUG_UNSETCACHE STRING )
项目(your_project C CXX)

if($ {CMAKE_CXX_FLAGS_DEBUG} STREQUAL_UNSET)
#在这里做一些编译器切换,然后用FORCE设置你的标志。
set(CMAKE_CXX_FLAGS_DEBUG-ggdb3 -O0CACHE STRINGFORCE)
endif()

现在,我可以选择默认值,它们可以通过-D或者cmake-gui中的命令行完全覆盖。

解决方案

我只想添加我看到的四种可能:


  1. 拥有自己的包含预设的工具链文件您支持的每个编译器都是:



    GNUToolchain.cmake

     code> set(CMAKE_CXX_FLAGS_DEBUG-ggdb3 -O0CACHE STRING)

    使用

      cmake -DCMAKE_TOOLCHAIN_FILE:string = GNUToolchain.cmake ... 


  2. 您可以尝试通过检查 CMAKE_GENERATOR (在 project() 命令):



    CMakeLists.txt

      if($ {CMAKE_GENERATOR} MATCHESMakefilesOR 
    ($ {CMAKE_GENERATOR}MATCHESNinjaAND NOT WIN32))
    set(CMAKE_CXX_FLAGS_DEBUG-ggdb3 -O0CACHE STRING)
    endif

    项目(your_project C CXX)


  3. a href =https://cmake.org/cmake/help/v3.3/variable/CMAKE_USER_MAKE_RULES_OVERRIDE.html =nofollow> CMAKE_USER_MAKE_RULES_OVERRIDE 给出具有您自己的 ..._ INIT 值的脚本:


    加载后CMake的内置编译器和平台信息模块已加载,但在使用信息之前。该文件可以设置平台信息变量以覆盖CMake的默认值。


    MyInitFlags.cmake b
    $ b #覆盖由CMake
    选择的初始值if(CMAKE_COMPILER_IS_GNUCXX)
    set(CMAKE_CXX_FLAGS_DEBUG_INIT-ggdb3 -O0)
    endif()

CMakeLists.txt b
$ b

 设置(CMAKE_USER_MAKE_RULES_OVERRIDEMyInitFlags.cmake)

项目(your_project C CXX)


  • 您可以从3月1日起通过检查 ..._ INIT 编译器标记变量的变体:



    CMakeLists.txt

     项目(your_project C CXX)

    if(DEFINED CMAKE_CXX_FLAGS_DEBUG_INIT AND
    $ {CMAKE_CXX_FLAGS_DEBUG_INIT}STREQUAL$ {CMAKE_CXX_FLAGS_DEBUG})
    #覆盖由CMake
    选择的初始化值if(CMAKE_COMPILER_IS_GNUCXX)
    set(CMAKE_CXX_FLAGS_DEBUG-ggdb3 -O0CACHE STRINGFORCE)
    endif()
    endif b $ b


  • / p>

    我更喜欢使用工具链变体。但我承认它的缺点是必须手动提供工具链文件(如果你不通过脚本/批处理文件调用 cmake )。



    参考




    I would like to change the default values for CMAKE_CXX_FLAGS_RELEASE or CMAKE_CXX_FLAGS_DEBUG in CMake. Basically, I have some project defaults that differ just slightly from CMake's (for release, for instance), and I shouldn't have to ask myself "Oh, does their -O3 or our -O2 take precedence when added with add_compile_options."

    Now, I know how to set these values, but I don't know how to make them user editable in the two usual ways: by using -DCMAKE_CXX_FLAGS_DEBUG=yourflags on the command line or by configuring it with ccmake or CMakeSetup.

    The problem being that CMAKE sets and caches its own defaults for these, and if you try to overwrite the variables without using FORCE, the "defaults" are never changed. If I use FORCE in my set command: set(CMAKE_CXX_FLAGS_DEBUG blah CACHE STRING "" FORCE), it will overwrite it every time the script is run, eliminating the possibility for the user to change it if he wishes.

    I managed to hack it to work with CCMAKE by doing the following, but this still doesn't work with cmake -DCMAKE_CXX_FLAGS_DEBUG as it overwrites the user change AFTER he's done it:

    set(DEFAULTS_SET FALSE CACHE BOOL "")
    set(CMAKE_CXX_FLAGS_DEBUG "-this -that" CACHE STRING "" FORCE)
    set(DEFAULTS_SET TRUE CACHE BOOL "" FORCE)
    

    Obviously, this is a nasty hack and doesn't completely work(in the case of cmake -Dwhatever=thisorthat). I could add other build types as well, but I don't really see why that should be necessary just to change a few simple things.

    Edit March 1st, 2015:

    I've created a solution that works, though I still am not super thrilled about what I have to do. I'd seen other comments that solve the problem of setting CMAKE_CXX_FLAGS_DEBUG and friends without having them get clobbered, but this originally didn't work for me because I was trying to also select them based on the compiler that was in use. The compiler isn't determined, however, until it has already filled the variables for me. The trick I used is as follows. You must set the flags variables to something 'special' before the project command.

    set(CMAKE_CXX_FLAGS_DEBUG "_UNSET" CACHE STRING "")
    project(your_project C CXX)
    
    if(${CMAKE_CXX_FLAGS_DEBUG} STREQUAL "_UNSET")
        # Do some compiler switching here and then set your flags with FORCE.
        set(CMAKE_CXX_FLAGS_DEBUG "-ggdb3 -O0" CACHE STRING "" FORCE)
    endif()
    

    This now allows me to choose defaults that are completely override-able via command line with -D or in cmake-gui.

    解决方案

    I just wanted to add the four possibilities I see:

    1. Having your own toolchain files containing the presets for each compiler you support like:

      GNUToolchain.cmake

      set(CMAKE_CXX_FLAGS_DEBUG "-ggdb3 -O0" CACHE STRING "")
      

      And then use it with

      cmake -DCMAKE_TOOLCHAIN_FILE:string=GNUToolchain.cmake ...
      

    2. You can try to determine the compiler by checking CMAKE_GENERATOR (which is valid before the project() command):

      CMakeLists.txt

      if("${CMAKE_GENERATOR}" MATCHES "Makefiles" OR 
         ("${CMAKE_GENERATOR}" MATCHES "Ninja" AND NOT WIN32))
          set(CMAKE_CXX_FLAGS_DEBUG "-ggdb3 -O0" CACHE STRING "")
      endif()
      
      project(your_project C CXX)
      

    3. You can use CMAKE_USER_MAKE_RULES_OVERRIDE to give a script with your own ..._INIT values:

      It is loaded after CMake’s builtin compiler and platform information modules have been loaded but before the information is used. The file may set platform information variables to override CMake’s defaults.

      MyInitFlags.cmake

      # Overwrite the init values choosen by CMake
      if (CMAKE_COMPILER_IS_GNUCXX)
          set(CMAKE_CXX_FLAGS_DEBUG_INIT "-ggdb3 -O0")
      endif()
      

      CMakeLists.txt

      set(CMAKE_USER_MAKE_RULES_OVERRIDE "MyInitFlags.cmake")
      
      project(your_project C CXX)
      

    4. You can simplify your solution from March 1st by checking against the ..._INIT variants of the compiler flag variables:

      CMakeLists.txt

      project(your_project C CXX)
      
      if (DEFINED CMAKE_CXX_FLAGS_DEBUG_INIT AND  
          "${CMAKE_CXX_FLAGS_DEBUG_INIT}" STREQUAL "${CMAKE_CXX_FLAGS_DEBUG}")
          # Overwrite the init values choosen by CMake
          if (CMAKE_COMPILER_IS_GNUCXX)
              set(CMAKE_CXX_FLAGS_DEBUG "-ggdb3 -O0" CACHE STRING "" FORCE)
          endif()
      endif()
      

    Comments:

    I prefer and use the toolchain variant. But I admit it has the disadvantage of having to give the toolchain file manually (if you are not calling cmake via a script/batch file).

    References:

    这篇关于更改CMake中的CMAKE_CXX_FLAGS_DEBUG和朋友的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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