CMake找不到CUDA,显示错误:CUDA工具包目录 '' 不存在

编程语言 2026-07-09

我正在尝试使用CMake在 Windows 11上配置一个项目,系统中已安装MSVC和 CUDA 13.2。CMakeLists.txt 文件是:

cmake_minimum_required(VERSION 4.2 FATAL_ERROR)
PROJECT(hello LANGUAGES CUDA)
find_package(CUDAToolkit REQUIRED)

当我 cmake -B build 时,我得到一段很长的错误信息,为了简洁起见我在编辑:

CMake Error (snip)
CMakeDetermineCompilerId.cmake:928 (message):
  Compiling the CUDA compiler identification source file
  "CMakeCUDACompilerId.cu" failed.
(snip)
error : The CUDA Toolkit directory '' does not exist.
  Please verify the CUDA Toolkit is installed properly or define the
  CudaToolkitDir property to resolve this error.

为什么CMake认为存在一个空的工具包目录?“CudaToolkitDir属性”是什么?

解决方案

CudaToolkitDir属性是什么?

CudaToolkitDir 是一个环境变量。请按如下方式设置,且不要使用双引号括起来:

set CudaToolkitDir=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.2

(我使用了CUDA 13.2的默认安装位置,当然可以根据你自己系统的实际情况来调整。)

现在重新运行配置:

cmake -B build --fresh

并且CUDA应该能被检测到;你应该会看到类似下面的输出:

-- Building for: Visual Studio 18 2026
-- Selecting Windows SDK version 10.0.26100.0 to target Windows 10.0.22631.
-- The CUDA compiler identification is NVIDIA 13.2.51 with host compiler MSVC 19.50.35728.0
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Check for working CUDA compiler: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.2/bin/nvcc.exe - skipped
-- Detecting CUDA compile features
-- Detecting CUDA compile features - done
-- Found CUDAToolkit: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.2/include;C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.2/include/cccl (found version "13.2.51")
-- Configuring done (13.3s)
-- Generating done (0.1s)
-- Build files have been written to: C:/Users/joeuser/src/hello/build

为什么CMake会认为工具包目录是空的?

我会说那是一个错误;没有充分的理由让CMake不检测到CUDA的安装位置。不过——看起来CMake似乎确实需要你把信息逐字地喂给它。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章