PAYATHON 2026

如何永久配置 VSCode 使用 cl.exe

技术老齐

结论

可以。将 MSVC 编译器和 Windows SDK 的可执行文件目录加入 Windows 的 Path,再永久配置 INCLUDE 和 LIB。配置完成后,即使从开始菜单直接启动 VS Code,也可以运行 cl.exe、link.exe 和调试程序。

Developer Command Prompt 使用的也是普通的 cl.exe,只是通过 VsDevCmd.bat 动态设置所需的环境变量。手动配置会将这套环境固定下来。MSVC 或 Windows SDK 升级后,安装目录中的版本号可能变化,需要相应更新环境变量。

确认安装组件

打开 Visual Studio Installer,确认 Visual Studio 2022 已安装“使用 C++ 的桌面开发”工作负载,并至少包含以下组件:

  • MSVC C/C++ 编译工具
  • Windows SDK
  • C++ 调试支持

如果电脑上只有 VS Code,没有安装 Visual Studio 或 Visual Studio Build Tools,系统中就没有完整的 cl.exe 工具链。

查找实际安装路径

每台电脑安装的 MSVC 和 Windows SDK 版本可能不同,不要直接套用其他电脑上的版本号。

先临时打开一次 Developer Command Prompt,执行:

where cl
where link
echo %VCToolsInstallDir%
echo %WindowsSdkDir%
echo %WindowsSDKVersion%
echo %INCLUDE%
echo %LIB%

输出通常类似:

VCToolsInstallDir=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\<MSVC版本>\
WindowsSdkDir=C:\Program Files (x86)\Windows Kits\10\
WindowsSDKVersion=<SDK版本>\

将 <MSVC版本> 和 <SDK版本> 替换为本机的实际目录名。可以分别查看以下目录:

C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC
C:\Program Files (x86)\Windows Kits\10\Include

如果安装的是 Build Tools、Professional 或 Enterprise,路径中的产品名称也会不同。

永久配置 Windows 环境变量

打开“系统属性”中的“环境变量”,在用户变量中添加所需变量。如果只有当前账户使用 VS Code,一般不必修改系统变量。

先创建以下变量:

VCToolsInstallDir
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\<MSVC版本>\

WindowsSdkDir
C:\Program Files (x86)\Windows Kits\10\

WindowsSDKVersion
<SDK版本>\

WindowsSDKVersion 通常需要保留末尾的反斜杠,例如:

10.0.xxxxx.0\

接着向现有的 Path 追加以下目录,不要覆盖原有内容:

%VCToolsInstallDir%bin\Hostx64\x64
%WindowsSdkDir%bin\%WindowsSDKVersion%x64

创建 INCLUDE:

%VCToolsInstallDir%include;%WindowsSdkDir%Include\%WindowsSDKVersion%ucrt;%WindowsSdkDir%Include\%WindowsSDKVersion%shared;%WindowsSdkDir%Include\%WindowsSDKVersion%um;%WindowsSdkDir%Include\%WindowsSDKVersion%winrt;%WindowsSdkDir%Include\%WindowsSDKVersion%cppwinrt

创建 LIB:

%VCToolsInstallDir%lib\x64;%WindowsSdkDir%Lib\%WindowsSDKVersion%ucrt\x64;%WindowsSdkDir%Lib\%WindowsSDKVersion%um\x64

以上配置适用于 x64 主机和 x64 目标程序。要生成 x86 程序,需要改用对应的工具及库目录,例如:

%VCToolsInstallDir%bin\Hostx64\x86
%VCToolsInstallDir%lib\x86
%WindowsSdkDir%Lib\%WindowsSDKVersion%ucrt\x86
%WindowsSdkDir%Lib\%WindowsSDKVersion%um\x86

保存后,完全退出并重新启动 VS Code。已经运行的程序不会自动读取新的环境变量。如果重启 VS Code 后仍未生效,可以注销 Windows 账户并重新登录。

验证环境

在普通 PowerShell、命令提示符或 VS Code 集成终端中执行:

where cl
where link
cl

cl 应显示 Microsoft C/C++ 编译器的信息,而不是提示“无法识别命令”。

然后创建一个测试文件:

#include <stdio.h>

int main(void)
{
    printf("Hello from MSVC\n");
    return 0;
}

编译并运行:

cl /nologo /Zi /Fe:hello.exe hello.c
hello.exe

如果可以找到 cl.exe,但编译时提示找不到 stdio.h,通常是 INCLUDE 配置有误。如果编译成功,链接时却出现 LNK1104,需要检查 LIB、目标架构和 SDK 版本。

配置 VS Code 编译任务

在工作区的 .vscode/tasks.json 中可以直接调用 cl.exe:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "process",
            "label": "Build current file with cl.exe",
            "command": "cl.exe",
            "args": [
                "/nologo",
                "/Zi",
                "/Od",
                "/Fe:${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

这里的 "type": "process" 会让 VS Code 直接启动 cl.exe,无需额外编写批处理脚本。.c 文件按 C 语言编译,.cpp 文件按 C++ 编译。如果文件扩展名不能反映实际语言类型,可以使用 /TC 或 /TP 强制指定。

配置调试

安装 Microsoft C/C++ 扩展后,可以在 .vscode/launch.json 中配置 Visual Studio Windows 调试器:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug current program",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "console": "integratedTerminal",
            "preLaunchTask": "Build current file with cl.exe"
        }
    ]
}

编译任务中的 /Zi 用于生成调试信息,/Od 用于关闭优化,便于单步调试和查看变量。

compilerPath 不能代替环境变量

可以在 c_cpp_properties.json 中配置:

{
    "configurations": [
        {
            "name": "Win32",
            "compilerPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\<MSVC版本>\\bin\\Hostx64\\x64\\cl.exe",
            "intelliSenseMode": "windows-msvc-x64",
            "cStandard": "c17",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

这项配置主要帮助 IntelliSense 识别编译器、系统头文件和语言模式。它不会替构建任务设置 PATH、INCLUDE 和 LIB,单独配置 compilerPath 无法解决链接环境问题。

注意事项

手动固定环境变量适合工具链版本长期不变的电脑,但使用时要留意以下情况:

  • Visual Studio 更新后可能生成新的 MSVC 目录,旧目录以后也可能被删除。
  • Windows SDK 更新后,可能需要修改 WindowsSDKVersion、INCLUDE 和 LIB。
  • x64 与 x86 的编译器和库目录不能混用,否则可能发生目标架构冲突。
  • 同时安装多个 Visual Studio 或 SDK 版本时,全局环境只能默认指向其中一套工具链。
  • 不建议使用 setx PATH ... 随意重写 Path,操作失误可能导致现有条目丢失。通过“环境变量”界面逐项追加更便于检查。

如果经常切换 MSVC 版本、SDK 版本或目标架构,使用 VsDevCmd.bat、CMake Presets 或由构建系统管理环境通常更合适。版本和目标架构固定时,永久配置 Windows 环境变量,就能让普通方式启动的 VS Code 直接使用 cl.exe。

备注:内容仅供参考。