MinGW找不到OpenGL命令

时间:2014-01-19 16:12:27

标签: c++ opengl mingw

我正在尝试编译使用OpenGL的c ++应用程序。所有包含都在那里,libopengl32.a和libglu.a都在我的MinGW lib文件夹中。我的OpenGL c ++文件如下所示:

#include <GL/gl.h>
#include <GL/glu.h>
#include <pthread.h>
#include <windows.h>

#include "GL.h"

//using namespace std;

HDC hDC;
PIXELFORMATDESCRIPTOR pfd;
HGLRC hRC;

BOOL ren = false;
pthread_t t;

void initGL(HWND hWnd)
{
    hDC = GetDC(hWnd);
    ReleaseDC(hWnd, hDC);

    pfd.nSize = sizeof(pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    int iFormat = ChoosePixelFormat(hDC, &pfd);
    SetPixelFormat(hDC, iFormat, &pfd);

    hRC = wglCreateContext(hDC);
}

void destroyGL()
{
    wglDeleteContext(hRC);
}

void renderGL()
{
    wglMakeCurrent(hDC, hRC);

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin( GL_TRIANGLES );
    glColor3f( 1.0f, 0.0f, 0.0f ); glVertex2f( 0.0f, 1.0f );
    glColor3f( 0.0f, 1.0f, 0.0f ); glVertex2f( 0.87f, -0.5f );
    glColor3f( 0.0f, 0.0f, 1.0f ); glVertex2f( -0.87f, -0.5f );
    glEnd();
    glPopMatrix();

    SwapBuffers(hDC);

    wglMakeCurrent(NULL, NULL);
}

void *RenderLoop(void *args)
{
    while(ren)
    {
        renderGL();
    }
    return 0;
}

void StartRender()
{
    ren = true;
    pthread_create(&t, NULL, RenderLoop, NULL);
}

void StopRender()
{
    ren = false;
}

代码看起来很好,但是当我编译它时,我得到了这个:

GL.cpp:(.text+0x1c): undefined reference to `_imp____glutInitWithExit
GL.cpp:(.text+0x3e): undefined reference to `_imp____glutCreateWindow
GL.cpp:(.text+0x60): undefined reference to `_imp____glutCreateMenuWi
GL.cpp:(.text+0x118): undefined reference to `wglCreateContext@4'
GL.cpp:(.text+0x135): undefined reference to `wglDeleteContext@4'
GL.cpp:(.text+0x157): undefined reference to `wglMakeCurrent@8'
GL.cpp:(.text+0x182): undefined reference to `glClearColor@16'
GL.cpp:(.text+0x191): undefined reference to `glClear@4'
GL.cpp:(.text+0x1a0): undefined reference to `glBegin@4'
GL.cpp:(.text+0x1c2): undefined reference to `glColor3f@12'
GL.cpp:(.text+0x1db): undefined reference to `glVertex2f@8'
GL.cpp:(.text+0x1fd): undefined reference to `glColor3f@12'
GL.cpp:(.text+0x216): undefined reference to `glVertex2f@8'
GL.cpp:(.text+0x238): undefined reference to `glColor3f@12'
GL.cpp:(.text+0x251): undefined reference to `glVertex2f@8'
GL.cpp:(.text+0x259): undefined reference to `glEnd@0'
GL.cpp:(.text+0x25e): undefined reference to `glPopMatrix@0'
GL.cpp:(.text+0x282): undefined reference to `wglMakeCurrent@8'
GL.cpp:(.text+0x2dd): undefined reference to `_imp__pthread_create'

我像这样编译我的代码:

windres res.rc -o res.o
g++ -static -static-libgcc -static-libstdc++ -s -mwindows -c win.cpp GL.cpp
g++ -static -static-libgcc -static-libstdc++ -s -mwindows -lopengl32 -lglu32 win.o GL.o res.o -o bin/win.exe

1 个答案:

答案 0 :(得分:1)

似乎g ++没有找到2个lib中的任何一个。尝试使用 -L 选项在g ++命令中显式提供所需的.a文件目录。