How to compile without optimizations -O0 using CMake
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()
Related videos on Youtube
user4352158
Updated on September 18, 2022Comments
-
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 entermake
to compile and link all the .cpp files. I then have to go touser/run/
and then type./run.sh values.txt
To debug with GDB, I have to go to
user/run
and then typegdb ../project/Build/bin/Project
and to run, I enterrun -Project INPUT/inputfile.txt
. However, I am trying to print out the value of variable usingp 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 theCMakeLists
? If so, which CMakeLists? The one inproject/Build
orproject/src/project
?-
smw about 9 yearsHave 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 almost 3 yearsActually,
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.