如何在VS Code中通过cl.exe编译32位可执行文件?

编程语言 2026-07-12

在64位 Windows 10操作系统中,我想用cl.exe结合Visual Studio Code来编译一个32位的Windows可执行文件。我已经安装了C/C++、CMAKE和 C/C++ DevTools扩展,并安装了Visual C++编译器以及其他开发工具。

我可以在VS Code中通过 cl.exe 成功编译出64位的可执行文件(命令行程序,以及GUI程序)。

为了在不需要重启VS Code的情况下编译32位可执行文件(或至少尽量如此),我应该怎么做?是否可以通过修改 tasks.json 在32位和64位之间切换?

tasks.json 中,我用(带引号)指定了似乎是x86的 cl.exe 路径,但编译时出现大量“未解析符号”的链接器 LNK2019 致命错误以及 warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86' 警告。

所有警告的位置都是: C:\Program Files (x86)\Windows Kits\10\\lib\10.0.19041.0\\um\x64\user32.lib : warning LNK4272: ... 所以我大概需要修改静态库的位置。但在VS Code及这些扩展中,应该在哪里进行?

当前 tasks.json

  // C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\um\x86
 {
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "\"C:/Program Files (x86)/Microsoft Visual Studio/18/BuildTools/VC/Tools/MSVC/14.50.35717/bin/Hostx64/x86/cl.exe\"",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}",
                "/link kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

解决方案

所以问题大致出在双斜杠和 vc.exe 参数语法上。

  1. link 参数项中,我需要对反斜杠再进行一次转义,以便正确处理 LIBPATH
  2. 需要链接的具体库应在该 LIBPATH 参数之后列出,放在它所在的位置
  3. 还要确保 command 的值是用双引号括起来

下面给出可工作的文件,唯一的问题是要从 x64 切换到 x86 时,需要手动把所有 x86 条目改为 x64。也许我可以通过某种方式让VS Code,或C/C++扩展的变量来实现?参见 这里

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "\"C:/Program Files (x86)/Microsoft Visual Studio/18/BuildTools/VC/Tools/MSVC/14.50.35717/bin/Hostx64/x86/cl.exe\"",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}",
                "/link /LIBPATH:\\\"C:/Program Files (x86)/Microsoft Visual Studio/18/BuildTools/VC/Tools/MSVC/14.50.35717/lib/x86\\\" /LIBPATH:\\\"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.19041.0/um/x86\\\" kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib Uuid.lib odbc32.lib odbccp32.lib /LIBPATH:\\\"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.19041.0/ucrt/x86\\\""
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章