# CUDA selction with Visual Studio Generator

**URL:** https://discourse.cmake.org/t/cuda-selction-with-visual-studio-generator/9535
**Category:** Usage
**Created:** [December 1, 2023, 6:34am UTC](https://discourse.cmake.org/t/cuda-selction-with-visual-studio-generator/9535 "2023-12-01T06:34:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![cudawarped](https://discourse.cmake.org/user_avatar/discourse.cmake.org/cudawarped/32/4047_2.png) [@cudawarped](https://discourse.cmake.org/u/cudawarped)
#### Post date: [December 1, 2023, 6:34am UTC](https://discourse.cmake.org/t/cuda-selction-with-visual-studio-generator/9535/1 "2023-12-01T06:34:32Z")

</div>

If I have two CUDA toolkits installed say 12.2 and 12.3 the default Visual Studio behaviour appears to be to use CUDA 12.3, how can I generate a Visual Studio project which uses CUDA toolkit 12.2 when CUDA is a first class language?

If I use the ninja generator then adding _C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.2/bin_ to the begining of the system/user PATH environmental variable will select 12.2, however this does not work for the Visual Studio generator.

If I use Visual Studio generator it seems that the path to CUDA is determined by the **CUDA\_PATH\_V12\_3** environmental variable. That is if this is set this to to _C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.2_ CMake detects CUDA 12.2 and outputs

> – Looking for a CUDA compiler  
> – Looking for a CUDA compiler - C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.2/bin/nvcc.exe  
> – The CUDA compiler identification is NVIDIA 12.2.128

however if I delete this environmental variable, even if there is a **CUDA\_PATH** and **CUDA\_PATHV12\_2** environmental variable pointing to the same place and the directory containing the compiler is on the **PATH** the CUDA toolkit is not found.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 1, 2023, 11:41am UTC](https://discourse.cmake.org/t/cuda-selction-with-visual-studio-generator/9535/2 "2023-12-01T11:41:04Z")

</div>

Cc: @robert.maynard

---

<div class="post-metadata">

### Author: ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)
#### Post date: [December 1, 2023, 4:06pm UTC](https://discourse.cmake.org/t/cuda-selction-with-visual-studio-generator/9535/3 "2023-12-01T16:06:04Z")

</div>

For Visual Studio Generators you select the CUDA version via the Toolset: [https://cmake.org/cmake/help/latest/variable/CMAKE\_GENERATOR\_TOOLSET.html#visual-studio-toolset-selection](https://cmake.org/cmake/help/latest/variable/CMAKE_GENERATOR_TOOLSET.html#visual-studio-toolset-selection)

---

<div class="post-metadata">

### Author: ![cudawarped](https://discourse.cmake.org/user_avatar/discourse.cmake.org/cudawarped/32/4047_2.png) [@cudawarped](https://discourse.cmake.org/u/cudawarped)
#### Post date: [December 2, 2023, 7:20am UTC](https://discourse.cmake.org/t/cuda-selction-with-visual-studio-generator/9535/4 "2023-12-02T07:20:47Z")

</div>

That worked perfectly, thank you for the help.

---

<div class="post-metadata">

### Author: ![cudawarped](https://discourse.cmake.org/user_avatar/discourse.cmake.org/cudawarped/32/4047_2.png) [@cudawarped](https://discourse.cmake.org/u/cudawarped)
#### Post date: [December 2, 2023, 3:53pm UTC](https://discourse.cmake.org/t/cuda-selction-with-visual-studio-generator/9535/5 "2023-12-02T15:53:29Z")

</div>

@robert.maynard Is there a recommended way to select the CUDA toolkit when using other generators? Currently in linux I add the location of the CUDA toolkit to the **CUDA\_PATH** environmental variable and in Windows I add it to the start of the system/user **PATH**.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 4, 2023, 12:58pm UTC](https://discourse.cmake.org/t/cuda-selction-with-visual-studio-generator/9535/6 "2023-12-04T12:58:40Z")

</div>

I’m a very tangential CUDA user (I mainly just compile other people’s code), but setting `CUDACC` and `CUDACXX` environment variables has worked well.
