# How to set $PATH for try\_compile runs, and during build?

**URL:** https://discourse.cmake.org/t/how-to-set-path-for-try-compile-runs-and-during-build/10256
**Category:** Usage
**Tags:** os:windows, gen:ninja, lang:cuda
**Created:** [March 1, 2024, 6:13pm UTC](https://discourse.cmake.org/t/how-to-set-path-for-try-compile-runs-and-during-build/10256 "2024-03-01T18:13:28Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![garyo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/garyo/32/4364_2.png) [@garyo](https://discourse.cmake.org/u/garyo)
#### Post date: [March 1, 2024, 6:13pm UTC](https://discourse.cmake.org/t/how-to-set-path-for-try-compile-runs-and-during-build/10256/1 "2024-03-01T18:13:28Z")

</div>

I’m trying to use CUDA with clang-cl (or just plain cl.exe) on Windows, using [Mark Schofield’s Windows toolchain](https://github.com/MarkSchofield/WindowsToolchain). The toolchain finds `cl.exe`, but then when CMake tries to find CUDA it fails, because `nvcc` can’t find compiler `cl.exe` in $PATH.

I think the same thing happens at build time, because cmake doesn’t set $PATH before invoking ninja.

How can I set my project up so `cl.exe` is on $PATH both while reading the CMakefiles and when running the build?

---

<div class="post-metadata">

### Author: ![garyo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/garyo/32/4364_2.png) [@garyo](https://discourse.cmake.org/u/garyo)
#### Post date: [March 1, 2024, 7:16pm UTC](https://discourse.cmake.org/t/how-to-set-path-for-try-compile-runs-and-during-build/10256/2 "2024-03-01T19:16:14Z")

</div>

I guess the best way on Windows is to open a developer prompt and work from there. That seems to be what cmake is expecting. Oh well.

---

<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: [March 29, 2024, 11:22am UTC](https://discourse.cmake.org/t/how-to-set-path-for-try-compile-runs-and-during-build/10256/3 "2024-03-29T11:22:16Z")

</div>

> [@garyo](#):
>
> I think the same thing happens at build time, because cmake doesn’t set $PATH before invoking ninja.

There’s no mechanism to do so (unless `cmake --build $dir` is the way to run the build).

Note that CMake expects the environment to support running the compiler as needed. MSVC has this as a persistent requirement and other platforms work because the default environment “works”. However, when using `module load` on Unix machines or `DEVELOPER_DIR=` to choose an Xcode toolchain on macOS, the environment needs to be consistent between compiler detection and use. CMake cannot know what all matters from the environment nor how to persist it to the build in a local way (e.g., `xcode-select` can change default toolchain behaviors on macOS behind CMake’s back).

---

<div class="post-metadata">

### Author: ![MarkSchofield](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@MarkSchofield](https://discourse.cmake.org/u/MarkSchofield)
#### Post date: [September 9, 2024, 4:36am UTC](https://discourse.cmake.org/t/how-to-set-path-for-try-compile-runs-and-during-build/10256/4 "2024-09-09T04:36:20Z")

</div>

Hey, @garyo, thanks for taking a look at my WindowsToolchain project.

> I guess the best way on Windows is to open a developer prompt and work from there.

Yes. If you want an ‘off the shelf’ solution then your best bet is either:

1. Use CMake with a Visual Studio Generator.
2. Use CMake with a Ninja generator, running from a Visual Studio command-prompt.

The WindowsToolchain project is an attempt at getting the best of both worlds - Ninja builds are (from my experience) faster than MSBuild builds, and the WindowsToolchain avoids having to configure/change environment variables to configure builds.

That being said, WindowsToolchain just added some basic support for CUDA compilation - with the help of a contributor to the project, we worked to make it easier to use the Toolchain with CUDA. Take a look at [#106](https://github.com/MarkSchofield/WindowsToolchain/pull/106) from the project for more details.

TL;DR: Rather than adding cl.exe to the $PATH, you can set [CMAKE\_CUDA\_HOST\_COMPILER](https://cmake.org/cmake/help/latest/variable/CMAKE_CUDA_HOST_COMPILER.html).

---

<div class="post-metadata">

### Author: ![corio](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/d26b3c/32.png) [@corio](https://discourse.cmake.org/u/corio)
#### Post date: [March 11, 2026, 3:53pm UTC](https://discourse.cmake.org/t/how-to-set-path-for-try-compile-runs-and-during-build/10256/5 "2026-03-11T15:53:11Z")

</div>

> [@garyo](#):
>
> I guess the best way on Windows is to open a developer prompt and work from there. That seems to be what cmake is expecting. Oh w

Hi, @garyo

Let me share my experience.  
Originally, I tried to work with Visual Studio Generator and `-T ClangCL` switch but it failed.  
I successfully used WindowsToolchain with cl.exe to build CUDA projects in the past and found it could not use Сlang with Visual Studio Generator or Ninja.

Main reason to run builds on top of Сlang is superior speed of built executables.

Working recipe for standard CUDA samples from Nvidia (it doesn’t require developer prompt, one can open a normal prompt):

```auto
set VS_DIR=c:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools
call “%VS_DIR%\VC\Auxiliary\Build\vcvarsall.bat” x64
set LLVM_DIR=c:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/Llvm/x64
set CUDA_DIR=c:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.6
set “PATH=%LLVM_DIR%/bin;%CUDA_DIR%/bin;%PATH%”

“c:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe” ^ 
-G Ninja ^
-S “C:/Downloads/cuda-samples-master/cuda-samples-master/Samples/1_Utilities/deviceQuery” ^
-B .^
-DCMAKE_C_COMPILER=“%LLVM_DIR%/bin/clang.exe” ^
-DCMAKE_CXX_COMPILER=“%LLVM_DIR%/bin/clang++.exe” ^
-DCMAKE_CUDA_COMPILER=“C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.6/bin/nvcc.exe” ^
-DCMAKE_VS_PLATFORM_TOOLSET_CUDA_CUSTOM_DIR=“C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.6” ^
-DCMAKE_VS_PLATFORM_TOOLSET_CUDA=12.6 ^
-DCUDAToolkit_ROOT=“C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.6” ^
-DCMAKE_CUDA_STANDARD=14 ^
-DCMAKE_CUDA_ARCHITECTURES=75 ^
-DCMAKE_CUDA_EXTENSIONS=OFF

```

Hope it helps.
