nvlink failure on Windows with CMake due to spaces in install path

I just reinstalled CUDA, in the default C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.2/bin/nvcc.exe location, and I include it in my CMake project using

project(myproject LANGUAGES CXX CUDA)

find_package(CUDA REQUIRED) # find and include CUDA
if (CUDA_FOUND)
    message(STATUS "CUDA FOUND")
    target_include_directories(Loch PUBLIC ${CUDA_INCLUDE_DIRS})
    target_link_libraries(Loch PRIVATE ${CUDA_LIBRARIES} cuda cudart)
else()
    message(STATUS "CUDA NOT FOUND")
endif()

However, when I build the program, once compilation is almost complete, linking fails with the error message

[ 90%] Linking CUDA device code CMakeFiles\Loch.dir\cmake_device_link.obj
nvlink fatal   : Could not open input file 'C:\Program'
main.cpp.obj
vec.cu.obj
sim.cu.obj
shader.cpp.obj
mass.cu.obj
spring.cu.obj
object.cu.obj
graphics.cpp.obj
cuda.lib
cudart.lib
opengl32.lib
glu32.lib
cudadevrt.lib
cudart_static.lib
kernel32.lib
user32.lib
gdi32.lib
winspool.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
comdlg32.lib
advapi32.lib
NMAKE : fatal error U1077: 'C:\PROGRA~1\NVIDIA~2\CUDA\v9.2\bin\nvcc.exe' : return code '0x1'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64\nmake.exe"' : return code '0x2'
Stop.

This clearly seems to be an issue with spaces in the Program Files path where the CUDA compiler is installed, but I’m not sure how to fix it. I’ve tried adding quotes to all the paths I can think of, to no avail. Is there any way I can circumvent this? Others, (here cmake CUDA include-path whitespaces not supported · Issue #15920 · tensorflow/tensorflow · GitHub for instance), seem to have had similar problems, but I can’t figure out how to debug it so far.

The command on which it actually fails is

C:\PROGRA~1\NVIDIA~2\CUDA\v9.2\bin\nvcc.exe -D_WINDOWS -Xcompiler="/W3 /GR /EHsc" -Xcompiler="-MDd -Zi -Ob0 -Od /RTC1" -Wno-deprecated-gpu-targets -shared -dlink "CMakeFiles\Loch.dir\main.cpp.obj"  "CMakeFiles\Loch.dir\src\vec.cu.obj"  "CMakeFiles\Loch.dir\src\sim.cu.obj"  "CMakeFiles\Loch.dir\src\shader.cpp.obj"  "CMakeFiles\Loch.dir\src\mass.cu.obj"  "CMakeFiles\Loch.dir\src\spring.cu.obj"  "CMakeFiles\Loch.dir\src\object.cu.obj"  "CMakeFiles\Loch.dir\src\graphics.cpp.obj"  -o CMakeFiles\Loch.dir\cmake_device_link.obj  -LIBPATH:C:\PROGRA~1\NVIDIA~2\CUDA\v9.2\lib\x64 -Xnvlink "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\lib\x64\cudart_static.lib" cuda.lib opengl32.lib glu32.lib -Xnvlink C:\Users\CreativeMachines\Documents\vcpkg\installed\x64-windows\debug\lib\glew32d.lib -Xnvlink C:\Users\CreativeMachines\Documents\vcpkg\installed\x64-windows\debug\lib\glfw3dll.lib cudadevrt.lib cudart_static.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib  -Xcompiler=-FdC:\Users\...\Documents\Loch\cmake-build-debug\CMakeFiles\Loch.dir\,-FS

I was able to fix the problem by removing the line

target_link_libraries(Loch PRIVATE ${CUDA_LIBRARIES})

and replacing it with

target_link_libraries(Loch PRIVATE cuda)

This still seems like an issue which should be resolved. I think the offending line was “C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\lib\x64\cudart_static.lib”, but I don’t know why that should be an issue.

CMake isn’t an NVIDIA product nor is it maintained by NVIDIA.

If you want to report a CMake issue, this probably isn’t the right forum for that.

[url]Sign in · GitLab

I’m getting this problem without CMake. This line on Windows:

C:\PROGRA~1\NVIDIA~2\CUDA\v10.0\bin\nvcc.exe "path with space\Plugin.cpp.obj" -Xnvlink "path with spaces\OpenMaya.lib"

Produces the following error:

nvlink fatal   : Could not open input file 'path'

Looks like the path to the obj file can contain spaces, but not the lib file.

I’d recommend filing a bug. The instructions are linked to a sticky post at the top of this sub-forum.