Reputation: 93
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
best regards
Upvotes: 9
Views: 24423
Reputation: 61
${val}
references a variable.
Your set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall -O2")
Adds previously configured CMAKE_CXX_FLAGS_RELEASE variables.
So change it to
set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -Wall -O2")
-DNDEBUG
removes assert from your code - skips it.
More at http://en.cppreference.com/w/cpp/error/assert
Upvotes: 4
Reputation: 22246
If you want to override the default CMAKE_C_FLAGS_RELEASE
or CMAKE_CXX_FLAGS_RELEASE
variable for the CMAKE_BUILD_TYPE
release
which is set to -O3 -DNDEBUG
you'll need to do so before the project
line. In essence, if the release build type defaults don't suit you, you'll need to take matters into your own hands. An additional possibility is to handle this in the 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 )
Upvotes: 2
Reputation:
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.
Upvotes: 14