# Incorrect Source File Paths

**URL:** <https://discourse.cmake.org/t/incorrect-source-file-paths/10123>\
**Category:** Usage\
**Tags:** gen:makefiles\
**Created:** [February 17, 2024, 1:49pm UTC](https://discourse.cmake.org/t/incorrect-source-file-paths/10123 "2024-02-17T13:49:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhvima](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bhvima/32/4319_2.png) [@bhvima](https://discourse.cmake.org/u/bhvima)\
**Post date:** [February 17, 2024, 1:49pm UTC](https://discourse.cmake.org/t/incorrect-source-file-paths/10123/1 "2024-02-17T13:49:55Z")

</div>

I created a very simple CMake project to demonstrate my issue.

```auto
├── CMakeLists.txt
└── apps
    ├── CMakeLists.txt
    └── src
        └── main.cpp

```

```auto
# CMakeLists.txt
cmake_minimum_required(VERSION 3.19)

# Set the project name
project (hello_cmake)

set(CMAKE_BUILD_TYPE Debug)

add_subdirectory(apps)

```

```auto
# apps/CMakeLists.txt
add_executable(main src/main.cpp)

```

```auto
// main.cpp
#include <iostream>

int main(int argc, char *argv[])
{
   std::cout << "Hello CMake!" << std::endl;
   return 0;
}

```

When I build the project

```auto
cmake -S . -B build
cd build/ && make

```

The source file paths in the executable are weird.

```auto
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.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [February 18, 2024, 3:51am UTC](https://discourse.cmake.org/t/incorrect-source-file-paths/10123/2 "2024-02-18T03:51:54Z")

</div>

I closed this other related thread because this has a reproducer:

> [@Incorrect Source File Paths](https://discourse.cmake.org/t/incorrect-source-file-paths/10123):
>
> 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](https://gcc.gnu.org/legacy-ml/gcc-help/2013-11/msg00064.html) 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:

```cmake
add_compile_options(-fdebug-prefix-map=${CMAKE_SOURCE_DIR}=SRC)

```

results in:

```auto
% 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

```

---

<div class="post-metadata">

**Author:** ![bhvima](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bhvima/32/4319_2.png) [@bhvima](https://discourse.cmake.org/u/bhvima)\
**Post date:** [February 18, 2024, 3:45pm UTC](https://discourse.cmake.org/t/incorrect-source-file-paths/10123/3 "2024-02-18T15:45:35Z")

</div>

Thanks. I changed it to

```auto
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdebug-prefix-map=`pwd`=\ ")

```

resulting in:

```auto
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?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [February 19, 2024, 4:04am UTC](https://discourse.cmake.org/t/incorrect-source-file-paths/10123/4 "2024-02-19T04:04:56Z")

</div>

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.
