代码之家  ›  专栏  ›  技术社区  ›  Luke Wright

LNK2019未引用函数main

  •  0
  • Luke Wright  · 技术社区  · 2 年前

    我正在从空白的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???
    
    }
    

    我以前从未发生过这种事?

    1 回复  |  直到 2 年前
        1
  •  1
  •   Stack Exchange Supports Israel    2 年前

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

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