Cuda hello world project fails to run with CMake on Windows

When I try to run the following CMake project:

cmake_minimum_required(VERSION 3.29)

project(my_cuda_project LANGUAGES CXX CUDA)

add_executable(my_cuda_project Main.cu)

Why do I get the following error:

PS G:\CMake Tutorial\tutorial\build> cmake ../src  
-- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.22631.
CMake Error at C:/Program Files/CMake/share/cmake-3.29/Modules/CMakeDetermineCompilerId.cmake:814 (message):
  Compiling the CUDA compiler identification source file
  "CMakeCUDACompilerId.cu" failed.

  Compiler:

  Build flags:

  Id flags: --keep;--keep-dir;tmp -v



  The output was:

  1

  MSBuild version 17.9.5+33de0b227 for .NET Framework

  Build started 6/18/2024 5:54:46 PM.



  Project "G:\CMake
  Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA.vcxproj"
  on node 1 (default targets).

  PrepareForBuild:

    Creating directory "Debug\".
    Structured output is enabled. The formatting of compiler diagnostics will reflect the error hierarchy. See https://aka.ms/cpp/structured-output for more details.
    Creating directory "Debug\CompilerIdCUDA.tlog\".

  InitializeBuildStatus:

    Creating "Debug\CompilerIdCUDA.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
    Touching "Debug\CompilerIdCUDA.tlog\unsuccessfulbuild".

  AddCudaCompileDeps:

    C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX64\x86\cl.exe /E /nologo /showIncludes /TP /D__CUDACC__ /D__CUDACC_VER_MAJOR__=12 /D__CUDACC_VER_MINOR__=3 /D_MBCS /I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\include" /I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin" /I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\include" /I. /FIcuda_runtime.h /c "G:\CMake Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CMakeCUDACompilerId.cu"

  Project "G:\CMake
  Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA.vcxproj"
  (1) is building "G:\CMake
  Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA.vcxproj"
  (1:2) on node 1 (CudaBuildCore target(s)).

  CudaBuildCore:

    Compiling CUDA source file CMakeCUDACompilerId.cu...
    Creating directory "G:\CMake Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA\Debug".
    cmd.exe /C "C:\Users\mrakg\AppData\Local\Temp\tmpb0be3e1a0b414c67800d716f317f04bd.cmd"
    "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX64\x86" -x cu   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\include"  -G   --keep-dir CompilerIdCUDA\Debug  -maxrregcount=0   --machine 64 --compile -cudart static -v -allow-unsupported-compiler -g  -D_MBCS -Xcompiler "/EHsc /W0 /nologo /Od /FS /Zi /RTC1 /MDd " -Xcompiler "/FdDebug\vc143.pdb" -o "G:\CMake Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA\Debug\CMakeCUDACompilerId.cu.obj" "G:\CMake Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CMakeCUDACompilerId.cu"

    G:\CMake Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX64\x86" -x cu   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\include"  -G   --keep-dir CompilerIdCUDA\Debug  -maxrregcount=0   --machine 64 --compile -cudart static -v -allow-unsupported-compiler -g  -D_MBCS -Xcompiler "/EHsc /W0 /nologo /Od /FS /Zi /RTC1 /MDd " -Xcompiler "/FdDebug\vc143.pdb" -o "G:\CMake Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA\Debug\CMakeCUDACompilerId.cu.obj" "G:\CMake Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CMakeCUDACompilerId.cu"
    #$ _NVVM_BRANCH_=nvvm
    #$ _SPACE_=
    #$ _CUDART_=cudart
    #$ _HERE_=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin
    #$ _THERE_=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin
    #$ _TARGET_SIZE_=
    #$ _TARGET_DIR_=
    #$ _TARGET_SIZE_=64
    #$ _WIN_PLATFORM_=x64
    #$ TOP=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin/..
    #$ NVVMIR_LIBRARY_DIR=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin/../nvvm/libdevice
    #$ PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin/../nvvm/bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin/../lib;C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX64\x86;C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX64\x64;C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64;;C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\x64;C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x86;;C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools;C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\tools;C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\ide;C:\Program Files (x86)\HTML Help Workshop;;C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\;C:\Windows\Microsoft.NET\Framework\v4.0.30319\;C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin;C:\Windows\Microsoft.NET\Framework\v4.0.30319\;;C:\Program Files\PowerShell\7;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\libnvvp;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.2\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.2\libnvvp;;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files\dotnet\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\NVIDIA Corporation\Nsight Compute 2023.3.1\;C:\Program Files\PowerShell\7\;C:\Program Files\CMake\bin;C:\Users\mrakg\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\mrakg\AppData\Local\Programs\Python\Python311\;C:\Users\mrakg\AppData\Local\Programs\Python\Launcher\;C:\Users\mrakg\AppData\Local\Microsoft\WindowsApps;C:\Users\mrakg\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\mrakg\AppData\Roaming\npm;C:\Users\mrakg\AppData\Local\Microsoft\WinGet\Packages\Gyan.FFmpeg_Microsoft.Winget.Source_8wekyb3d8bbwe\ffmpeg-6.1-full_build\bin;C:\ProgramData\chocolatey\bin;C:\Program Files\NVIDIA Corporation\Nsight Systems 2024.1.1\target-windows-x64\;C:\Users\mrakg\AppData\Roaming\Python\Scripts;
    #$ INCLUDES="-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin/../include"
    #$ LIBRARIES=  "/LIBPATH:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin/../lib/x64"
    #$ CUDAFE_FLAGS=--sdk_dir "C:\Program Files (x86)\Windows Kits\10"
    #$ PTXAS_FLAGS=
    CMakeCUDACompilerId.cu
    #$ resource file C:\Users\mrakg\AppData\Local\Temp/tmpxft_00007444_00000000-8.res: [-D__CUDA_ARCH_LIST__=520 -nologo -E -TP -EHsc -D__CUDACC__ -D__NVCC__ -D__CUDACC_DEBUG__  /EHsc /W0 /nologo /Od /FS /Zi /RTC1 /MDd /FdDebug\vc143.pdb -I"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.3/include" -I"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.3/include" "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin/../include"    -D "_MBCS" -D__CUDACC_VER_MAJOR__=12 -D__CUDACC_VER_MINOR__=3 -D__CUDACC_VER_BUILD__=107 -D__CUDA_API_VER_MAJOR__=12 -D__CUDA_API_VER_MINOR__=3 -D__NVCC_DIAG_PRAGMA_SUPPORT__=1 -FI "cuda_runtime.h" -Zi "G:\CMake Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CMakeCUDACompilerId.cu" ]
    #$ cl.exe -D__NV_NO_HOST_COMPILER_CHECK=1 @"C:\Users\mrakg\AppData\Local\Temp/tmpxft_00007444_00000000-8.res" > "C:/Users/mrakg/AppData/Local/Temp/tmpxft_00007444_00000000-6_CMakeCUDACompilerId.cpp4.ii"
    CMakeCUDACompilerId.cu
    #$ erase C:\Users\mrakg\AppData\Local\Temp/tmpxft_00007444_00000000-8.res
    #$ cudafe++ --microsoft_version=1939 --msvc_target_version=1939 --compiler_bindir "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.39.33519/bin/HostX64/x86/../../../../../../.." --display_error_number --orig_src_file_name "G:/CMake Tutorial/tutorial/build/CMakeFiles/3.29.6/CompilerIdCUDA/CMakeCUDACompilerId.cu" --orig_src_path_name "G:\CMake Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CMakeCUDACompilerId.cu" --allow_managed --debug_mode --sdk_dir "C:\Program Files (x86)\Windows Kits\10" --m64 --parse_templates --gen_c_file_name "C:/Users/mrakg/AppData/Local/Temp/tmpxft_00007444_00000000-7_CMakeCUDACompilerId.cudafe1.cpp" --stub_file_name "tmpxft_00007444_00000000-7_CMakeCUDACompilerId.cudafe1.stub.c" --gen_module_id_file --module_id_file_name "C:/Users/mrakg/AppData/Local/Temp/tmpxft_00007444_00000000-5_CMakeCUDACompilerId.module_id" "C:/Users/mrakg/AppData/Local/Temp/tmpxft_00007444_00000000-6_CMakeCUDACompilerId.cpp4.ii"

  CUDACOMPILE : nvcc error : 'cudafe++' died with status 0xC0000005
  (ACCESS_VIOLATION) [G:\CMake
  Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA.vcxproj]


    # --error 0xc0000005 --

  C:\Program Files\Microsoft Visual
  Studio\2022\Community\MSBuild\Microsoft\VC\v170\BuildCustomizations\CUDA
  12.3.targets(799,9): error MSB3722: The command ""C:\Program Files\NVIDIA
  GPU Computing Toolkit\CUDA\v12.3\bin\nvcc.exe"
  -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin
  "C:\Program Files\Microsoft Visual
  Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX64\x86" -x cu
  -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\include"
  -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\include" -G
  --keep-dir CompilerIdCUDA\Debug -maxrregcount=0 --machine 64 --compile
  -cudart static -v -allow-unsupported-compiler -g -D_MBCS -Xcompiler "/EHsc
  /W0 /nologo /Od /FS /Zi /RTC1 /MDd " -Xcompiler "/FdDebug\vc143.pdb" -o
  "G:\CMake
  Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA\Debug\CMakeCUDACompilerId.cu.obj"
  "G:\CMake
  Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CMakeCUDACompilerId.cu""
  exited with code 5.  Please verify that you have sufficient rights to run
  this command.  [G:\CMake
  Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA.vcxproj]


  Done Building Project "G:\CMake
  Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA.vcxproj"
  (CudaBuildCore target(s)) -- FAILED.

  Done Building Project "G:\CMake
  Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA.vcxproj"
  (default targets) -- FAILED.



  Build FAILED.



  "G:\CMake
  Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA.vcxproj"
  (default target) (1) ->

  "G:\CMake
  Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA.vcxproj"
  (CudaBuildCore target) (1:2) ->

  (CudaBuildCore target) ->

    CUDACOMPILE : nvcc error : 'cudafe++' died with status 0xC0000005 (ACCESS_VIOLATION) [G:\CMake Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA.vcxproj]     
    C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\BuildCustomizations\CUDA 12.3.targets(799,9): error MSB3722: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX64\x86" -x cu   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\include"  -G   --keep-dir CompilerIdCUDA\Debug  -maxrregcount=0   --machine 64 --compile -cudart static -v -allow-unsupported-compiler -g  -D_MBCS -Xcompiler "/EHsc /W0 /nologo /Od /FS /Zi /RTC1 /MDd " -Xcompiler "/FdDebug\vc143.pdb" -o "G:\CMake Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA\Debug\CMakeCUDACompilerId.cu.obj" "G:\CMake Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CMakeCUDACompilerId.cu"" exited with code 5. Please verify that you have sufficient rights to run this command. [G:\CMake Tutorial\tutorial\build\CMakeFiles\3.29.6\CompilerIdCUDA\CompilerIdCUDA.vcxproj]



      0 Warning(s)
      2 Error(s)



  Time Elapsed 00:00:00.75





Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.29/Modules/CMakeDetermineCompilerId.cmake:8 (CMAKE_DETERMINE_COMPILER_ID_BUILD)
  C:/Program Files/CMake/share/cmake-3.29/Modules/CMakeDetermineCompilerId.cmake:53 (__determine_compiler_id_test)
  C:/Program Files/CMake/share/cmake-3.29/Modules/CMakeDetermineCUDACompiler.cmake:131 (CMAKE_DETERMINE_COMPILER_ID)
  CMakeLists.txt:3 (project)


-- Configuring incomplete, errors occurred!

Main.cu is a simple hello world style of program.

#include <cstdio>
#include <cstdlib>
 
int main(int argc, const char **argv) {
    printf("Hello CMake!\n");

    return EXIT_SUCCESS;
}

Given the error, it seems CMake doesn’t seem to work with Cuda on Windows.