Most variables are optimized out, even though -O0 is specified (using cmake and mpicxx/g++)

60 views Asked by At

I'm working on a project in C++ with MPI. Whenever I try debugging any code contained in the library added library, most variables are optimized out. I compile the library myself and set -O0 -g for it. My only guess is, that link time optimizations plays a role here, but I can't figure out how to disable it in cmake.

Adding either

set(CMAKE_INTERPROCEDURAL_OPTIMIZATION FALSE)

or

set_property(TARGET TEST PROPERTY INTERPROCEDURAL_OPTIMIZATION FALSE)

has no effect. The binary is still compiled with -flto=auto -ffat-lto-objects. Does anyone have an idea how to make cmake not add these flags?

As additional info, adding my full CMakeLists.txt

cmake_minimum_required(VERSION 3.21)
project(Patch CXX)

find_package(MPI REQUIRED)
set(CMAKE_C_COMPILER mpicc)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION FALSE)

set(CMAKE_CXX_COMPILER /usr/bin/mpicxx)
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -DDEBUG")
set(CMAKE_CXX_STANDARD 17)

set(CMAKE_DEBUG_POSTFIX d)

set(MPI_INCLUDE_PATH /usr/include/mpich)
message(STATUS "Run: ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${MPIEXEC_MAX_NUMPROCS} ${MPIEXEC_PREFLAGS} EXECUTABLE ${MPIEXEC_POSTFLAGS} ARGS")

link_directories(${CMAKE_SOURCE_DIR}/libs)
include_directories(${CMAKE_SOURCE_DIR}/include, ${MPI_INCLUDE_PATH})


ADD_LIBRARY(PATCH_NODES src/Patch.cpp src/Node.cpp)
target_include_directories(PATCH_NODES PUBLIC include ${MPI_INCLUDE_PATH})
target_compile_options(PATCH_NODES PUBLIC -O0 -g -Wall -Wextra -Wpedantic)

target_link_libraries(PATCH_NODES PUBLIC MPI::MPI_CXX)

add_executable(TEST src/main.cpp)
target_compile_options(TEST PUBLIC -O0 -g -Wall -Wextra -Wpedantic)
set_property(TARGET TEST PROPERTY INTERPROCEDURAL_OPTIMIZATION FALSE)

target_include_directories(TEST PUBLIC include)
target_link_libraries(TEST PUBLIC MPI::MPI_CXX PATCH_NODES)

cmake is called with

/usr/bin/cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -S(my_path) -B(my_path) -G "Unix Makefiles

The library is static and gets generated with the "d" suffix, marking it as a debug version. The generated compiler command looks like this:

"command": "/usr/bin/mpicxx  -I/(my_path)/include, -I/usr/include/mpich -I/(my_path)/include -isystem /usr/include/x86_64-linux-gnu/mpich -O0 -g -DDEBUG -O0 -g -Wall -Wextra -Wpedantic -flto=auto -ffat-lto-objects -o CMakeFiles/PATCH_NODES.dir/src/Patch.cpp.o -c /(my_path)/src/Patch.cpp"

and is identical for all source files.

0

There are 0 answers