bhvima
February 17, 2024, 1:49pm
1
I created a very simple CMake project to demonstrate my issue.
├── CMakeLists.txt
└── apps
├── CMakeLists.txt
└── src
└── main.cpp
# CMakeLists.txt
cmake_minimum_required(VERSION 3.19)
# Set the project name
project (hello_cmake)
set(CMAKE_BUILD_TYPE Debug)
add_subdirectory(apps)
# apps/CMakeLists.txt
add_executable(main src/main.cpp)
// main.cpp
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello CMake!" << std::endl;
return 0;
}
When I build the project
cmake -S . -B build
cd build/ && make
The source file paths in the executable are weird.
readelf -wi apps/main | grep -B1 DW_AT_comp_dir
<12> DW_AT_name : (indirect line string, offset: 0x22): /home/user/cmaketest/apps/src/main.cpp
<16> DW_AT_comp_dir : (indirect line string, offset: 0x0): /home/user/cmaketest/build/apps
When a debugger looks for source files it will try DW_AT_comp_dir + DW_AT_name
which is not a valid path.
ben.boeckel
(Ben Boeckel (Kitware))
February 18, 2024, 3:51am
2
I closed this other related thread because this has a reproducer:
I created a very simple CMake project to demonstrate my issue.
├── CMakeLists.txt
└── apps
├── CMakeLists.txt
└── src
└── main.cpp
# CMakeLists.txt
cmake_minimum_required(VERSION 3.19)
# Set the project name
project (hello_cmake)
set(CMAKE_BUILD_TYPE Debug)
add_subdirectory(apps)
# apps/CMakeLists.txt
add_executable(main src/main.cpp)
// main.cpp
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello CMake!" << std::endl;
return 0;
}
When I buil…
Based on this GCC thread it seems to be based on whether the source file is passed as an absolute or relative path. CMake tends to generate absolute paths on command lines. I think that the -fdebug-prefix-map=
flag is what is wanted here.
Edit:
Yes, adding this:
add_compile_options(-fdebug-prefix-map=${CMAKE_SOURCE_DIR}=SRC)
results in:
% readelf -wi build/apps/main | grep -B1 DW_AT_comp_dir
<12> DW_AT_name : (indirect line string, offset: 0x0): SRC/apps/src/main.cpp
<16> DW_AT_comp_dir : (indirect line string, offset: 0x16): SRC/build/apps
bhvima
February 18, 2024, 3:45pm
3
Thanks. I changed it to
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdebug-prefix-map=`pwd`=\ ")
resulting in:
readelf -wi build/apps/main | grep -B1 DW_AT_comp_dir
<12> DW_AT_name : (indirect line string, offset: 0x0): /home/user/cmaketest/apps/src/main.cpp
<16> DW_AT_comp_dir : (indirect line string, offset: 0x7e):
This works. Is this the cleanest way to do it?
ben.boeckel
(Ben Boeckel (Kitware))
February 19, 2024, 4:04am
4
Maybe? Note that the `pwd`
bit is a hack that (I’m pretty sure) won’t work in the Ninja
generator. You should probably make explicit entries for various CMake directory variables.