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

3

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

Clearstory answered 14/8, 2013 at 13:45 Comment(0)
Z
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.

Zen answered 14/8, 2013 at 14:46 Comment(3)
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 ! :)Clearstory
I have found this: remove(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) is this way ok too?Clearstory
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. ?Clearstory
E
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

Express answered 14/4, 2018 at 16:46 Comment(0)
P
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 )
Parmenter answered 21/12, 2017 at 21:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.