title: How to make working CUDA 11.6 with CMake 3.23 and MSVC 2019
body: <p>I cannot find a solution to manage how to use the langage CUDA in a CMake project on Windows with the standard MSVC 2019 compiler.</p>
  <p>I am trying to configure and compile <a href="https://gitlab.com/dominoc925/hello-cuda-cmake" rel="nofollow noreferrer">this <code>hello-cmake-cuda</code> repository</a> (also described in <a href="https://dominoc925.blogspot.com/2021/03/creating-simple-cuda-with-cmake-c.html" rel="nofollow noreferrer">this blog post</a>).</p>
  <p><code>CMakeLists.txt</code> file contents:</p>
  <pre><code>cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
  project(hello LANGUAGES CXX CUDA)
  enable_language(CUDA)
  add_executable(hello hello.cu)
  </code></pre>
  <p>Here is the output to the <code>cmake ..</code> command, run from within the build directory:</p>
  <pre><code>PS C:\GitRepo\cuda_hello\build&gt; cmake ..
  -- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.22000.
  CMake Error at C:/Program Files/CMake/share/cmake-3.23/Modules/CMakeDetermineCUDACompiler.cmake:311 (message):
    CMAKE_CUDA_ARCHITECTURES must be valid if set.
  Call Stack (most recent call first):
    CMakeLists.txt:5 (project)
  
  
  -- Configuring incomplete, errors occurred!
  See also &quot;C:/GitRepo/cuda_hello/build/CMakeFiles/CMakeOutput.log&quot;.
  See also &quot;C:/GitRepo/cuda_hello/build/CMakeFiles/CMakeError.log&quot;.
  </code></pre>
  <p>It means that <code>architectures_tested</code> from <code>CMakeDetermineCUDACompiler.cmake:311</code> is empty...</p>
  <p>How can I get CMake to complete its configuration and the simple program to build?</p>
  <h3>My development environment</h3>
  <ul>
  <li>Operating system: Windows 11 Version 10.0.22000 Build 22000</li>
  <li>Compiler: Microsoft Visual Studio Community 2019 Version 16.11.11</li>
  <li>CMake version is 3.23</li>
  <li>CUDA version is 11.6</li>
  </ul>
  <p>I have tried different versions of each soft and keep having the same issue. I have decided to stay with these versions at the moment.</p>
  <p>My GPU is properly configured: It shows up with <code>nvidia-smi</code>, and I am also able to build and run the <code>deviceQuery</code> CUDA sample:</p>
  <pre><code>CUDA Device Query (Runtime API) version (CUDART static linking)
  
  Detected 1 CUDA Capable device(s)
  
  Device 0: &quot;NVIDIA GeForce GTX 1650&quot;
    CUDA Driver Version / Runtime Version          11.6 / 11.6
    CUDA Capability Major/Minor version number:    7.5
    etc. etc. ...
  
  deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 11.6, CUDA Runtime Version = 11.6, NumDevs = 1
  Result = PASS
  </code></pre>
  <hr />
  <p>My environment PATH variable:</p>
  <pre><code>PS C:\GitRepo\hello-cuda-cmake-master&gt; $env:path -split &quot;;&quot;
  C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin
  C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\libnvvp
  C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3\bin
  C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3\libnvvp
  
  C:\Program Files (x86)\Common Files\Oracle\Java\javapath
  C:\Python38\Scripts\
  C:\Python38\
  C:\Windows\system32
  C:\Windows
  C:\Windows\System32\Wbem
  C:\Windows\System32\WindowsPowerShell\v1.0\
  C:\Windows\System32\OpenSSH\
  C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
  C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR
  C:\Program Files\PuTTY\
  C:\Program Files (x86)\PuTTY\
  C:\Program Files\Microsoft SQL Server\110\Tools\Binn\
  C:\Program Files\TortoiseSVN\bin
  C:\Program Files\TortoiseGit\bin
  C:\Program Files\Microsoft VS Code\bin
  C:\WINDOWS\system32
  C:\WINDOWS
  C:\WINDOWS\System32\Wbem
  C:\WINDOWS\System32\WindowsPowerShell\v1.0\
  C:\WINDOWS\System32\OpenSSH\
  C:\Program Files\Docker\Docker\resources\bin
  C:\ProgramData\DockerDesktop\version-bin
  C:\Program Files\Git\cmd
  C:\WINDOWS\system32
  C:\WINDOWS
  C:\WINDOWS\System32\Wbem
  C:\WINDOWS\System32\WindowsPowerShell\v1.0\
  C:\WINDOWS\System32\OpenSSH\
  C:\Program Files\NVIDIA Corporation\Nsight Compute 2022.1.1\
  C:\Program Files\CMake\bin
  C:\Ruby30-x64\bin
  C:\Users\Thibault GEFFROY\.cargo\bin
  C:\Users\Thibault GEFFROY\AppData\Local\Microsoft\WindowsApps
  C:\Program Files\OpenCppCoverage
  C:\intelFPGA\20.1\modelsim_ase\win32aloem
  </code></pre>
  <h3>What I've tried and hasn't worked</h3>
  <p>If I try to insert the wanted <code>CMAKE_CUDA_ARCHITECTURES</code> :</p>
  <p><code>set(CMAKE_CUDA_ARCHITECTURES 75)</code></p>
  <p>I get:</p>
  <pre><code>PS C:\GitRepo\cuda_hello\build&gt; cmake ..
  -- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.22000.
  -- The CUDA compiler identification is unknown
  CMake Error at C:/Program Files/CMake/share/cmake-3.23/Modules/CMakeDetermineCUDACompiler.cmake:654 (message):
    The CMAKE_CUDA_ARCHITECTURES:
  
      75
  
    do not all work with this compiler.  Try:
  
  
  
    instead.
  Call Stack (most recent call first):
    CMakeLists.txt:5 (project)
  
  
  -- Configuring incomplete, errors occurred!
  See also &quot;C:/GitRepo/cuda_hello/build/CMakeFiles/CMakeOutput.log&quot;.
  See also &quot;C:/GitRepo/cuda_hello/build/CMakeFiles/CMakeError.log&quot;.
  </code></pre>
  <p>If I try to use the <code>FindCUDA</code> module to set <code>CMAKE_CUDA_ARCHITECTURES</code> -  the solution given by @alfC <a href="https://stackoverflow.com/a/69353718/12851157">here</a> - I get:</p>
  <pre><code>PS C:\GitRepo\cuda_hello\build&gt; cmake ..
  CMake Error at C:/Program Files/CMake/share/cmake-3.23/Modules/FindCUDA/select_compute_arch.cmake:120 (file):
    file failed to open for writing (Permission denied):
  
      /detect_cuda_compute_capabilities.cpp
  Call Stack (most recent call first):
    CMakeLists.txt:4 (CUDA_DETECT_INSTALLED_GPUS)
  
  
  CMake Error: The source directory &quot;CMAKE_FLAGS&quot; does not exist.
  Specify --help for usage, or press the help button on the CMake GUI.
  CMake Error at C:/Program Files/CMake/share/cmake-3.23/Modules/FindCUDA/select_compute_arch.cmake:141 (try_run):
    Failed to configure test project build system.
  Call Stack (most recent call first):
    CMakeLists.txt:4 (CUDA_DETECT_INSTALLED_GPUS)
  
  
  CMake Error: TRY_COMPILE attempt to remove -rf directory that does not contain CMakeTmp:/detect_cuda_compute_capabilities.cpp
  -- Configuring incomplete, errors occurred!
  See also &quot;C:/GitRepo/cuda_hello/build/CMakeFiles/CMakeOutput.log&quot;.
  See also &quot;C:/GitRepo/cuda_hello/build/CMakeFiles/CMakeError.log&quot;.
  </code></pre>
  <p>finally, if I try to invoke <code>find_package(CUDA)</code>, I get:</p>
  <pre><code>PS C:\GitRepo\cuda_hello\build&gt; cmake ..
  CMake Error at C:/Program Files/CMake/share/cmake-3.23/Modules/FindCUDA.cmake:677 (cmake_initialize_per_config_variable):
    Unknown CMake command &quot;cmake_initialize_per_config_variable&quot;.
  Call Stack (most recent call first):
    CMakeLists.txt:2 (find_package)
  
  
  -- Configuring incomplete, errors occurred!
  See also &quot;C:/GitRepo/cuda_hello/build/CMakeFiles/CMakeOutput.log&quot;.
  See also &quot;C:/GitRepo/cuda_hello/build/CMakeFiles/CMakeError.log&quot;.
  </code></pre>
  <h3>Edit 1:</h3>
  <p>Answer to @einpoklum solution <a href="https://stackoverflow.com/a/71743245/12851157">this</a>:</p>
  <p>Thanks for the proposal but it doesn't work either.</p>
  <p>Here is the output of the <code>cmake -B build</code> command in <a href="https://gitlab.com/eyalroz/hello-cuda-cmake" rel="nofollow noreferrer">your repository</a>:</p>
  <pre><code>PS C:\GitRepo\hello-cuda-cmake-master&gt; cmake -B build
  -- Building for: Visual Studio 16 2019
  -- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.22000.
  -- The CUDA compiler identification is unknown
  CMake Error at C:/Program Files/CMake/share/cmake-3.23/Modules/CMakeDetermineCUDACompiler.cmake:633 (message):
    Failed to detect a default CUDA architecture.
  
  
  
    Compiler output:
  
  Call Stack (most recent call first):
    CMakeLists.txt:2 (project)
  
  
  -- Configuring incomplete, errors occurred!
  See also &quot;C:/GitRepo/hello-cuda-cmake-master/build/CMakeFiles/CMakeOutput.log&quot;.
  See also &quot;C:/GitRepo/hello-cuda-cmake-master/build/CMakeFiles/CMakeError.log&quot;.
  </code></pre>
  <p>The output is the same using PowerShell or a MSVC command prompt.</p>
  <hr />
  <p>Here are the cmake variables and their value when using cmake-gui:</p>
  <p><a href="https://i.stack.imgur.com/BBvxD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BBvxD.png" alt="Cmake Gui" /></a></p>
  <hr />
  <p>When using the simple nvcc build command: <code>nvcc hello.cu</code> from MSVC command prompt I get:</p>
  <pre><code>nvcc fatal   : Could not set up the environment for Microsoft Visual Studio using 'c:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/HostX86/x86/../../../../../../../VC/Auxiliary/Build/vcvars64.bat'
  </code></pre>
  <p>The PATH is valid though, and the script vcvars64.bat exists at this location.</p>
  <hr />
  <h3>What happens if I add the <code>find_package(CUDAToolkit)</code> to the <code>CMakeLists.txt</code></h3>
  <p>The new <code>CMakeLists.txt</code>:</p>
  <pre><code>cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
  find_package(CUDAToolkit)
  project(hello LANGUAGES CUDA)
  add_executable(hello hello.cu)
  </code></pre>
  <p>The output :</p>
  <pre><code>PS C:\GitRepo\hello-cuda-cmake-master&gt; cmake -B build
  -- Building for: Visual Studio 16 2019
  -- Found CUDAToolkit: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.6/include (found version &quot;11.6.124&quot;)
  -- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.22000.
  -- The CUDA compiler identification is unknown
  CMake Error at C:/Program Files/CMake/share/cmake-3.23/Modules/CMakeDetermineCUDACompiler.cmake:633 (message):
    Failed to detect a default CUDA architecture.
  
  
  
    Compiler output:
  
  Call Stack (most recent call first):
    CMakeLists.txt:3 (project)
  
  
  -- Configuring incomplete, errors occurred!
  See also &quot;C:/GitRepo/hello-cuda-cmake-master/build/CMakeFiles/CMakeOutput.log&quot;.
  See also &quot;C:/GitRepo/hello-cuda-cmake-master/build/CMakeFiles/CMakeError.log&quot;.
  </code></pre>
  <h3>Edit 2:</h3>
  <p>I am trying to compile the <a href="https://github.com/nvidia/cuda-samples" rel="nofollow noreferrer">CUDA sample</a> BlackScholes without CMake, with the MSVC 2019 solution provided.</p>
  <p>I end up with this error:</p>
  <pre><code>Severity        Code        Description        Project        File        Line        Suppression State
  Error        MSB3721        The command &quot;&quot;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin\nvcc.exe&quot; -gencode=arch=compute_35,code=\&quot;sm_35,compute_35\&quot; -gencode=arch=compute_37,code=\&quot;sm_37,compute_37\&quot; -gencode=arch=compute_50,code=\&quot;sm_50,compute_50\&quot; -gencode=arch=compute_52,code=\&quot;sm_52,compute_52\&quot; -gencode=arch=compute_60,code=\&quot;sm_60,compute_60\&quot; -gencode=arch=compute_61,code=\&quot;sm_61,compute_61\&quot; -gencode=arch=compute_70,code=\&quot;sm_70,compute_70\&quot; -gencode=arch=compute_75,code=\&quot;sm_75,compute_75\&quot; -gencode=arch=compute_80,code=\&quot;sm_80,compute_80\&quot; -gencode=arch=compute_86,code=\&quot;sm_86,compute_86\&quot; --use-local-env -ccbin &quot;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x64&quot; -x cu   -I./ -I../../../Common -I./ -I&quot;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\/include&quot; -I../../../Common -I&quot;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\include&quot;  -G   --keep-dir x64\Debug  -maxrregcount=0  --machine 64 --compile -cudart static -Xcompiler &quot;/wd 4819&quot;  --threads 0 -g  -DWIN32 -DWIN32 -D_MBCS -D_MBCS -Xcompiler &quot;/EHsc /W3 /nologo /Od /Fdx64/Debug/vc142.pdb /FS /Zi /RTC1 /MTd &quot; -o &quot;C:\ProgramData\NVIDIA Corporation\CUDA Samples\v11.6\cuda-samples\Samples\5_Domain_Specific\BlackScholes\x64\Debug\BlackScholes.cu.obj&quot; &quot;C:\ProgramData\NVIDIA Corporation\CUDA Samples\v11.6\cuda-samples\Samples\5_Domain_Specific\BlackScholes\BlackScholes.cu&quot;&quot; exited with code 1.        BlackScholes        C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\BuildCustomizations\CUDA 11.6.targets        790
  </code></pre>
  <p>While using WSL 2 Ubuntu 20.4 and the <a href="https://docs.nvidia.com/cuda/wsl-user-guide/index.html#ch03a-setting-up-cuda" rel="nofollow noreferrer">following CUDA installation</a> and these <a href="https://docs.nvidia.com/cuda/wsl-user-guide/index.html#building-cuda-samples" rel="nofollow noreferrer">instructions</a> to build the BlackScholes sample I get this output:</p>
  <pre><code>$ sudo make BlackScholes
  /usr/local/cuda/bin/nvcc -ccbin g++ -I../../../Common  -m64    -maxrregcount=16 --threads 0 --std=c++11 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_86,code=compute_86 -o BlackScholes.o -c BlackScholes.cu
  nvcc warning : The 'compute_35', 'compute_37', 'sm_35', and 'sm_37' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
  ptxas warning : For profile sm_86 adjusting per thread register count of 16 to lower bound of 24
  ptxas warning : For profile sm_80 adjusting per thread register count of 16 to lower bound of 24
  ptxas warning : For profile sm_70 adjusting per thread register count of 16 to lower bound of 24
  ptxas warning : For profile sm_75 adjusting per thread register count of 16 to lower bound of 24
  /usr/local/cuda/bin/nvcc -ccbin g++ -I../../../Common  -m64    -maxrregcount=16 --threads 0 --std=c++11 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_86,code=compute_86 -o BlackScholes_gold.o -c BlackScholes_gold.cpp
  nvcc warning : The 'compute_35', 'compute_37', 'sm_35', and 'sm_37' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
  /usr/local/cuda/bin/nvcc -ccbin g++   -m64      -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_86,code=compute_86 -o BlackScholes BlackScholes.o BlackScholes_gold.o
  nvcc warning : The 'compute_35', 'compute_37', 'sm_35', and 'sm_37' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
  mkdir -p ../../../bin/x86_64/linux/release
  cp BlackScholes ../../../bin/x86_64/linux/release
  
  
  $ ./BlackScholes
  [./BlackScholes] - Starting...
  GPU Device 0: &quot;Turing&quot; with compute capability 7.5
  
  Initializing data...
  ...allocating CPU memory for options.
  ...allocating GPU memory for options.
  ...generating input data in CPU mem.
  ...copying input data to GPU mem.
  Data init done.
  
  Executing Black-Scholes GPU kernel (512 iterations)...
  Options count             : 8000000
  BlackScholesGPU() time    : 0.722482 msec
  Effective memory bandwidth: 110.729334 GB/s
  Gigaoptions per second    : 11.072933
  
  BlackScholes, Throughput = 11.0729 GOptions/s, Time = 0.00072 s, Size = 8000000 options, NumDevsUsed = 1, Workgroup = 128
  
  Reading back GPU results...
  Checking the results...
  ...running CPU calculations.
  
  Comparing the results...
  L1 norm: 1.741792E-07
  Max absolute error: 1.192093E-05
  
  Shutting down...
  ...releasing GPU memory.
  ...releasing CPU memory.
  Shutdown done.
  
  [BlackScholes] - Test Summary
  
  NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.
  
  Test passed
  </code></pre>