Search code examples
cmakeg++flags

CMake, g++ : set CXX Flags -02 but -03 is still there?


i think want to add specific compiler cxx flags to the release-mode. I read here in another thread that -O2 are good flags for release configuration

set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall -O2")

but if i check now the CXX Flags

message(${CMAKE_CXX_FLAGS_RELEASE})

he write me

-O3 -DNDEBUG -Wall -O2
  1. Did it make sense using -02 instead of -03 ?
  2. How can i delete -03 from the Flags?
  3. What is DNDEBUG using for?

best regards


Solution

  • Use compiler documentation to see difference between O2 and O3 and make your choice (: for example - gcc. Here you can found recommendation to use O2 for stability.


    You can use this macro for removing flags:

    macro(remove_cxx_flag flag)
      string(REPLACE "${flag}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
    endmacro()
    

    [usage]

      macro(remove_cxx_flag flag)
        string(REPLACE "${flag}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
      endmacro()
    
      message(${CMAKE_CXX_FLAGS_RELEASE}) # print "-O3 -DNDEBUG"
      remove_cxx_flag("-O3")
      message(${CMAKE_CXX_FLAGS_RELEASE}) # print "-DNDEBUG"
    

    Here is used macro because you need to update variable from parent scope, read this - http://www.cmake.org/cmake/help/v2.8.10/cmake.html#command:macro (or you can use function with PARENT_SCOPE modifier)


    NDEBUG used for disabling assert, see What is the NDEBUG preprocessor macro used for (on different platforms)? for more info.