3

When I create a project for visual studio (2015) by using a cmake file, the optimization level is set to O2 by default in release mode.

I am unable to find a way to change this to other values within a cmake file. Additionaly, I would also need a program database (.pdb) to be build.

I tried:

SET(CMAKE_CXX_FLAGS "-O0")
SET(CMAKE_C_FLAGS "-O0")

and

SET(CMAKE_CXX_FLAGS_RELEASE "-O0")
SET(CMAKE_C_FLAGS_RELEASE "-O0")

as proposed here:

https://unix.stackexchange.com/questions/187455/how-to-compile-without-optimizations-o0-using-cmake

without the success. Anybody knows the right way?

2
  • 3
    On Stack Overflow we tend to maintain question post and solution(s) separated. Instead of adding SOLVED clause, you may post answer on your own question. This feature is perfectly fit for your case: please, use it.
    – Tsyvarev
    Commented Dec 21, 2016 at 17:57
  • good to know that. Its fixed!
    – GpG
    Commented Dec 23, 2016 at 11:30

4 Answers 4

3

I use this code:

string(REPLACE "-O2" "-O0" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
string(REPLACE "-O2" "-O0" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
0
2

Updating CMAKE_CXX_FLAGS_RELEASE and friends did not work for me. What did was:

target_compile_options(${target_name} PRIVATE "/Zi")   
target_compile_options(${target_name} PRIVATE "/Ob0")   
target_compile_options(${target_name} PRIVATE "/Od")   
target_compile_options(${target_name} PRIVATE "/RTC1")   

message(STATUS "updating options for target: ${target_name}")

It seems like cmake 3.x and up has it s.t. if we add a compile option that conflicts with a previous option, the newer option will take precedence.

1

Following the Antonio's answer the solution for me was:

First change the optimization setting:
string(REPLACE "/O2" "/Od" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")

Than add a flag that creates program database file:
string(CONCAT CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" " /Zi")

This also works but clears everything that was set to CMAKE_CXX_FLAGS_RELEASE before the call.

SET(CMAKE_CXX_FLAGS_RELEASE "/Od /Zi")
1

For Windows MSVC, I'm using this flag: -DCMAKE_CXX_FLAGS="/Od". Looks like this:

cmake src\ -DCMAKE_CXX_FLAGS="/Od"

Not the answer you're looking for? Browse other questions tagged or ask your own question.