对过剩函数的未定义引用

Undefined reference to glut functions

本文关键字:未定义 引用 函数      更新时间:2023-10-16

这是我第一次编码关于openGL,我一直有很多麻烦,让所有的功能为它工作。这是我们老师给我们的一个程序,里面应该没有错误或问题。我正在使用MinGW和最新版本的freeglut。我已经在环境变量中创建了路径,并添加了所有的库及其路径。

#define GLUT_DISABLE_ATEXIT_HACK
# include <windows.h>
# include <stdlib.h>
# include <GL/freeglut.h>
GLfloat vertices[][3] = {{-1.0, -1.0, -1.0}, {1.0, -1.0, -1.0}, {1.0, 1.0, -1.0}, {-1.0, 1.0, -1.0}, {-1.0, -1.0, 1.0}, {1.0, -1.0, 1.0}, {1.0, 1.0, 1.0}, {-1.0, 1.0, 1.0}};
GLfloat colors[][3]= { {0.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {1.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 0.0, 1.0}, {1.0, 1.0, 1.0}, {0.0, 1.0, 1.0} };
bool stop = false;
void face(int a, int b, int c, int d)
{
    glBegin(GL_POLYGON);
    glColor3fv(colors[a]);
    glVertex3fv(vertices[a]);
    glColor3fv(colors[b]);
    glVertex3fv(vertices[b]);
    glColor3fv(colors[c]);
    glVertex3fv(vertices[c]);
    glColor3fv(colors[d]);
    glVertex3fv(vertices[d]);
    glEnd();
}
void colorcube()
{
    face(0, 3, 2, 1);
    face(2, 3, 7, 6);
    face(0, 4, 7, 3);
    face(1, 2, 6, 5);
    face(4, 5, 6, 7);
    face(0, 1, 5, 4);
}
static GLfloat theta[] = {0.0, 0.0, 0.0};
static GLint axis = 2;
void display()
{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glShadeModel(GL_SMOOTH);
    glLoadIdentity();
    glRotatef(theta[0], 1.0, 0.0, 0.0);
    glRotatef(theta[1], 0.0, 1.0, 0.0);
    glRotatef(theta[2], 1.0, 0.0, 1.0);
    colorcube();
    glFlush();
    glutSwapBuffers();
}

void spinCube()
{
    theta[axis] += 2.0;
    if (theta[axis] > 360,0) theta[axis] -= 360;
    glutPostRedisplay();
}
void mouse(int btn, int state, int x, int y)
{
    if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
    if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
    if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
}
void keyboard(unsigned char key, int x, int y)
{
    if (key == 'q' || key == 'Q') exit(0);
    if (key == ' ') stop = !stop;
    if (stop)
        glutIdleFunc(NULL);
    else
        glutIdleFunc(spinCube);
}
void myReshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-2.0, 2.0, -2.0,
            2.0, -10.0, 10.0);
    //if (w <= h)
    // glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
    // 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
    // else
    // glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
    // 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
    glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutCreateWindow("colorcube");
    glutReshapeFunc(myReshape);
    glutDisplayFunc(display);
    glutIdleFunc(spinCube);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glEnable(GL_DEPTH_TEST);
    glutMainLoop();
}

接下来是控制台的错误,它给了我,没有关于无法找到库,只是变量未定义。

16:19:35 **** Incremental Build of configuration Debug for project spinningCube ****
Info: Internal Builder is used for build
g++ "-IC:\MinGW\freeglut\include" "-IC:\MinGW\include" "-IC:\MinGW\lib\gcc\mingw32\4.8.1" "-IC:\MinGW\mingw32\include" -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "..\main.cpp" 
..main.cpp: In function 'void spinCube()':
..main.cpp:70:21: warning: left operand of comma operator has no effect [-Wunused-value]
     if (theta[axis] > 360,0) theta[axis] -= 360;
                     ^
g++ "-LC:\MinGW\freeglut\lib\x64\Debug" -o spinningCube.exe main.o -lglu32 -lopengl32 -lfreeglut 
main.o: In function `Z7displayv':
C:UsersJosh DonckelsworkspacespinningCubeDebug/../main.cpp:62: undefined reference to `_imp__glutSwapBuffers@0'
main.o: In function `Z8spinCubev':
C:UsersJosh DonckelsworkspacespinningCubeDebug/../main.cpp:71: undefined reference to `_imp__glutPostRedisplay@0'
main.o: In function `Z8keyboardhii':
C:UsersJosh DonckelsworkspacespinningCubeDebug/../main.cpp:86: undefined reference to `_imp__glutIdleFunc@4'
C:UsersJosh DonckelsworkspacespinningCubeDebug/../main.cpp:88: undefined reference to `_imp__glutIdleFunc@4'
main.o: In function `main':
C:UsersJosh DonckelsworkspacespinningCubeDebug/../main.cpp:111: undefined reference to `_imp__glutInit@8'
C:UsersJosh DonckelsworkspacespinningCubeDebug/../main.cpp:113: undefined reference to `_imp__glutInitDisplayMode@4'
C:UsersJosh DonckelsworkspacespinningCubeDebug/../main.cpp:114: undefined reference to `_imp__glutInitWindowSize@8'
C:UsersJosh DonckelsworkspacespinningCubeDebug/../main.cpp:115: undefined reference to `_imp__glutCreateWindow@4'
C:UsersJosh DonckelsworkspacespinningCubeDebug/../main.cpp:116: undefined reference to `_imp__glutReshapeFunc@4'
C:UsersJosh DonckelsworkspacespinningCubeDebug/../main.cpp:117: undefined reference to `_imp__glutDisplayFunc@4'
C:UsersJosh DonckelsworkspacespinningCubeDebug/../main.cpp:118: undefined reference to `_imp__glutIdleFunc@4'
C:UsersJosh DonckelsworkspacespinningCubeDebug/../main.cpp:119: undefined reference to `_imp__glutMouseFunc@4'
C:UsersJosh DonckelsworkspacespinningCubeDebug/../main.cpp:120: undefined reference to `_imp__glutKeyboardFunc@4'
C:UsersJosh DonckelsworkspacespinningCubeDebug/../main.cpp:122: undefined reference to `_imp__glutMainLoop@0'
collect2.exe: error: ld returned 1 exit status
16:19:36 Build Finished (took 1s.89ms)

首先:您应该真正修复编译器警告。','不是c++中的小数逗号。

对于链接错误:您必须更改链接库的顺序。Freeglut必须在opengl32之前出现在列表中。

g++ "-LC:\MinGW\freeglut\lib\x64\Debug" -o spinningCube.exe main.o -lfreeglut -lglu32 -lopengl32  

应该解决这个问题