Qt error: undefined reference to 'gluOrtho2D'
Asked Answered
F

2

13

I have the same error than here. There's no solution there and also I'm gonna try to explain it more clearly so hopefully someone has the solution. Also I've installed everything from the answer to this question.

I'm trying to compile a program that a friend has done in QT (linux). I've tryed it on Windows and it runs just fine, the problem is that I can't run it on Ubuntu 11.10.

This is the compiling output at QT:

...
glwidget.o:glwidget.cpp:function GLWidget::resizeGL(int, int): error: undefined reference to 'gluOrtho2D'
glwidget.o:glwidget.cpp:function GLWidget::projectScene(): error: undefined reference to 'gluOrtho2D'
make: Leaving directory...
collect2: ld returned 1 exit status
make: *** [QTapplication] Error 1
...
  • glu.h and gl.h are #included.
  • QT += opengl is put at the .pro

I don't know if it's problem of Ubuntu 11.10. I've read that it has some problems with the linker though I downloaded "binutils-gold" that supposedly fixed it. Also I've tryed to add manually the library with right click over the projects' .pro->Add Librarys, but it won't let me add nothing at "/usr/include/GL".

Finally, here's the complete compile output:


make: Entering directory `/home/daniel/Escritorio/Practica3-build-desktop-Desktop_Qt_4_8_0_for_GCC__Qt_SDK__Release'
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../QtSDK/Desktop/Qt/4.8.0/gcc/mkspecs/linux-g++ -I../Practica3 -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtCore -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtGui -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtOpenGL -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include -I../Practica3/SceneObjects -I../Practica3/SceneObjects/Auxiliar -I/usr/X11R6/include -I. -I. -I../Practica3 -I. -o main.o ../Practica3/main.cpp
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../QtSDK/Desktop/Qt/4.8.0/gcc/mkspecs/linux-g++ -I../Practica3 -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtCore -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtGui -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtOpenGL -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include -I../Practica3/SceneObjects -I../Practica3/SceneObjects/Auxiliar -I/usr/X11R6/include -I. -I. -I../Practica3 -I. -o mainwindow.o ../Practica3/mainwindow.cpp
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../QtSDK/Desktop/Qt/4.8.0/gcc/mkspecs/linux-g++ -I../Practica3 -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtCore -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtGui -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtOpenGL -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include -I../Practica3/SceneObjects -I../Practica3/SceneObjects/Auxiliar -I/usr/X11R6/include -I. -I. -I../Practica3 -I. -o glwidget.o ../Practica3/glwidget.cpp
../Practica3/glwidget.cpp:120:6: warning: unused parameter 'event' [-Wunused-parameter]
/home/daniel/QtSDK/Desktop/Qt/4.8.0/gcc/bin/moc -DQT_NO_DEBUG -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../QtSDK/Desktop/Qt/4.8.0/gcc/mkspecs/linux-g++ -I../Practica3 -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtCore -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtGui -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtOpenGL -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include -I../Practica3/SceneObjects -I../Practica3/SceneObjects/Auxiliar -I/usr/X11R6/include -I. -I. -I../Practica3 -I. ../Practica3/mainwindow.h -o moc_mainwindow.cpp
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../QtSDK/Desktop/Qt/4.8.0/gcc/mkspecs/linux-g++ -I../Practica3 -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtCore -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtGui -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtOpenGL -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include -I../Practica3/SceneObjects -I../Practica3/SceneObjects/Auxiliar -I/usr/X11R6/include -I. -I. -I../Practica3 -I. -o moc_mainwindow.o moc_mainwindow.cpp
/home/daniel/QtSDK/Desktop/Qt/4.8.0/gcc/bin/moc -DQT_NO_DEBUG -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../QtSDK/Desktop/Qt/4.8.0/gcc/mkspecs/linux-g++ -I../Practica3 -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtCore -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtGui -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtOpenGL -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include -I../Practica3/SceneObjects -I../Practica3/SceneObjects/Auxiliar -I/usr/X11R6/include -I. -I. -I../Practica3 -I. ../Practica3/glwidget.h -o moc_glwidget.cpp
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../QtSDK/Desktop/Qt/4.8.0/gcc/mkspecs/linux-g++ -I../Practica3 -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtCore -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtGui -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtOpenGL -I../../QtSDK/Desktop/Qt/4.8.0/gcc/include -I../Practica3/SceneObjects -I../Practica3/SceneObjects/Auxiliar -I/usr/X11R6/include -I. -I. -I../Practica3 -I. -o moc_glwidget.o moc_glwidget.cpp
g++ -Wl,-O1 -Wl,-rpath,/home/daniel/QtSDK/Desktop/Qt/4.8.0/gcc/lib -o Practica3 main.o mainwindow.o glwidget.o scene.o pv2d.o regularpolygon.o obstacle.o sceneobject.o rectangle.o paintableobject.o segment.o point.o pen.o bouncingball.o circle.o moc_mainwindow.o moc_glwidget.o    -L/home/daniel/QtSDK/Desktop/Qt/4.8.0/gcc/lib -L/usr/X11R6/lib -lQtOpenGL -lQtGui -lQtCore -lGL -lpthread 
glwidget.o:glwidget.cpp:function GLWidget::resizeGL(int, int): error: undefined reference to 'gluOrtho2D'
make: Leaving directory `/home/daniel/Escritorio/Practica3-build-desktop-Desktop_Qt_4_8_0_for_GCC__Qt_SDK__Release'
glwidget.o:glwidget.cpp:function GLWidget::projectScene(): error: undefined reference to 'gluOrtho2D'
collect2: ld returned 1 exit status
make: *** [Practica3] Error 1
16:44:35: The process "/usr/bin/make" exited with code 2.
Error while building project Practica3 (target: Desktop)
When executing build step 'Make'
Foreignborn answered 26/2, 2012 at 15:39 Comment(3)
Whether or not the headers are included is not relevant here - you have a linker error not a compiler error. To fix it, you need to make sure that you're linking in the glu library correctly.Juieta
You seem to be linking in gl with -lGL but not glu. Try adding -lGLU and see if that helps.Juieta
possible duplicate of undefined reference to `gluOrtho2D'Drudge
A
13

You need to add the relevant libraries to the linker options.

Libraries are added with -l options (lower-case L). They are not paths, but actual files that contain code. On Linux, those files have .so* extensions. On Windows, those are .dll files. Extensions vary on other platforms.

One of the questions you link states that you need the following libraries:

-lglut -lGL -lGLU -lGLEW

(gluOrtho2D is actually in libGLU, but you might be missing others.)

Read your IDE's docs to see how you're supposed to add those to the linker options, but many allow you to enter them just like that in the right area of the project settings.

If those libraries are not in the usual library paths on your machine (/lib, /usr/lib on Linux, possibly /lib64 and /usr/lib64, or same with 32 instead), you'll have to tell the linker to look in other directories.
For that, either your IDE has another option for linker search paths, or you can specify them in the same spot as the actual libraries. You use the -L switch for that.

-L for search paths, -l for libraries.

Recap: if you want to link with libsuperfoo.so that is in /usr/local/bar/libs, then the correct linker options are:

-L/usr/local/bar/libs -lsuperfoo

If you're using qmake, the only thing you need to add to the .pro file is (for the above hypothetical library):

LIBS += -L/usr/local/bar/libs -lsuperfoo
Abrasive answered 26/2, 2012 at 15:54 Comment(1)
Thanks for the answer! I achieved it putting: LIBS += -lglut -lGL -lGLU -lGLEW at the .proForeignborn
M
2

In windows, adding

LIBS += -lopengl32 -lglu32

in the .pro file and

#include<GL/glu.h>

in the source file solves the issue.

Manometer answered 5/2, 2022 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.