c++中对WinMain@16的未定义引用

Undefined reference to WinMain@16 in C++

本文关键字:未定义 引用 WinMain@16 中对 c++      更新时间:2023-10-16

我正在上MSDN课程,用c++编程窗口,所以我尝试了他们的代码:

#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, 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, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return 0;
}
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);
        FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
        EndPaint(hwnd, &ps);
    }
    return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);

}

,因为我将它编译为:

g++ -c app.cpp -s app.o
g++ -o app.exe app.o -s -Wl,--subsystem,windows 

然后我得到错误:

 undefined reference to WinMain@16

缺乏什么?

wWinMain应该WinMain

编辑:好的,所以如果你想使用wWinMain的"宽"版本,我希望你需要定义-D_UNICODE,以便#include <windows.h>和其他位拾取正确的部分。(或者编辑#define UNICODE#define _UNICODE在你的源代码,我想)

正确的代码如下:

#ifndef UNICODE
#define UNICODE
#endif 
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(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, 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, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return 0;
}
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);
        FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
        EndPaint(hwnd, &ps);
    }
    return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
<<p> 链接器设置/strong>
g++ -c app.cpp -s app.o -lgdi32 -lmingw32
g++ -o app.exe app.o -s -Wl,--subsystem,windows -lgdi32 -lmingw32