C ++使用g ++返回“对WinMain @ 16的未定义引用”的Win32 API示例代码

问题描述

我正在将G ++编译器与notepad ++一起使用。

我正在按照Windows进行教程学习,我使用的代码是Windows教程(https://docs.microsoft.com/en-us/windows/win32/learnwin32/your-first-windows-program)中的示例代码,我将其粘贴到了记事本中并尝试对其进行编译,但是我对错误消息。

这是代码和消息:

#ifndef UNICODE
#define UNICODE
#endif 

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE,PWSTR pCmdLine,int nCmdShow)
{
    // Register the window class.
    const wchar_t CLASS_NAME[]  = L"Sample Window Class";
    
    WNDCLASS wc = { };

    wc.lpfnWndProc   = WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindowEx(
        0,// Optional window styles.
        CLASS_NAME,// Window class
        L"Learn to Program Windows",// Window text
        WS_OVERLAPPEDWINDOW,// Window style

        // Size and position
        CW_USEDEFAULT,CW_USEDEFAULT,NULL,// Parent window    
        NULL,// Menu
        hInstance,// Instance handle
        NULL        // Additional application data
        );

    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd,nCmdShow);

    // Run the message loop.

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

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd,LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd,&ps);



            FillRect(hdc,&ps.rcPaint,(HBRUSH) (COLOR_WINDOW+1));

            EndPaint(hwnd,&ps);
        }
        return 0;

    }
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

这是我用来编译的命令(powershell)

PS  g++ main.cpp

这是我尝试进行编译时得到的消息(powershell)

c:/ mingw / bin /../ lib / gcc / mingw32 / 6.3.0 /../../../ libmingw32.a(main.o):(。text.startup + 0xa0):未定义对“ WinMain @ 16”的引用 collect2.exe:错误:ld返回1退出状态

如果我尝试将wWinMain重命名为WinMain,则会出现错误

main.cpp:9:12:错误:C函数的声明'int WinMain(HINSTANCE,HINSTANCE,PWSTR,int)'int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,PWSTR pCmdLine,int nCmdShow)
在c:\ mingw \ include \ windows.h:44:0中包含的文件中,来自main.cpp:5:c:\ mingw \ include \ winbase.h:1263:14:注意:先前的声明'int WinMain(HINSTANCE ,HINSTANCE,LPSTR,int)'int APIENTRY WinMain(HINSTANCE,HINSTANCE,LPSTR,int);

如果我尝试使用命令g++ main.cpp -municode,则会收到消息

g ++。exe:错误:无法识别的命令行选项'-municode'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)