[CMake] Appending to CMAKE_CXX_FLAGS

Thompson, KT kgt at lanl.gov
Tue Sep 11 17:27:54 EDT 2018


Michael,

I agree heartily with your frustration concerning how CMake handles compiler flags -- especially when the FLAG strings seen in cmake-gui don't match the baseline flags used by the build.  I use a heavy handed, old-fashioned approach to solve this problem.

I ignore all of CMake's default compiler flags.  Instead of using CMake's defaults, I set these flags manually for every supported build environment. For example, I have a setflags.cmake file included from my top level CMakeLists.txt that contains logic that looks something like this:

if( NOT C_FLAGS_INITIALIZED )
  # only do this on the first pass through to avoid overwriting user added options.
  set( C_FLAGS_INITIALIZED "yes" CACHE INTERNAL "Are compiler flags already set?" )

  # Overwrite CMake's defaults...
  if( "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
    set( CMAKE_C_FLAGS                "-Wall -pedantic -Wunused-macros" )
    set( CMAKE_C_FLAGS_DEBUG          "-g -DDEBUG")
    set( CMAKE_C_FLAGS_RELEASE        "-O3 -DNDEBUG" )
    set( CMAKE_C_FLAGS_MINSIZEREL     "${CMAKE_C_FLAGS_RELEASE}" )
    set( CMAKE_C_FLAGS_RELWITHDEBINFO "-O3 -g -gdwarf-3" )
    # in a similar fashion, provide CXX_FLAGS and Fortran_FLAGS

  elseif( "${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC" )
    set( CMAKE_C_FLAGS "/fp:precise /DWIN32 /D_WINDOWS /MP" )
    ...
  endif()
endif()

# Save the current compiler flags to the cache every time cmake configures the project.
set(CMAKE_C_FLAGS                "${CMAKE_C_FLAGS}"                CACHE
     STRING "compiler flags" FORCE)
set(CMAKE_C_FLAGS_DEBUG          "${CMAKE_C_FLAGS_DEBUG}"          CACHE
     STRING "compiler flags" FORCE)
set(CMAKE_C_FLAGS_RELEASE        "${CMAKE_C_FLAGS_RELEASE}"        CACHE
     STRING "compiler flags" FORCE)
set(CMAKE_C_FLAGS_MINSIZEREL     "${CMAKE_C_FLAGS_MINSIZEREL}"     CACHE
     STRING "compiler flags" FORCE)
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}" CACHE
     STRING "compiler flags" FORCE)
# and similar for CXX_FLAGS and Fortran_FLAGS...

With this logic, I have customized baseline compiler flags that show up for everyone and they match the strings found in CMakeCache.txt (and via cmake-gui).  If I modify the flags via ccmake or cmake-gui, the new options are saved to the cache and are used by the build.  

I manually set many more flags for my default set than I show in the example above and I have extra logic to check compiler version or option availability (e.g.: if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 8.0) -> add some gcc-8 flags).  I also have some logic that looks for flags set in the environment and these options are appended to these strings before the strings are saved to the cache.  

There are a few downsides to this approach:
- I build on many different platforms with many different compilers, so I have a significant amount of cmake code to manage.
- Some flags that are set automagically set by cmake don't show up in the _FLAGS strings.  For example, setting CMAKE_CXX_STANDARD will add appropriate compiler flags like '-std=c++14' but you don't see them in cmake-gui. 

In a few cases I also use per-sourcefile, per-target, and per-directory properties (sometimes via generator expressions) as other have described in replying to your question.  In most cases, I have found that the default flags that are set by the top level of my build system prevent me from needing to customize compile flags for individual targets.

I hope this helps.

-kt

-----Original Message-----
From: CMake <cmake-bounces at cmake.org> On Behalf Of Michael Jackson
Sent: Tuesday, September 11, 2018 2:10 PM
To: cmake at cmake.org
Subject: Re: [CMake] Appending to CMAKE_CXX_FLAGS

I add it manually each and every time. I have to tell all new developers to remember to add the flag otherwise they are still sitting after an hour waiting on our code to compile wondering why it takes so long. Then it hits us, "Oh, Yeah. Open CMake-Gui and set the /MP flag". I'm frustrated at the situation but not sure how to fix it. I tried the other suggestions and just nothing works. This is one of those things that I poke at once a year and figure out that nothing has changed. Been this way since VS 2013. Someday it will change.

--
Mike Jackson 

On 9/11/18, 1:28 PM, "CMake on behalf of Innokentiy Alaytsev" <cmake-bounces at cmake.org on behalf of alaitsev at gmail.com> wrote:

    Hello!
    
    Did you consider adding the flag manually during project configuration? I do
    not know you use case, but after some thinking about the best way of
    achieving multiprocess compilation under MSVS with CMake I decided, that the
    simplest, most portable and flexible is to just add this flag manually. One
    of the reasons for such a decision is that I do not know how the project may
    be built and multiprocess compilation may cause problems under some hardware
    configurations.
    
    Best regards,
    Innokentiy
    
    
    
    --
    Sent from: http://cmake.3232098.n2.nabble.com/
    -- 
    
    Powered by www.kitware.com
    
    Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
    
    Kitware offers various services to support the CMake community. For more information on each offering, please visit:
    
    CMake Support: http://cmake.org/cmake/help/support.html
    CMake Consulting: http://cmake.org/cmake/help/consulting.html
    CMake Training Courses: http://cmake.org/cmake/help/training.html
    
    Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
    
    Follow this link to subscribe/unsubscribe:
    https://cmake.org/mailman/listinfo/cmake
    


-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake


More information about the CMake mailing list