特定于本地工作副本的CMake编译器设置 [英] CMake compiler settings specific to the local working copy

查看:82
本文介绍了特定于本地工作副本的CMake编译器设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前要在使用gcc时更改编译器标志,我为构建目标编辑CMakeLists.txt:

  if( UNIX)
add_definitions(-Wall)
add_definitions(-g)
#add_definitions(-O2)
endif(UNIX)

问题是git接受了更改。如果我继续进行此更改,我将使其他希望使用-O2而不是-g的开发人员感到恼火,但是当他们进行无关的更改时,他们会得到我的版本。通常,我可以从提交中排除此更改,但是当我对CMakeLists.txt文件进行 actual 更改时,无法避免推高我对编译标志的选择。 p>

是否可以告诉CMake在build /目录中创建文件(特定于每个工作副本,因此也适用于每个开发人员),每个人都可以对其进行修改内心的渴望而无需触碰项目文件(除了build /以外的所有东西)。自然,我们的build /不会提交到git存储库。



可能需要注意的是,当使用Visual Studio而不是gcc时,IDE会通过以下方式为我们处理此问题其用户界面可修改build /中的VS解决方案文件。问题是使用GNU Makefile时我们没有这种机制。



我们的项目的组织方式如下:

  ourproject / 
bin /
build /<-CMake生成的东西在这里
lib /
src /
abuildtarget /
anotherbuildtarget /
source.cpp
source.h
CMakeLists.txt


解决方案

您在此处错误地使用了CMake。 add_definitions 函数不能像您所做的那样添加编译器选项。相反,它是添加预处理器定义,例如 add_definitions(-DDEBUG)



您想要的内容配置为所需选项时,请设置 CMAKE_< language&_; FLAGS 。如果需要标准设置,则将其放在CMakeLists.txt文件中,例如:

  if($ {CMAKE_Fortran_COMPILER_ID } STREQUAL Intel)
set(CMAKE_Fortran_FLAGS_RELEASE -O2 -xhost CACHE STRING FORCE)

set(CMAKE_Fortran_FLAGS_NODEBUG -O0 CACHE STRING FORCE)
mark_as_advanced(CMAKE_Fortran_FLAGS_NODEBUG)

set(CMAKE_Fortran_FLAGS_PROFILING -O2 -xhost -p CACHE STRING FORCE)
mark_as_advanced(CMAKE_Fortran_FLAGS_PROFILING)

$ b -DDEBUG -g -check noarg_temp_created -C -traceback缓存字符串强制)
endif()

其中 Fortran 可以用 CXX C代替



在这种情况下, CMAKE_< language> _FLAGS_<构建类型> 设置标记基于 CMAKE_BUILD_TYPE 变量。如果将其设置为发行版,则它使用 CMAKE_Fortran_FLAGS_RELEASE 。我们添加了其他几种可能的构建类型。如果用户想要的不是标准构建类型之一,则可以在配置时将 CMAKE_< language> _FLAGS 设置为他们想要的任何值,它会覆盖构建类型设置并改用用户定义的标志。


Currently to change a compiler flag when using gcc, I edit the CMakeLists.txt for the build target:

if (UNIX)
    add_definitions(-Wall)
    add_definitions(-g)
    #add_definitions(-O2)
endif (UNIX)

The problem with this is that git picks up the change. If I go ahead and commit this change, I will annoy the other developers who expect to use -O2 instead of -g but they get my version when they pull unrelated changes. Normally, I could just exclude this change from my commits, but when I make an actual change to the CMakeLists.txt file, there is no way to avoid pushing up my personal selection of compile flags.

Is there a way to tell CMake to create a file in the build/ directory (specific to each working copy, and therefore to each developer) that an individual person can modify to their heart's desire without touching project files (everything but build/). Naturally, our build/ is not committed to the git repository.

It might be helpful to note that when using Visual Studio instead of gcc, the IDE handles this for us through its UI which modifies the VS solution file in build/. The problem is that we have no such mechanism when using GNU Makefiles.

Our project is organized like this:

ourproject/
    bin/
    build/ <-- CMake-generated stuff goes here
    lib/
    src/
        abuildtarget/
        anotherbuildtarget/
            source.cpp
            source.h
            CMakeLists.txt

解决方案

You are using CMake incorrectly here. The add_definitions function is not for adding compiler options like you are doing; rather, it is to add pre-processor definitions such as add_definitions(-DDEBUG).

What you want to do is set the CMAKE_<language>_FLAGS when you configure to the options you want. If there is a standard set you need, then put that in the CMakeLists.txt file such as:

if(${CMAKE_Fortran_COMPILER_ID} STREQUAL "Intel")                                   
  set(CMAKE_Fortran_FLAGS_RELEASE "-O2 -xhost" CACHE STRING "" FORCE)

  set(CMAKE_Fortran_FLAGS_NODEBUG "-O0" CACHE STRING "" FORCE)
  mark_as_advanced(CMAKE_Fortran_FLAGS_NODEBUG)

  set(CMAKE_Fortran_FLAGS_PROFILING "-O2 -xhost -p" CACHE STRING "" FORCE)
  mark_as_advanced(CMAKE_Fortran_FLAGS_PROFILING)

  set(CMAKE_Fortran_FLAGS_DEBUG
      "-DDEBUG -g -check noarg_temp_created -C -traceback" CACHE STRING "" FORCE)
endif()

Where Fortran can be replaced by CXX or C.

In this case, the CMAKE_<language>_FLAGS_<build type> sets the flags based on the CMAKE_BUILD_TYPE variable. If it is set to Release, then it uses CMAKE_Fortran_FLAGS_RELEASE. We added several other possible build types. If the user wants something that isn't one of the standard build types, then they set CMAKE_<language>_FLAGS to whatever they want when configuring and it overrides the build type setting and uses the user-defined flags instead.

这篇关于特定于本地工作副本的CMake编译器设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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