9

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

Arkinaid
  • 93
  • 1
  • 1
  • 3

3 Answers3

14

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.

Community
  • 1
  • 1
  • Thanks for the fast answer! Iam not sure how to use macros in cmake at all. Can you explain me how to use your posted macro for replacing the flags? Thanks ! :) – Arkinaid Aug 14 '13 at 15:04
  • I have found this: remove(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) is this way ok too? – Arkinaid Aug 14 '13 at 15:14
  • Oh ~ thats bad. Because it works so fine xD . . . What should i do when i want to remove the -O3 flag from CMAKE_CXX_FLAGS_RELEASE for example ? I do not really know how to use this macro you post me. ? – Arkinaid Aug 14 '13 at 15:33
4

${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

Fabulous
  • 2,393
  • 2
  • 20
  • 27
2

Modifying the flags

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 )
Cameron Lowell Palmer
  • 21,528
  • 7
  • 125
  • 126