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

I want to add certain cxx compiler flags to 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 the CXX flags now

 message(${CMAKE_CXX_FLAGS_RELEASE}) 

he will write to me

 -O3 -DNDEBUG -Wall -O2 
  • Did it make sense to use -02 instead of -03?
  • How can I remove -03 from flags?
  • What is DNDEBUG used for?

Best wishes

+7
compiler-construction g ++ flags cmake
source share
2 answers

Use the compiler documentation to see the difference between O2 and O3 and make your choice (: for example, gcc . Here you can find a recommendation to use O2 for stability .


You can use this macro to remove flags:

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

[using]

  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" 

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


NDEBUG is used to disable assert , see What is the NDEBUG preprocessor macro used for (on different platforms)? for more information.

+10
source share

Change flags

If you want to override the CMAKE_C_FLAGS_RELEASE or CMAKE_CXX_FLAGS_RELEASE variable for CMAKE_BUILD_TYPE release , which is set to -O3 -DNDEBUG , you need to do this before the project line. In fact, if the default values โ€‹โ€‹of the release type are not suitable for you, you need to take matters into your own hands. An additional feature is to handle this at CMAKE_TOOLCHAIN_FILE

 cmake_minimum_required( VERSION 3.8 ) set( CMAKE_C_FLAGS_DEBUG "" CACHE STRING "" ) set( CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "" ) set( CMAKE_C_FLAGS_RELEASE "" CACHE STRING "" ) set( CMAKE_CXX_FLAGS_RELEASE "" CACHE STRING "" ) project(hello) set( SOURCE_FILES hello.c foo.c ) add_executable( hello ${SOURCE_FILES} ) set_source_files_properties( foo.c PROPERTIES COMPILE_FLAGS "-O3 -DNDEBUG" ) set_source_files_properties( hello.c PROPERTIES COMPILE_FLAGS -O0 ) 
0
source share

All Articles