Linker Error



  • Hallo ich habe ein kleines Problem mit meinem Programm. Ich möchte ein kleines OpenGL Spiel programmieren und schaffe es aber nicht den Code zu kompilieren, da ich diese Fehlermeldungen bekomme:

    [Linker error] undefined reference to `GetStockObject@4'
    [Linker error] undefined reference to `ChoosePixelFormat@8' 
    [Linker error] undefined reference to `SetPixelFormat@12' 
    [Linker error] undefined reference to `wglCreateContext@4'
    [Linker error] undefined reference to `wglMakeCurrent@8' 
    ld returned 1 exit status
    

    Das sind alle Fehlermeldungen die ich erhalte. Hier ist der reine Code:

    #include <windows.h>
    #include <gl/gl.h>
    #include <gl/glu.h>
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
    unsigned char key_pressed( HWND hWnd ); 
    MSG msg;
    void opengl_setup( HWND hWnd );
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "OpenGL Spiel";
    
    int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR IpCmdLine, int iCmdShow)
    
    {
        HWND                     hWnd;               /* Für das Handle dieses Fensters */
        WNDCLASSEX               WndClassEx;   /* Datenstruktur für die windowclass */
    
        char                     szClassName[] =     "ExtendedWndClass";
        char                     szWindowTitle[] =   "OpenGL Spiel"; 
    
        WndClassEx.cbSize           = sizeof( WNDCLASSEX );
        WndClassEx.style            = 0;
        WndClassEx.cbClsExtra       = 0;
        WndClassEx.cbWndExtra       = 0;
        WndClassEx.hInstance        = hInst;
    
        WndClassEx.hIcon            = LoadIcon ( hInst, IDI_APPLICATION );
        WndClassEx.hCursor          = LoadCursor ( hInst, IDC_ARROW );
        WndClassEx.hIconSm          = 0;    
        WndClassEx.hbrBackground    = ( HBRUSH ) GetStockObject ( BLACK_BRUSH );
    
        WndClassEx.lpfnWndProc      = WndProc;
        WndClassEx.lpszMenuName     = 0;
        WndClassEx.lpszClassName    = szClassName;
    
        if( !RegisterClassEx( &WndClassEx ) ) {
            MessageBox( NULL, "Fail to register WndClassEx!",
                        "Error", MB_OK | MB_ICONERROR );
        }
    
        hWnd = CreateWindow( szClassName, szWindowTitle,
                             WS_OVERLAPPEDWINDOW | WS_VISIBLE,                //WS_OVERLAPPEDNWINDOW ist für die normalen Zeichen, wie das "X" oder der Unterstrich. Zum schließen oder klein machen des Fensters
                             CW_USEDEFAULT, CW_USEDEFAULT,                    //Standard Fensterkoordinaten.
                             640, 420, 0, 0, hInst, 0 );
    
        if ( !hWnd ) {
             MessageBox( 0, "Failed to create Window!", "Error",
                         MB_OK | MB_ICONERROR );
        }
    
        ShowWindow( hWnd, iCmdShow );
        UpdateWindow( hWnd );
        opengl_setup( hWnd );
    
        while ( true ) {
              if( key_pressed( hWnd ) ) {
                  break;
              }
    
        }
        return (int) msg.wParam;
    }
    
    LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
    
            switch( msg ) {
               case WM_DESTROY:
                    PostQuitMessage(0);
                    break;
               default:
                    return DefWindowProc( hWnd, msg, wParam, lParam );
                    break;
            }
    return 0;
    }
    
    unsigned char key_pressed( HWND hWnd ) {
             if( PeekMessage( &msg, hWnd, 0, 0, PM_REMOVE ) ) {
                 if( msg.message == WM_QUIT || msg.message == WM_KEYDOWN ) {
                     return true;
                 }
                 TranslateMessage( &msg );
                 DispatchMessage(  &msg );
             } 
             return 0;        
    }
    
    void opengl_setup( HWND hWnd ){
         HDC                           hDc; //Device Context
         HGLRC                         hGLRC;     
         PIXELFORMATDESCRIPTOR         pfd;
         int iFormat;
    
         memset( &pfd, 0, sizeof( pfd ) );
         pfd.nSize                = sizeof( PIXELFORMATDESCRIPTOR );
         pfd.nVersion             = 1;
         pfd.dwFlags              = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
         pfd.iPixelType           = PFD_TYPE_RGBA;
         pfd.cColorBits           = 32;
         pfd.cDepthBits           = 32;
         pfd.cAlphaBits           = 8;
    
         hDc = GetDC( hWnd );
         iFormat = ChoosePixelFormat( hDc, &pfd );
         SetPixelFormat( hDc, iFormat, &pfd );
    
         hGLRC = wglCreateContext( hDc );
         wglMakeCurrent( hDc, hGLRC );
    }
    

    Ich habe den Code mit Dev C++ geschrieben und ich bin eher noch unerfahren, was das angeht. Es soll ein einfaches Fenster werden, dem später noch mehr OpenGl hinzugefügt werden soll. Fehler sollten eigentlich auch keine mehr drin sein! Bis auf die Linkerfehler!

    Grüße



    1. Du hast offenbar vergessen, opengl32.lib und gdi32.lib zu linken.
    2. Nimm Visual C++.


  • Sind in Visual Studio 2012 die beiden Bibliotheken schon eingefügt?
    Oder muss ich die manuel einfügen?


  • Mod

    Bei VC Projekten werden diese Libraries automatisch hinzugefügt.



  • opengl32.lib musst du auch in VC selbst eintragen, daran sollt's aber nicht scheitern...


Anmelden zum Antworten