【问题标题】:Compiling Windows program in Dev-C++ gives error在 Dev-C++ 中编译 Windows 程序会出错
【发布时间】:2011-07-18 23:34:29
【问题描述】:

我只是在学习用 C++ 编写 Windows 程序并使用 Dev-C++ IDE。我尝试编译的第一个程序是 MSDN 站点中的以下示例:

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

但是当我尝试编译它时,我得到以下错误:

C:\Dev-Cpp\lib/libmingw32.a(main.o)(.text+0x106):main.c: 未定义对 `WinMain@16' 的引用

【问题讨论】:

    标签: windows compiler-construction dev-c++


    【解决方案1】:

    mingw 运行时似乎没有正确配置 Unicode 支持,因为您提供了 Unicode wWinMain,它正在寻找 ANSI 版本。

    您可以切换到 Unicode 中性编程(定义 _tWinMain 并使用 LPTSTR 代替 LPWSTR,也可以使用 _T("string") 代替 L"string")。

    为此,您还必须#include &lt;tchar.h&gt;

    【讨论】:

      【解决方案2】:

      对于旧版本的 MinGW,您可以使用包装器。

      对于新版本的 MinGW,您应该使用 -municode 选项。

      详情见:

      【讨论】:

        【解决方案3】:

        你写了 int "WINAPI wWinMain"。删除多余的 w。它应该是“WINAPI WinMain”

        【讨论】:

          猜你喜欢
          • 2015-05-31
          • 2015-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-08
          • 1970-01-01
          • 2011-05-16
          • 2011-01-10
          相关资源
          最近更新 更多