cuda build_error ai_generated true

nvcc fatal : Cannot find compiler 'cl.exe' in PATH

ID: cuda/nvcc-include-path-not-found

Also available as: JSON · Markdown · 中文
85%Fix Rate
90%Confidence
1Evidence
2023-04-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
CUDA 12.0 active
CUDA 11.8 active
Visual Studio 2022 active
Visual Studio 2019 active

Root Cause

On Windows, nvcc requires the Microsoft Visual C++ compiler (cl.exe) to be in the PATH, but the build environment is missing the necessary Visual Studio tools or the correct architecture (x64 vs x86) is not configured.

generic

中文

在 Windows 上,nvcc 需要在 PATH 中包含 Microsoft Visual C++ 编译器 (cl.exe),但构建环境缺少必要的 Visual Studio 工具或未配置正确的架构(x64 与 x86)。

Official Documentation

https://docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows/index.html#compiler

Workarounds

  1. 95% success Open the 'x64 Native Tools Command Prompt for VS 2022' (or VS 2019) from the Start Menu, then run your nvcc command from that prompt. This sets all required environment variables automatically.
    Open the 'x64 Native Tools Command Prompt for VS 2022' (or VS 2019) from the Start Menu, then run your nvcc command from that prompt. This sets all required environment variables automatically.
  2. 85% success If using a build system like CMake, set the `CMAKE_GENERATOR` to 'Visual Studio 17 2022' and specify `-A x64` to ensure the correct toolchain is used.
    If using a build system like CMake, set the `CMAKE_GENERATOR` to 'Visual Studio 17 2022' and specify `-A x64` to ensure the correct toolchain is used.
  3. 75% success Manually add the Visual C++ compiler directory to PATH: `set PATH=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64;%PATH%` (adjust version numbers). Also set INCLUDE and LIB as per VS developer prompt.
    Manually add the Visual C++ compiler directory to PATH: `set PATH=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64;%PATH%` (adjust version numbers). Also set INCLUDE and LIB as per VS developer prompt.

中文步骤

  1. 从开始菜单打开“x64 Native Tools Command Prompt for VS 2022”(或 VS 2019),然后在该提示符下运行 nvcc 命令。这会自动设置所有必需的环境变量。
  2. 如果使用 CMake 等构建系统,设置 `CMAKE_GENERATOR` 为 'Visual Studio 17 2022' 并指定 `-A x64` 以确保使用正确的工具链。
  3. 手动将 Visual C++ 编译器目录添加到 PATH:`set PATH=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64;%PATH%`(调整版本号)。同时根据 VS 开发者提示符设置 INCLUDE 和 LIB。

Dead Ends

Common approaches that don't work:

  1. Reinstalling CUDA Toolkit without Visual Studio 95% fail

    The error is about the missing Visual Studio compiler, not CUDA itself; reinstalling CUDA does not add cl.exe to PATH.

  2. Setting PATH to a random directory containing cl.exe from a different architecture (e.g., x86 instead of x64) 80% fail

    nvcc requires the x64 native tools; using x86 cl.exe causes link errors or wrong object file format.

  3. Installing only the 'Desktop development with C++' workload but not launching from the correct developer command prompt 75% fail

    The environment variables (INCLUDE, LIB, PATH) are only set in the Visual Studio developer command prompt; a regular cmd or PowerShell does not inherit them.