# Object file names use absolute path to sources outside source tree

**URL:** https://discourse.cmake.org/t/object-file-names-use-absolute-path-to-sources-outside-source-tree/6383
**Category:** Usage
**Created:** [September 1, 2022, 10:31am UTC](https://discourse.cmake.org/t/object-file-names-use-absolute-path-to-sources-outside-source-tree/6383 "2022-09-01T10:31:38Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jmqwerk](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/a5b964/32.png) [@jmqwerk](https://discourse.cmake.org/u/jmqwerk)
#### Post date: [September 1, 2022, 10:31am UTC](https://discourse.cmake.org/t/object-file-names-use-absolute-path-to-sources-outside-source-tree/6383/1 "2022-09-01T10:31:38Z")

</div>

Is it possible to override the current directory that CMake is using for relative file paths from a specific CMakeLists.txt file? The use case for this is to reduce very long object filenames in windows caused by full paths being used. The specific project has it’s main CMake files in /\<folder\_a\> and a part of that has a chained CMakeLists.txt file inside of /\<folder\_a\> which compiles files inside of /\<folder\_b\>, which is where the long full paths come in. It is not possible to move the CMakeLists.txt file from folder\_a to folder\_b therefore is it possible in the CMakeLists.txt file to make CMake think that the file is being included from /\<folder\_b\> so that if a source is added, it is added relative to the /\<folder\_b\> path and not the /\<folder\_a\> path?  
I’ve tried manually setting CMAKE\_CURRENT\_SOURCE\_DIR and CMAKE\_SOURCE\_DIR but this does not seem to fool CMake and it continues to use relative paths from \<folder\_a\>

---

<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: [September 1, 2022, 11:57am UTC](https://discourse.cmake.org/t/object-file-names-use-absolute-path-to-sources-outside-source-tree/6383/2 "2022-09-01T11:57:05Z")

</div>

You can give an explicit binary directory in `add_subdirectory`. For example:

```cmake
add_subdirectory(folderb "${CMAKE_BINARY_DIR}/folderb")

```

which should shorten paths for you.

---

<div class="post-metadata">

### Author: ![jmqwerk](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/a5b964/32.png) [@jmqwerk](https://discourse.cmake.org/u/jmqwerk)
#### Post date: [September 1, 2022, 2:17pm UTC](https://discourse.cmake.org/t/object-file-names-use-absolute-path-to-sources-outside-source-tree/6383/3 "2022-09-01T14:17:51Z")

</div>

Still seems to produce long paths, given this CMakeLists.txt file:

```auto
add_subdirectory(nrfx "${CMAKE_BINARY_DIR}/nrfx")

```

Which then has the CMakeLists.txt file using absolute paths to the files in a different root directory, however the object files are still using long paths with the full source file path in the output name:

```auto
./nrfx/CMakeFiles/nrfx.dir/nrfx_glue.c.obj
./nrfx/CMakeFiles/nrfx.dir/tmp/aa/modules/hal/nordic/nrfx/drivers/src/nrfx_dppi.c.obj
./nrfx/CMakeFiles/nrfx.dir/tmp/aa/modules/hal/nordic/nrfx/drivers/src/nrfx_qspi.c.obj
./nrfx/CMakeFiles/nrfx.dir/tmp/aa/modules/hal/nordic/nrfx/drivers/src/nrfx_nvmc.c.obj

```

---

<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: [September 1, 2022, 2:20pm UTC](https://discourse.cmake.org/t/object-file-names-use-absolute-path-to-sources-outside-source-tree/6383/4 "2022-09-01T14:20:18Z")

</div>

Are you compiling sources that live outside of the source or binary directories?

@brad.king IIRC, there was some recent stuff about using absolute paths in places, but I thought that was Ninja generator-related.

---

<div class="post-metadata">

### Author: ![jmqwerk](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/a5b964/32.png) [@jmqwerk](https://discourse.cmake.org/u/jmqwerk)
#### Post date: [September 1, 2022, 2:35pm UTC](https://discourse.cmake.org/t/object-file-names-use-absolute-path-to-sources-outside-source-tree/6383/5 "2022-09-01T14:35:44Z")

</div>

Yes, so `<root>/<folder_a>` has the cmake files in and is building files in `<root>/<folder_b>`, there are no cmake files in `<folder_b>`. Ninja is being used for this specific test but the same paths are present if make is used instead

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [September 1, 2022, 4:08pm UTC](https://discourse.cmake.org/t/object-file-names-use-absolute-path-to-sources-outside-source-tree/6383/6 "2022-09-01T16:08:32Z")

</div>

> `CMakeLists.txt` file inside of `<root>/<folder_a>` which compiles files inside of `<root>/<folder_b>`, which is where the long full paths come in.

That is by design, and there is no way to avoid it without putting a `CMakeLists.txt` file in `<root>` or above.

For a given source file, CMake choose an object file name that is unique, deterministic, and independent of what other sources may produce object files too. In order to avoid name collisions, that means some part of the path to the source file needs to be used in the path to the object file. When the source files are located inside the source tree, the leading absolute path to the source tree can be left out, and short object names used. When the sources are located outside the source tree, the only way to reference each one uniquely is to use an absolute path.

As for windows path length limitations, we have code to convert the path fragment into a hash when the total path length will be too long. See the code [here](https://gitlab.kitware.com/cmake/cmake/-/blob/v3.24.1/Source/cmLocalGenerator.cxx#L3516-3560). Maybe something is going wrong with that heuristic in your case.

---

<div class="post-metadata">

### Author: ![jmqwerk](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/a5b964/32.png) [@jmqwerk](https://discourse.cmake.org/u/jmqwerk)
#### Post date: [September 2, 2022, 8:13am UTC](https://discourse.cmake.org/t/object-file-names-use-absolute-path-to-sources-outside-source-tree/6383/7 "2022-09-02T08:13:57Z")

</div>

Thank you, I will relay that information and see if there’s any more reported issues.
