c++ - C++ 中对 WinMain@16 的 undefined reference

标签 c++ winapi winmain

我正在学习使用 C++ 进行窗口编程的 MSDN 类(class),所以我尝试了他们的代码:

#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)

关于c++ - C++ 中对 WinMain@16 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16745174/

相关文章:

c++ - CUDA 对许多小型数组求和

c++ - Appverify 未捕获 C++ 堆损坏

c++ - Windows DLL 中的字符串表在哪里?

c++ - 这个 CreateWindowEx 函数有什么问题?

c - undefined reference `WinMain@16' collect2.exe : error: ld returned 1 exit status

C++/CX ^ 与 &

c++ - 安全派生指针的整数表示

c++ - 更改可警告/可等待线程的上下文

c++ - LNK2019:使用 WinMain 时,函数 ___tmainCRTStartup 中引用了无法解析的外部符号 _main

c++ - 在 main 之前未调用 WinMain(C/C++ 程序入口点问题)