1. 安装msvc编译工具
查询所有可用的vs产品
winget search Microsoft.VisualStudio
我们其实只要msvc就可以,也就是vs的编译工具,这里选择2022的buildtools(要与cuda版本适配,2019也可以)
winget install Microsoft.VisualStudio.2019.BuildTools --override "--passive --wait --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended"
-
Microsoft.VisualStudio.2019.BuildTools:安装 Visual Studio 生成工具,这比完整的 IDE 更轻量,专门用于命令行编译。
-
--override:用于将后面的参数直接传递给 Visual Studio 安装程序。
-
--add Microsoft.VisualStudio.Workload.VCTools:安装“使用 C++ 的桌面开发”核心组件(包含 MSVC 编译器 cl.exe)。
-
--includeRecommended:非常重要。它会自动包含编译程序必不可少的 Windows SDK 以及 CMake 等推荐工具。
-
--passive:显示安装进度但不需要你手动点击。
-
--wait:让命令在安装完全结束后再返回。
安装vs后会默认重启,可以添加 --norestart参数来禁止重启
winget install Microsoft.VisualStudio.2019.BuildTools --override "--passive --wait --norestart --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended"
也可以看一下有哪些版本,但是默认下载最新的版本就可以
winget show Microsoft.VisualStudio.2019.BuildTools --versions
2. 安装cuda toolkit
查询所有产品
winget search nvidia
查询想要cuda版本
winget show Nvidia.CUDA --versions
安装指定的版本
winget install Nvidia.CUDA --version 11.8
通过 winget 安装 CUDA 时,安装程序通常会使用静默模式,这往往会导致它跳过 Visual Studio 集成(Visual Studio Integration)组件,从而出现"No CUDA toolset found"的错.要解决这个问题,你不需要卸载重新安装,只需按照以下步骤手动补全缺失的构建文件:
-
打开 CUDA 的安装路径(默认): C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\extras\visual_studio_integration\MSBuildExtensions
-
全选并复制该文件夹下的所有文件(共 4 个左右,包括 CUDA 11.8.props 等)。
-
粘贴到 Visual Studio 的 BuildTools 对应目录下: C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Microsoft\VC\v160\BuildCustomizations
注意:手动安装勾选Visual Studio Integration就可以自动识别
2.1 驱动下载
Win + R后输入control /name Microsoft.DeviceManager打开设备管理器,找到显示适配器就可以看到显卡型号
根据GPU的型号下载 NVIDIA 官方驱动 | NVIDIA,如果是GeForce系列,直接到NVIDIA GeForce 驱动程序 - N 卡驱动 | NVIDIA下载即可,手动驱动搜索
安装完成后打开CMD或者powershell,输入
nvidia-smi
- 当前最新支持到了13。也就是最高可以运行使用 CUDA 13.0 编写的程序。
- 向下兼容:它也可以完美运行 CUDA 12.x 或 11.x 编写的旧程序。
3. 指定安装路径
默认都是安装在C盘,如果想要指定安装的位置,通常需要先下载,然后手动执行安装,并指定安装位置
winget download Microsoft.VisualStudio.2022.BuildTools --download-directory "D:\WingetDownloads"
winget download Nvidia.CUDA --version 11.8 --download-directory "D:\WingetDownloads"
-
作用:这只会把引导程序下载到 D:\WingetDownloads,不会开始安装。
-
下一步:去 D 盘双击那个 .exe,在安装界面手动选择安装路径。
当然也可以直接修改winget的下载路径
winget settings
在打开的 JSON 文件中加入
"installers": {
"installerDownloadsDirectory": "D:\\WingetDownloads"
}
3.1 VS安装
VS安装没有捷径了
3.2 CUDA安装
下载完成后,去 D:\WingetDownloads 找到那个 3GB 左右的 .exe 文件,手动双击运行。 关键省空间操作(必看):
- 临时解压路径:运行后第一个弹窗问路径,请改到 D:\CUDA_Temp。
- 安装类型:选 “自定义 (高级)”。
- 安装位置:在“安装位置”选项卡中,将 CUDA Toolkit 的路径从 C:\ 改为 D:\NVIDIA\CUDA\v11.8。
- 精简组件:
- 取消勾选 Samples(示例代码,占几百 MB)。
- 取消勾选 Documentation(文档)。
4. 编译llama.cpp
编译的时候需要指定显卡的计算能力,
# 配置项目,明确指定显卡计算能力为 6.1
cmake -B build -DCMAKE_CXX_FLAGS="/EHsc /wd4244 /wd4319 /wd4804 /wd4267 /utf-8" -DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=61
# 开始编译
cmake --build build --config Release --parallel
5. 遇到的问题
5.1 no kernel image is available for execution on the device
官方提供了cuda版本的预编译二进制,有cuda12和cuda13两个版本。我的cuda版本是13,但是运行的时候报错了
PS D:\llama> .\llama-cli.exe -m .\Qwen3.5-0.8B-Q8_0.gguf
ggml_cuda_init: found 1 CUDA devices (Total VRAM: 2047 MiB):
Device 0: NVIDIA GeForce MX250, compute capability 6.1, VMM: yes, VRAM: 2047 MiB
load_backend: loaded CUDA backend from D:\llama\ggml-cuda.dll
load_backend: loaded RPC backend from D:\llama\ggml-rpc.dll
load_backend: loaded CPU backend from D:\llama\ggml-cpu-haswell.dll
Loading model... /ggml_cuda_compute_forward: MUL failed
CUDA error: no kernel image is available for execution on the device
current device: 0, in function ggml_cuda_compute_forward at D:\a\llama.cpp\llama.cpp\ggml\src\ggml-cuda\ggml-cuda.cu:2819
err
D:\a\llama.cpp\llama.cpp\ggml\src\ggml-cuda\ggml-cuda.cu:98: CUDA error
这个错误的核心原因是:编译生成的 CUDA 内核代码(ggml-cuda.dll)不兼容显卡计算能力(Compute Capability) 。我的显卡是 NVIDIA GeForce MX250(计算能力 6.1),但编译时生成的 CUDA 内核仅支持更高版本的计算能力(比如 7.0+),导致显卡无法执行这些内核代码。
解决方案有
- 切换到 CPU 模式
- 寻找兼容旧显卡的版本
- 自行编译(彻底解决)
为了一劳永逸,我选择的解决办法就是自行编译。
5.2 nvcc fatal : Cannot find compiler 'cl.exe' in PATH
之前用msys2搭建了一套c++的编译环境,编译器是gcc,编译CPU版本的时候没有问题,但是编译cuda版本碰到了错误
Compiling the CUDA compiler identification source file "CMakeCUDACompilerId.cu" failed.
Compiler: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.0/bin/nvcc.exe Build flags: Id flags: --keep;--keep-dir;tmp -v
The output was: 1
nvcc fatal : Cannot find compiler 'cl.exe' in PATH
CUDA 的编译器 nvcc 在处理 .cu 文件时,会将代码拆分为“主机代码”(CPU 运行)和“设备代码”(GPU 运行)。
-
设备代码由 NVIDIA 自己的工具处理。
-
主机代码在 Windows 上必须交给 MSVC (cl.exe) 进行预处理和链接,因为 CUDA 的 Windows 版运行时库(Runtime Libraries)是针对 MSVC 的 ABI(应用二进制接口)构建的。GCC 和 MSVC 的 ABI 不兼容,强行混合会导致链接错误。
5.3 No CUDA toolset found
这个错误 No CUDA toolset found 是因为 Visual Studio (2022) 和 CUDA 10.2 不兼容 导致的。
最开始使用的是2026版本的vs,后面改成了2022版本,然后又改成了2019
CUDA 10.2 发布于 2019 年,它根本不认识 Visual Studio 2022 的编译器。 当你运行 CMake 时,它试图让 VS2022 调用 CUDA 10.2 的工具链,但因为版本跨度太大,VS2022 拒绝配合。
解决方案
- 需要先安装vs,然后再安装cuda,这样cuda就可以识别安装的vs版本
- 强行修复 CUDA 10.2(出于某种原因必须用 CUDA 10.2) ◦ 2.1 你需要安装旧版的 Visual Studio 2019。 ◦ 2.2 手动将 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\extras\visual_studio_integration\MSBuildExtensions 下的文件复制到 VS 的对应目录。
即便配置成功,你还是会卡在 “CUDA 10.2 不支持 C++17” 这个硬性要求上,最终还是无法编译 llama.cpp 的 CUDA 部分。对于编译llama.cpp而言,直接升级cuda即可。
5.4 Target "ggml-cuda" requires the language dialect "CUDA17"
由于显卡是MX250,安装的cuda版本是10.2(后来发现10,11,12都可以)导致版本较低
Target "ggml-cuda" requires the language dialect "CUDA17" (with compiler
extensions), but CMake does not know the compile flags to use to enable it.
llama.cpp 的较新版本要求 CUDA 代码必须以 C++17 模式编译。如果你的 CUDA Toolkit 版本太低,或者 CMake 找不到 CUDA 编译器路径,就会报这个错。
- 查看cmake版本 CMake 从 3.18 版本开始才正式支持 CMAKE_CUDA_STANDARD 17。如果你的版本低于此,它将无法为编译器生成正确的 C++17 标志。
cmake --version
- 查看nvcc版本 CUDA 10.2 自带的编译器 (nvcc) 最高只支持到 C++14。只有 CUDA 11.0 及以上版本才开始支持 C++17。
nvcc --version
5.5 "CMakeCUDACompilerId.cu" failed.
这个错误 CMakeCUDACompilerId.cu failed 通常意味着 CMake 尝试通过 nvcc 编译一段测试代码来识别编译器版本,但编译过程失败了。
Compiling the CUDA compiler identification source file "CMakeCUDACompilerId.cu" failed.
Compiler:
Build flags:
Id flags: --keep;--keep-dir;tmp -v
The output was: 1
通过 winget 安装的 CUDA,且遇到了 MSBuild 17.14 (VS 2022 v17.10+) 版本,最可能的原因是 CUDA 11.8 与最新版 Visual Studio 2022 的不兼容。
- 版本代差:CUDA 11.8 发布时,VS 2022 的工具集版本还没更新到 14.4x (对应 MSBuild 17.10+)。
- 编译器限制:nvcc 内部有一个版本检查机制,如果它发现 Visual Studio 的版本(_MSC_VER)太新,它会直接报错退出。
解决方案
- DCMAKE_CUDA_FLAGS="-allow-unsupported-compiler":这个方案没有用
- 装回2019,CUDA 11.8 发布时的主要适配对象就是 VS 2019