How to compile without optimizations -O0 using CMake

56,011

Solution 1

Or at invocation time:

cmake -DCMAKE_BUILD_TYPE=DEBUG \
      -DCMAKE_C_FLAGS_DEBUG="-g -O0" \
      -DCMAKE_CXX_FLAGS_DEBUG="-g -O0"

and then confirm with:

make VERBOSE=1

to show the GCC build commands as explained at: https://stackoverflow.com/questions/5820303/how-do-i-force-make-gcc-to-show-me-the-commands

Tested on Ubuntu 17.04, CMake 3.7.2.

Solution 2

Chip's answer was helpful, however since the SET line overwrote CMAKE_CXX_FLAGS_DEBUG this removed the -g default which caused my executable to be built without debug info. I needed to make a small additional modification to CMakeLists.txt in the project source directory to get an executable built with debugging info and -O0 optimizations (on cmake version 2.8.12.2).

I added the following to CMakeLists.txt to add -O0 and leave -g enabled:

# Add -O0 to remove optimizations when using gcc
IF(CMAKE_COMPILER_IS_GNUCC)
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0")
ENDIF(CMAKE_COMPILER_IS_GNUCC)

This adds the -O0 optimization to flags already used for debug by CMake and only is included for GCC builds if you happen to be using a cross platform project.

Solution 3

Add this to CMakeLists.txt (the one in the project source directory; don't touch anything in the build directory):

SET(CMAKE_CXX_FLAGS_DEBUG "-O0")
SET(CMAKE_C_FLAGS_DEBUG "-O0")

and then

$ cmake -DCMAKE_BUILD_TYPE=Debug

will work. Alternatively just add this to CMakeLists.txt:

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

Solution 4

I found that in some tests my CMAKE_C_FLAGS contained some optimization option by default (i.e. -O2). In this case, the above suggestions to do something like set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0") resulted in "-O2 -g -O0" which doesn't disable optimization.

Instead, I've had success using a simple regex to replace "-O#" with an empty string before appending "-O0".

My regex could probably use some fine tuning, but so far it seems to work well enough:

option(OPTIMIZE "Allow compiler optimizations.  Set to OFF to disable" ON)

if(NOT OPTIMIZE)
    string(REGEX REPLACE "(\-O[011123456789])" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
    string(REGEX REPLACE "(\-O[011123456789])" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
endif()
Share:
56,011

Related videos on Youtube

user4352158
Author by

user4352158

Updated on September 18, 2022

Comments

  • user4352158
    user4352158 over 1 year

    I am using Scientific Linux (SL). I am trying to compile a project that uses a bunch of C++ (.cpp) files.

    In the directory user/project/Build, I enter make to compile and link all the .cpp files. I then have to go to user/run/ and then type ./run.sh values.txt

    To debug with GDB, I have to go to user/run and then type gdb ../project/Build/bin/Project and to run, I enter run -Project INPUT/inputfile.txt. However, I am trying to print out the value of variable using p variablename.

    However, I get the message s1 = <value optimized out>. I have done some research online, and it seems I need to compile without optimizations using -O0 to resolve this. But where do I enter that? In the CMakeLists? If so, which CMakeLists? The one in project/Build or project/src/project?

    • smw
      smw about 9 years
      Have you tried simply running cmake again with the build type set to DEBUG? e.g. (from the Build directory) cmake -DCMAKE_BUILD_TYPE=DEBUG ..
  • Daniel Ignacio Fernández
    Daniel Ignacio Fernández almost 3 years
    Actually, gcc overrides previously provided flags whenever a new one is found. So, adding -O0 after -O2 disables optimization. Your solution produces a cleaner execution output nonetheless.