vscode环境出现winmain@16错误

问题描述 投票:0回答:1

我正在使用 msvc 编译器创建窗口。出现链接错误,但问题很难解决,因为我对vscode不熟悉

#include <windows.h>

#pragma comment(linker, "/entry:wWinMainCRTStartup /subsystem:console")

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PWSTR pCmdLine, int nCmdShow) {

    MSG msg;
    HWND hwnd;
    WNDCLASSW wc;

    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.lpszClassName = L"Window";
    wc.hInstance     = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpszMenuName  = NULL;
    wc.lpfnWndProc   = WndProc;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);

    RegisterClassW(&wc);
    hwnd = CreateWindowW(wc.lpszClassName, L"Window",
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                100, 100, 350, 250, NULL, NULL, hInstance, NULL);

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, NULL, 0, 0)) {

        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
    WPARAM wParam, LPARAM lParam) {

    switch(msg) {

      case WM_DESTROY:

          PostQuitMessage(0);
          break;
    }

    return DefWindowProcW(hwnd, msg, wParam, lParam);
}


我已经确认在使用g++时不保存编译时也会出现类似的错误,但在我的例子中是msvc。 对于 msvc,它在 2019 年通过 Developer power shell 使用。

winapi visual-c++ lnk2019
1个回答
0
投票

您链接到控制台 sybsystem 选项,但您缺少

main
功能。控制台应用程序以
main
.

开头

要么创建一个

main
函数,要么与“Windows”子系统链接以从
WinMain
开始。

也许根本不需要

/entry:wWinMainCRTStartup

© www.soinside.com 2019 - 2024. All rights reserved.