# 运行时错误：NVRTC 编译失败：错误：无法打开源文件 'cuda.h'

- **ID:** `cuda/nvrtc-cannot-open-source-file-cuda`
- **领域:** cuda
- **类别:** build_error
- **错误码:** `NVRTC_ERROR_COMPILATION`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

NVRTC（CUDA 运行时编译）库无法找到 CUDA 头文件，因为 CUDA 包含路径未正确设置或未安装 CUDA 工具包。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CUDA 11.7 | active | — | — |
| CUDA 12.0 | active | — | — |
| CUDA 12.2 | active | — | — |

## 解决方案

1. ```
   Install the full CUDA toolkit (e.g., version 12.2) and set the CUDA_PATH environment variable to the installation directory. For Ubuntu: wget https://developer.download.nvidia.com/compute/cuda/12.2.0/local_installers/cuda_12.2.0_535.54.03_linux.run; sudo sh cuda_12.2.0_535.54.03_linux.run --toolkit --silent --toolkitpath=/usr/local/cuda-12.2; export CUDA_PATH=/usr/local/cuda-12.2; export CPATH=$CUDA_PATH/include:$CPATH; then retry NVRTC compilation.
   ```
2. ```
   If using conda, install the cuda-toolkit package: conda install -c nvidia cuda-toolkit=12.2. This places headers in the conda environment's include directory, which NVRTC can find automatically.
   ```

## 无效尝试

- **** — Reinstalling PyTorch doesn't fix missing CUDA headers because it ships with its own CUDA runtime but not the full toolkit headers. (60% 失败率)
- **** — Setting CUDA_HOME to a non-existent directory is a common mistake that doesn't resolve the include path. (80% 失败率)
