You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code无法编译C++桌面程序(Visual Studio可编译)的解决方法

问题:VS Code编译Windows桌面程序出现链接错误

已按VS Code官方文档配置MSVC环境,简单HelloWorld可正常编译,但Windows桌面程序在VS Code编译失败,Visual Studio 2022可正常编译。

程序代码(hello.cpp)

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        RECT rect;
        GetClientRect(hwnd, &rect);
        DrawTextW(hdc, L"Hi", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

        EndPaint(hwnd, &ps);
    }
    return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

int main()
{
    WNDCLASSW wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = GetModuleHandleW(nullptr);
    wc.lpszClassName = L"HelloWindowClass";
    RegisterClassW(&wc);

    HWND hwnd = CreateWindowW(
        L"HelloWindowClass",
        L"hi",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        400,
        300,
        nullptr,
        nullptr,
        GetModuleHandleW(nullptr),
        nullptr
    );

    if (hwnd == nullptr)
    {
        return 1;
    }

    ShowWindow(hwnd, SW_SHOWNORMAL);
    UpdateWindow(hwnd);

    MSG msg = {};
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

当前tasks.json配置

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

编译错误输出

*  Executing task: C/C++: cl.exe build active file 

Starting build...
cl.exe /Zi /EHsc /nologo /FeC:\Dev\learncpp\testwin\hello.exe C:\Dev\learncpp\testwin\hello.cpp
hello.cpp
hello.obj : error LNK2019: unresolved external symbol __imp__GetMessageA@16 referenced in function _main
hello.obj : error LNK2019: unresolved external symbol __imp__TranslateMessage@4 referenced in function _main
hello.obj : error LNK2019: unresolved external symbol __imp__DispatchMessageA@4 referenced in function _main
hello.obj : error LNK2019: unresolved external symbol __imp__DefWindowProcA@16 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowProc@@YGJPAUHWND__@@IIJ@Z)
hello.obj : error LNK2019: unresolved external symbol __imp__PostQuitMessage@4 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowProc@@YGJPAUHWND__@@IIJ@Z)
hello.obj : error LNK2019: unresolved external symbol __imp__RegisterClassW@4 referenced in function _main
hello.obj : error LNK2019: unresolved external symbol __imp__CreateWindowExW@48 referenced in function _main
hello.obj : error LNK2019: unresolved external symbol __imp__ShowWindow@8 referenced in function _main
hello.obj : error LNK2019: unresolved external symbol __imp__DrawTextW@20 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowProc@@YGJPAUHWND__@@IIJ@Z)
hello.obj : error LNK2019: unresolved external symbol __imp__UpdateWindow@4 referenced in function _main
hello.obj : error LNK2019: unresolved external symbol __imp__BeginPaint@8 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowProc@@YGJPAUHWND__@@IIJ@Z)
hello.obj : error LNK2019: unresolved external symbol __imp__EndPaint@8 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowProc@@YGJPAUHWND__@@IIJ@Z)
hello.obj : error LNK2019: unresolved external symbol __imp__GetClientRect@8 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowProc@@YGJPAUHWND__@@IIJ@Z)
C:\Dev\learncpp\testwin\hello.exe : fatal error LNK1120: 13 unresolved externals

Build finished with error(s).

 *  The terminal process failed to launch (exit code: -1). 
 *  Terminal will be reused by tasks, press any key to close it. 
解决方案

这些LNK2019错误是因为编译时没有链接Windows用户界面库(user32.lib),你的代码调用了Windows API,需要显式告诉链接器引入这个库。修改.vscode/tasks.json中的args数组,添加链接参数:

修改后的tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}",
                "/link",
                "user32.lib"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

核心改动是在args末尾添加了"/link"和"user32.lib",告诉cl.exe在链接阶段引入user32.lib,这个库包含了所有你调用的Windows窗口相关API的实现。

重新执行编译任务即可正常生成可执行文件。

内容的提问来源于stack exchange,提问作者pupadupa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 11:15:54