adrianmcmenamin
adrianmcmenamin

Reputation: 1129

Maximum path lengths with CMake

I have a project that uses CMake and builds inside a deeply nested set of directories on Windows and typically fails due to excessive path lengths.

Based on this answer - How to set the Maximum Path Length with CMAKE? - (and on the comment from @Tsyvarev below - Maximum path lengths with CMake - I put this inside the CMakeLists.txt in the project root and close to the top of the file (i.e. before any other processing):

set(CMAKE_OBJECT_PATH_MAX 250)
set(CMAKE_OBJECT_NAME_MAX 245)

(As the build is always on Windows I did not bother with the Unix test in the answer in the earlier question.)

However, it still fails.

I have other mechanisms to shorten the path lengths so I know this is the issue here - but I'm looking for general advice to give to users about how they can set up their CMake builds to ensure they avoid this problem - my understanding was that CMake would use a hashing scheme to avoid the long path names but that doesn't seem to be happening.

So does such a mechanism exist and how do I access it if it does?

Upvotes: 5

Views: 14350

Answers (2)

Muneeb Wali
Muneeb Wali

Reputation: 1

Error: I'm encountering an error message that states: "The maximum full path to an object file is 250 characters." This is likely due to the long path length of the object file in my project.

Explanation:

The 250-character limit is a common guideline for object file paths, but it's not a strict rule. Some systems or tools might have different limitations or handle long paths differently.

Solutions:

  1. Change the drive: Consider moving your project to a drive with shorter path lengths, such as an E: drive etc drive you have rather then C:\ drive.

  2. Shorten file and directory names: If possible, shorten the names of files and directories in your project to reduce the overall path length.

I hope this will resolve the problem.

Upvotes: -3

Alex Reinking
Alex Reinking

Reputation: 19926

You can set the path length limit in CMake with CMAKE_OBJECT_PATH_MAX, for example:

if (WIN32)
  set(CMAKE_OBJECT_PATH_MAX 260)
endif ()

But really if you have the power to do so, you should just remove the path length limit (from Windows 10 v1607 onward) via the PowerShell command:

New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" `
-Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force

See: https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=powershell

Upvotes: 11

Related Questions