0

I'm trying to get in touch with OpenGL with this example:

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

}

I'm using the latest version of Code::Blocks (v13.12) with MinGW-TDM-GCC-4.8.1
In the linker settings I linked against libopengl32.a and I used the windows.h and GL-headers from the MinGW distribution.

However I get this error:

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|

Did I miss something? Or does this example only work when using Visual Studio (as the pragma looks like)?


EDIT

I cut everything out (no opengl stuff) and have had only the win header and its WinMain, with the same error.

Then I replaced WinMain with its standard main (see here)

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

    ...same as before

    return 0;
}

And it compiled without errors after I also linked against libgdi32.a, however it only shows an empty command promt then.


EDIT 2

I Removed the wide strings (L"" and CreateWindowW to CreateWindow) and now it works..

Community
  • 1
  • 1
nonsensation
  • 3,627
  • 6
  • 28
  • 41
  • 1
    [Compile error with winmain and simple code, "Previous declaration of WinMain"](https://stackoverflow.com/questions/66736489/compile-error-with-winmain-and-simple-code-previous-decleration-of-winmain). Your declaration of `WinMain` is incorrect. You forgot to say `APIENTRY`. – Raymond Chen Oct 29 '21 at 13:46

1 Answers1

1

I guess mingw does not like all the MSVC specific stuff, remove the #pragma comment and all the __in, etc. I think that since mingw cannot resolve those it can't match WinMain with the definition it has and fails. Basically rewrite the start to this:

#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)
Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71