2011-03-20 77 views
2

我刚学习使用C++编程Windows程序并使用Dev-C++ IDE。我想要编译第一个程序是从MSDN网站下面的例子:在Dev-C++中编译Windows程序给出错误

#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:\开发-CPP \ lib/libmingw32.a(main.o)(.text + 0x106):main.c:对'WinMain @ 16'的未定义引用

回答

2

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

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

要做到这一点,您还必须#include <tchar.h>

+0

谢谢,我会试试这个,让你知道它是否有效。 – ldemarco 2011-03-20 03:08:20

0

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

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

有关详细信息,请参阅:

-1

你写INT “WINAPI wWinMain。”删除额外的w。它应该是“WINAPI WinMain”