在OpenGL示例中重新定义WinMain错误

时间:2015-07-09 14:51:23

标签: c++ c opengl winmain

我试图通过this example:

与OpenGL取得联系
#include <windows.h>
#include <GL/GL.h>

#pragma comment (lib, "opengl32.lib")

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{
    MSG msg          = {0};
    WNDCLASS wc      = {0}; 
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
    wc.lpszClassName = L"oglversionchecksample";
    wc.style = CS_OWNDC;
    if( !RegisterClass(&wc) )
        return 1;
    CreateWindowW(wc.lpszClassName,L"openglversioncheck",WS_OVERLAPPEDWINDOW|WS_VISIBLE,0,0,640,480,0,0,hInstance,0);

    while( GetMessage( &msg, NULL, 0, 0 ) > 0 )
        DispatchMessage( &msg );

    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case WM_CREATE:
        {
        PIXELFORMATDESCRIPTOR pfd =
        {
            sizeof(PIXELFORMATDESCRIPTOR),
            1,
            PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,    //Flags
            PFD_TYPE_RGBA,            //The kind of framebuffer. RGBA or palette.
            32,                        //Colordepth of the framebuffer.
            0, 0, 0, 0, 0, 0,
            0,
            0,
            0,
            0, 0, 0, 0,
            24,                        //Number of bits for the depthbuffer
            8,                        //Number of bits for the stencilbuffer
            0,                        //Number of Aux buffers in the framebuffer.
            PFD_MAIN_PLANE,
            0,
            0, 0, 0
        };

        HDC ourWindowHandleToDeviceContext = GetDC(hWnd);

        int  letWindowsChooseThisPixelFormat;
        letWindowsChooseThisPixelFormat = ChoosePixelFormat(ourWindowHandleToDeviceContext, &pfd); 
        SetPixelFormat(ourWindowHandleToDeviceContext,letWindowsChooseThisPixelFormat, &pfd);

        HGLRC ourOpenGLRenderingContext = wglCreateContext(ourWindowHandleToDeviceContext);
        wglMakeCurrent (ourWindowHandleToDeviceContext, ourOpenGLRenderingContext);

        MessageBoxA(0,(char*)glGetString(GL_VERSION), "OPENGL VERSION",0);

        wglDeleteContext(ourOpenGLRenderingContext);
        PostQuitMessage(0);
        }
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;

}

我使用最新版本的Code::Blocks (v13.12)和MinGW-TDM-GCC-4.8.1
在我链接到 libopengl32.a 的链接器设置中,我使用了MinGW发行版中的 windows.h 和GL-header。

但是我收到了这个错误:

main.cpp|4|warning: ignoring #pragma comment  [-Wunknown-pragmas]|
main.cpp|8|error: 'int WinMain' redeclared as different kind of symbol|
codeblocks\mingw\include\winbase.h|1251|error: previous declaration of 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)'|
main.cpp|8|error: '__in' was not declared in this scope|
main.cpp|8|error: '__in_opt' was not declared in this scope|
main.cpp|8|error: '__in_opt' was not declared in this scope|
main.cpp|8|error: '__in' was not declared in this scope|

我错过了什么吗?或者此示例仅在使用Visual Studio时起作用(如编译指示所示)?

修改

我把所有内容都删掉了(没有opengl的东西)并且只有win头和它的WinMain,并且有相同的错误。

然后我将WinMain替换为标准main(see here

int main()
{
    HINSTANCE hInstance = GetModuleHandle( NULL );
    HINSTANCE hPrevInstance = 0;
    LPSTR lpCmdLine = NULL;
    int nShowCmd = 0;

    ...same as before

    return 0;
}

在我也链接到 libgdi32.a 之后编译没有错误,但是它只显示一个空命令promt。

编辑2

我删除了宽字符串(L""CreateWindowWCreateWindow),现在它可以正常工作..

1 个答案:

答案 0 :(得分:1)

我猜mingw不喜欢所有MSVC特定内容,删除__in和所有WinMain等等。我认为因为mingw无法解决那些无法匹配的{{1}它具有定义并失败。基本上重写了这个开头:

#include <windows.h>
#include <GL/GL.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)