LNK2019未引用函数main

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

我正在从空白的 C++ Windows 模板创建 d3d12 项目,但收到此错误:

1>MSVCRTD.lib(exe_main.obj):错误 LNK2019:函数“int __cdecl invoke_main(void)”中引用的未解析的外部符号 main (?invoke_main@@YAHXZ)

我读了另一篇文章,其中提到了有关 msvc 运行时库未链接的问题,因为它是一个空白模板?这不可能是我的情况的问题吧?我已经尝试链接在 https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features?view=msvc-170#c-standard-library-stl 找到的文件-lib-files,正如预期的那样,没有任何变化。

这是我的代码(c20):

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <WinUser.h>

#include <iostream>
#include <optional>

LRESULT CALLBACK MainWndProc
( // main window lifecycle manager
    HWND hwnd, // handle to the associated window
    UINT uMsg, // message identifier
    WPARAM wParam, // first mesage parameter
    LPARAM lParam // second message paremeter
)
{
    switch (uMsg)
    {
    case WM_CREATE:
        // init adapters, create swapchain, and prepare sample
        std::cout << "in lfcm" << std::endl;
        return LRESULT(0);
    case WM_SIZE:
        // resize swapchain
        return LRESULT(0);
    case WM_DESTROY:
        // cleanup
        return LRESULT(0);
    default:
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return LRESULT(0);

};

std::optional<HWND> WinInit(HINSTANCE hInstance) { // window creation factory // register window class, create window, TODO: initialize swapchain

    // create and configure the window class

    wchar_t WCLANN[] = L"window class";

    WNDCLASS cl = {};
    HWND hWnd;
    ZeroMemory(&cl, sizeof(WNDCLASS)); // pretty self explanatory

    cl.lpfnWndProc = MainWndProc;
    cl.hInstance = hInstance;
    cl.lpszClassName = WCLANN;

    RegisterClass(&cl); // register the window class

    hWnd = CreateWindowEx
    (
        0, // window styles (optional)
        WCLANN, // associated window class
        L"Window text", // Window text
        WS_OVERLAPPEDWINDOW, // Window style

        CW_USEDEFAULT, // use default x 
        CW_USEDEFAULT, // use default y
        1280, // width
        720, // height

        NULL, // Parent window
        NULL, // Menu
        hInstance, // Instance handle
        NULL // additional application data
    );

    if (hWnd == HWND(0))
    {
        // window creation factory failed; return
        return std::nullopt;
    }

    return hWnd;

};

int APIENTRY WinMain
( // windows specific program entrypoint
    _In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPSTR lpCmdLine,
    _In_ int nCmdShow
)
{
    std::optional<HWND> inVT = WinInit(hInstance); // CHANGE TO REFLECT OPTIONAL CHANGES
    if (inVT.has_value()) // window creation succeeded, window handle returned
    {
        // do something with the handle to the window
        std::cout << inVT.value();
    }
    else // window creation failed, no window handle to return
    {
        // log
        std::cout << "window creation factory failure" << std::endl;
    }

    std::cout << "window created" << std::endl;

    while (true) {}; // ??? why won't this work???

}

我以前从未遇到过这种情况?

c++ winapi
1个回答
0
投票

您使用了控制台应用程序模板,但代码适用于 Windows 应用程序。

有多种方法可以调整设置以使其成为 Windows 应用程序,但由于您没有太多代码,最简单的解决方案是从 Windows 应用程序模板创建一个新项目,复制代码并删除旧项目。

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