【问题标题】:Redefinition of WinMain Error in OpenGL example [duplicate]在 OpenGL 示例中重新定义 WinMain 错误 [重复]
【发布时间】:2015-09-28 00:54:51
【问题描述】:

我正在尝试通过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-headers。

但是我得到了这个错误:

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,但同样的错误。

然后我用它的标准 main (see here) 替换了 WinMain

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

    ...same as before

    return 0;
}

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


编辑 2

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

【问题讨论】:

标签: c++ c opengl winmain


【解决方案1】:

我猜 mingw 不喜欢所有特定于 MSVC 的东西,删除 #pragma comment 和所有 __in 等。我认为由于 mingw 无法解决这些问题,因此它无法将 WinMain 与它的定义匹配并失败。基本上把开头改写成这样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 2011-08-23
    • 2021-07-18
    相关资源
    最近更新 更多