windowscmakepath

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?


Solution

  • 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