Is there a a way to force shorter (relative) object file paths?


Iam using CMake 3.17.1.

My project structure is like this:


In the CMakeLists.txt I have to specify all sources relative to the Makefiles directory so I end up with something like this:

add_executable(some_target …/Src/SomeSource.c)

Now this works well so far as long as the root path is not too long, as it seems the paths to the compiled object files CMake produces are something like this:


Why’s this absolute path necessary here?

Instead if CMakeLists.txt is at toplevel not at sibling level and source file paths are specified like add_executable(some_target Src/SomeSource.c), the object file path looks something like this:


and would be much shorter…

The latter would require a modification of the project structure, but I’d like to try to avoid this, since Iam working in a bigger team maintaining one codebase with multiple projects, and so far my project is the only project which is using CMake/ninja to compile our software.

So my question is, if there is a way to tell CMake to produce object file paths like in the latter form? This would greatly reduce path length and would increase the chance that the project is compiling under most circumstances, because currently Iam struggling big time with Windows’ MAX_PATH_LENGTH restriction, and I’m a little bit out of idea what to do.

Any help on this issue is much appreciated, thanks in advance. If you need more information please let me know.

Kind regards, Steve

CMake will treat any path outside of the source or build top-level directories as an absolute path. I would recommend adding a simple CMakeLists.txt at the top level that ends up chaining into your Makefiles/CMakeLists.txt file to have Src/ be “under” the top-level source directory.

Hey Ben,

thanks very much for your tip on putting a simple CMakeLists.txt file at the toplevel, this did the trick and the paths to the object files within the build directory are now relative to the toplevel and therefore much shorter.

Thanks, Steve