nvcc 致命错误:在 PATH 中找不到编译器 'cl.exe'
nvcc fatal : Cannot find compiler 'cl.exe' in PATH
ID: cuda/nvcc-include-path-not-found
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| CUDA 12.0 | active | — | — | — |
| CUDA 11.8 | active | — | — | — |
| Visual Studio 2022 | active | — | — | — |
| Visual Studio 2019 | active | — | — | — |
根因分析
在 Windows 上,nvcc 需要在 PATH 中包含 Microsoft Visual C++ 编译器 (cl.exe),但构建环境缺少必要的 Visual Studio 工具或未配置正确的架构(x64 与 x86)。
English
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.
官方文档
https://docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows/index.html#compiler解决方案
-
从开始菜单打开“x64 Native Tools Command Prompt for VS 2022”(或 VS 2019),然后在该提示符下运行 nvcc 命令。这会自动设置所有必需的环境变量。
-
如果使用 CMake 等构建系统,设置 `CMAKE_GENERATOR` 为 'Visual Studio 17 2022' 并指定 `-A x64` 以确保使用正确的工具链。
-
手动将 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。
无效尝试
常见但无效的做法:
-
Reinstalling CUDA Toolkit without Visual Studio
95% 失败
The error is about the missing Visual Studio compiler, not CUDA itself; reinstalling CUDA does not add cl.exe to PATH.
-
Setting PATH to a random directory containing cl.exe from a different architecture (e.g., x86 instead of x64)
80% 失败
nvcc requires the x64 native tools; using x86 cl.exe causes link errors or wrong object file format.
-
Installing only the 'Desktop development with C++' workload but not launching from the correct developer command prompt
75% 失败
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.