Can't build CUDA Separable Compilation sample application with CMake

I’m trying to build the code sample found here: code-samples/posts/cmake at master · robertmaynard/code-samples · GitHub but it doesn’t build.

I’m using the following configuration: CUDA 11.4.2_471.41_win10, CMake 3.21.3, Visual Studio 16.11.5

and I get the following output from Visual Studio when I try to build the project:

Rebuild started...
1>------ Rebuild All started: Project: ZERO_CHECK, Configuration: Debug x64 ------
1>Build started 10/20/2021 8:08:58 AM.
1>Target InitializeBuildStatus:
1>  Creating "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
1>Target CustomBuild:
1>  Checking Build System
1>Target FinalizeBuildStatus:
1>  Deleting file "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild".
1>  Touching "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\ZERO_CHECK.lastbuildstate".
1>
1>Build succeeded.
1>    0 Warning(s)
1>    0 Error(s)
1>
1>Time Elapsed 00:00:00.16
2>------ Rebuild All started: Project: particles, Configuration: Debug x64 ------
2>Build started 10/20/2021 8:08:58 AM.
2>Target CudaClean:
2>Target ResolveProjectReferences:
2>Target InitializeBuildStatus:
2>  Touching "particles.dir\Debug\particles.tlog\unsuccessfulbuild".
2>Target CustomBuild:
2>  Building Custom Rule C:/Users/<username>/Documents/Github/code-samples/posts/cmake/CMakeLists.txt
2>Target CudaBuild:
2>  Target CudaBuildCore:
2>    Compiling CUDA source file ..\particle.cu...
2>    Target CudaBuildCore:
2>      Compiling CUDA source file ..\v3.cu...
2>
2>      C:\Users\<username>\Documents\Github\code-samples\posts\cmake\build>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64" -x cu -rdc=true   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\include"     --keep-dir x64\Debug  -maxrregcount=0 --ptxas-options=-v --machine 64 --compile -cudart static -std=c++14 -Xcompiler="/EHsc -Zi -Ob0" -g  -D_WINDOWS -D"CMAKE_INTDIR=\"Debug\"" -DWIN32 -D_WINDOWS -D"CMAKE_INTDIR=\"Debug\"" -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Fd"C:\Users\<username>\Documents\Github\code-samples\posts\cmake\build\Debug\particles.pdb" /FS /Zi /RTC1 /MDd /GR" -o particles.dir\Debug\particle.obj "C:\Users\<username>\Documents\Github\code-samples\posts\cmake\particle.cu"
2>
2>      C:\Users\<username>\Documents\Github\code-samples\posts\cmake\build>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64" -x cu -rdc=true   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\include"     --keep-dir x64\Debug  -maxrregcount=0 --ptxas-options=-v --machine 64 --compile -cudart static -std=c++14 -Xcompiler="/EHsc -Zi -Ob0" -g  -D_WINDOWS -D"CMAKE_INTDIR=\"Debug\"" -DWIN32 -D_WINDOWS -D"CMAKE_INTDIR=\"Debug\"" -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Fd"C:\Users\<username>\Documents\Github\code-samples\posts\cmake\build\Debug\particles.pdb" /FS /Zi /RTC1 /MDd /GR" -o particles.dir\Debug\v3.obj "C:\Users\<username>\Documents\Github\code-samples\posts\cmake\v3.cu"
2>      nvcc fatal   : A single input file is required for a non-link phase when an outputfile is specified
2>      C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VC\v160\BuildCustomizations\CUDA 11.4.targets(785,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64" -x cu -rdc=true   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\include"     --keep-dir x64\Debug  -maxrregcount=0 --ptxas-options=-v --machine 64 --compile -cudart static -std=c++14 -Xcompiler="/EHsc -Zi -Ob0" -g  -D_WINDOWS -D"CMAKE_INTDIR=\"Debug\"" -DWIN32 -D_WINDOWS -D"CMAKE_INTDIR=\"Debug\"" -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Fd"C:\Users\<username>\Documents\Github\code-samples\posts\cmake\build\Debug\particles.pdb" /FS /Zi /RTC1 /MDd /GR" -o particles.dir\Debug\particle.obj "C:\Users\<username>\Documents\Github\code-samples\posts\cmake\particle.cu"" exited with code 1.
2>    Done building target "CudaBuildCore" in project "particles.vcxproj" -- FAILED.
2>
2>    Done building project "particles.vcxproj" -- FAILED.
2>    nvcc fatal   : A single input file is required for a non-link phase when an outputfile is specified
2>    C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VC\v160\BuildCustomizations\CUDA 11.4.targets(785,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64" -x cu -rdc=true   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\include"     --keep-dir x64\Debug  -maxrregcount=0 --ptxas-options=-v --machine 64 --compile -cudart static -std=c++14 -Xcompiler="/EHsc -Zi -Ob0" -g  -D_WINDOWS -D"CMAKE_INTDIR=\"Debug\"" -DWIN32 -D_WINDOWS -D"CMAKE_INTDIR=\"Debug\"" -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Fd"C:\Users\<username>\Documents\Github\code-samples\posts\cmake\build\Debug\particles.pdb" /FS /Zi /RTC1 /MDd /GR" -o particles.dir\Debug\v3.obj "C:\Users\<username>\Documents\Github\code-samples\posts\cmake\v3.cu"" exited with code 1.
2>  Done building target "CudaBuildCore" in project "particles.vcxproj" -- FAILED.
2>
2>  Done building project "particles.vcxproj" -- FAILED.
2>
2>Build FAILED.
2>
2>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VC\v160\BuildCustomizations\CUDA 11.4.targets(785,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64" -x cu -rdc=true   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\include"     --keep-dir x64\Debug  -maxrregcount=0 --ptxas-options=-v --machine 64 --compile -cudart static -std=c++14 -Xcompiler="/EHsc -Zi -Ob0" -g  -D_WINDOWS -D"CMAKE_INTDIR=\"Debug\"" -DWIN32 -D_WINDOWS -D"CMAKE_INTDIR=\"Debug\"" -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Fd"C:\Users\<username>\Documents\Github\code-samples\posts\cmake\build\Debug\particles.pdb" /FS /Zi /RTC1 /MDd /GR" -o particles.dir\Debug\particle.obj "C:\Users\<username>\Documents\Github\code-samples\posts\cmake\particle.cu"" exited with code 1.
2>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VC\v160\BuildCustomizations\CUDA 11.4.targets(785,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64" -x cu -rdc=true   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\include"     --keep-dir x64\Debug  -maxrregcount=0 --ptxas-options=-v --machine 64 --compile -cudart static -std=c++14 -Xcompiler="/EHsc -Zi -Ob0" -g  -D_WINDOWS -D"CMAKE_INTDIR=\"Debug\"" -DWIN32 -D_WINDOWS -D"CMAKE_INTDIR=\"Debug\"" -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Fd"C:\Users\<username>\Documents\Github\code-samples\posts\cmake\build\Debug\particles.pdb" /FS /Zi /RTC1 /MDd /GR" -o particles.dir\Debug\v3.obj "C:\Users\<username>\Documents\Github\code-samples\posts\cmake\v3.cu"" exited with code 1.
2>    0 Warning(s)
2>    2 Error(s)
2>
2>Time Elapsed 00:00:02.98
3>------ Rebuild All started: Project: particle_test, Configuration: Debug x64 ------
3>Build started 10/20/2021 8:09:01 AM.
3>Target CudaClean:
3>Target ResolveProjectReferences:
3>Target InitializeBuildStatus:
3>  Touching "particle_test.dir\Debug\particle_test.tlog\unsuccessfulbuild".
3>Target CustomBuild:
3>  Building Custom Rule C:/Users/<username>/Documents/Github/code-samples/posts/cmake/CMakeLists.txt
3>Target CudaBuild:
3>  Target CudaBuildCore:
3>    Compiling CUDA source file ..\test.cu...
3>
3>    C:\Users\<username>\Documents\Github\code-samples\posts\cmake\build>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64" -x cu -rdc=true   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\include"     --keep-dir x64\Debug  -maxrregcount=0  --machine 64 --compile -cudart static -std=c++14 -Xcompiler="/EHsc -Zi -Ob0" -g  -D_WINDOWS -D"CMAKE_INTDIR=\"Debug\"" -DWIN32 -D_WINDOWS -D"CMAKE_INTDIR=\"Debug\"" -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Fdparticle_test.dir\Debug\vc142.pdb /FS /Zi /RTC1 /MDd /GR" -o particle_test.dir\Debug\test.obj "C:\Users\<username>\Documents\Github\code-samples\posts\cmake\test.cu"
3>    test.cu
3>Target GetReferencedVCProjectsInfo:
3>  Target GetReferencedVCProjectsInfo:
3>Target CudaLink:
3>  C:\Users\<username>\Documents\Github\code-samples\posts\cmake\build>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin\nvcc.exe" -dlink -o particle_test.dir\Debug\particle_test.device-link.obj -Xcompiler "/EHsc /W3 /nologo /Od /Fdparticle_test.dir\Debug\vc142.pdb /Zi /RTC1 /MDd /GR" -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin/crt" -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\lib\x64" Debug\particles.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 -forward-unknown-to-host-compiler -Wno-deprecated-gpu-targets  -gencode=arch=compute_52,code=sm_52  --machine 64 particle_test.dir\Debug\test.obj
3>  nvlink fatal   : Could not open input file 'debug/particles.lib'
3>  particles.lib
3>  cudadevrt.lib
3>  cudart_static.lib
3>  kernel32.lib
3>  user32.lib
3>  gdi32.lib
3>  winspool.lib
3>  shell32.lib
3>  ole32.lib
3>  oleaut32.lib
3>  uuid.lib
3>  comdlg32.lib
3>  advapi32.lib
3>  test.obj
3>  C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VC\v160\BuildCustomizations\CUDA 11.4.targets(874,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin\nvcc.exe" -dlink -o particle_test.dir\Debug\particle_test.device-link.obj -Xcompiler "/EHsc /W3 /nologo /Od /Fdparticle_test.dir\Debug\vc142.pdb /Zi /RTC1 /MDd /GR" -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin/crt" -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\lib\x64" Debug\particles.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 -forward-unknown-to-host-compiler -Wno-deprecated-gpu-targets  -gencode=arch=compute_52,code=sm_52  --machine 64 particle_test.dir\Debug\test.obj" exited with code 1.
3>Done building target "CudaLink" in project "particle_test.vcxproj" -- FAILED.
3>
3>Done building project "particle_test.vcxproj" -- FAILED.
3>
3>Build FAILED.
3>
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VC\v160\BuildCustomizations\CUDA 11.4.targets(874,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin\nvcc.exe" -dlink -o particle_test.dir\Debug\particle_test.device-link.obj -Xcompiler "/EHsc /W3 /nologo /Od /Fdparticle_test.dir\Debug\vc142.pdb /Zi /RTC1 /MDd /GR" -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin/crt" -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\lib\x64" Debug\particles.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 -forward-unknown-to-host-compiler -Wno-deprecated-gpu-targets  -gencode=arch=compute_52,code=sm_52  --machine 64 particle_test.dir\Debug\test.obj" exited with code 1.
3>    0 Warning(s)
3>    1 Error(s)
3>
3>Time Elapsed 00:00:05.74
4>------ Rebuild All started: Project: ALL_BUILD, Configuration: Debug x64 ------
4>Build started 10/20/2021 8:09:07 AM.
4>Target ResolveProjectReferences:
4>Target PrepareForBuild:
4>  Creating directory "x64\Debug\ALL_BUILD\ALL_BUILD.tlog\".
4>Target InitializeBuildStatus:
4>  Creating "x64\Debug\ALL_BUILD\ALL_BUILD.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
4>Target CustomBuild:
4>  Building Custom Rule C:/Users/<username>/Documents/Github/code-samples/posts/cmake/CMakeLists.txt
4>Target GetReferencedVCProjectsInfo:
4>Target FinalizeBuildStatus:
4>  Deleting file "x64\Debug\ALL_BUILD\ALL_BUILD.tlog\unsuccessfulbuild".
4>  Touching "x64\Debug\ALL_BUILD\ALL_BUILD.tlog\ALL_BUILD.lastbuildstate".
4>
4>Build succeeded.
4>    0 Warning(s)
4>    0 Error(s)
4>
4>Time Elapsed 00:00:00.16
========== Rebuild All: 2 succeeded, 2 failed, 0 skipped ==========

I have a similar issue with my own project, which is why I turned to a code sample for a sanity check, but it didn’t help my sanity. Am I doing something wrong here? Is this code sample too outdated?

The example is based on cmake 3.8 in which ‘FindCUDA’ and CUDA_NVCC_FLAGS are deprecated since version 3.10 .
If you are using 3.21 , you may want move to CMake built-in support for the CUDA language you would use CMAKE_CUDA_FLAGS. FindCUDAToolkit — CMake 3.22.0-rc1 Documentation .