windows - 在 Dev-C++ 中编译 Windows 程序会出错

标签 windows compiler-construction dev-c++

我正在学习用 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' 的 undefined reference

最佳答案

它看起来像 mingw运行时未正确配置 Unicode 支持,因为您提供了 Unicode wWinMain它正在寻找 ANSI 版本。

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

为此,您还必须 #include <tchar.h> .

关于windows - 在 Dev-C++ 中编译 Windows 程序会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5366208/

相关文章:

c - 弹性错误 matchtype.h

java - 找不到符号错误,拼写错误和参数均不正确

c++ - 开发 C++ (Mingw) 堆栈限制

c++ - C++中的两个连续for循环,第二个循环不起作用

arrays - 动态数组可以用作 Windows 回调函数的参数吗?

java - 从 Java ProcessBuilder 运行 OpenMPI 进程时,ompi_evesel->dispatch() 失败

c++ - 渲染各向异性纹理的更快方法?

windows - Qt Creator 项目向导不显示创建 Qt 项目的选项

c - 外部内联函数会发生什么?

visual-studio-2010 - 哪些编译器支持 CUDA