brian
brian

Reputation: 157

Problems linking with glut: "undefined reference to..."

I'm attempting to get started using OpenGL, but cannot seem to link to some basic libraries correctly. Particularly I'll try to use some glut functions (like glutInit). Here's my code:

#include <GL/glut.h>

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (250, 250); 
    glutCreateWindow ("hello");

    glutMainLoop();
}

Here's what my make spits out:

g++  -Wl,--trace  -L/usr/lib/x86_64-linux-gnu/ -lglut -lGL -lGLU -lXi -lXmu -o texture texture.cpp
/usr/bin/ld: mode elf_x86_64
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crt1.o
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crti.o
/usr/lib/gcc/x86_64-linux-gnu/4.9/crtbegin.o
-lglut (/usr/lib/x86_64-linux-gnu//libglut.so)
-lGL (/usr/lib/x86_64-linux-gnu//libGL.so)
-lGLU (/usr/lib/x86_64-linux-gnu//libGLU.so)
-lXi (/usr/lib/x86_64-linux-gnu//libXi.so)
-lXmu (/usr/lib/x86_64-linux-gnu//libXmu.so)
/tmp/ccDzovHh.o
-lstdc++ (/usr/lib/gcc/x86_64-linux-gnu/4.9/libstdc++.so)
-lm (/usr/lib/x86_64-linux-gnu//libm.so)
-lgcc_s (/usr/lib/gcc/x86_64-linux-gnu/4.9/libgcc_s.so)
/lib/x86_64-linux-gnu/libc.so.6
(/usr/lib/x86_64-linux-gnu/libc_nonshared.a)elf-init.oS
/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
-lgcc_s (/usr/lib/gcc/x86_64-linux-gnu/4.9/libgcc_s.so)
/usr/lib/gcc/x86_64-linux-gnu/4.9/crtend.o
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crtn.o
/tmp/ccDzovHh.o: In function `main':
texture.cpp:(.text+0x1e): undefined reference to `glutInit'
texture.cpp:(.text+0x28): undefined reference to `glutInitDisplayMode'
texture.cpp:(.text+0x37): undefined reference to `glutInitWindowSize'
texture.cpp:(.text+0x41): undefined reference to `glutCreateWindow'
texture.cpp:(.text+0x46): undefined reference to `glutMainLoop'
/usr/bin/ld: link errors found, deleting executable `texture'
collect2: error: ld returned 1 exit status
make: *** [texture] Error 1

I've examined libglut.so with nm -D --defined-only /usr/lib/x86_64-linux-gnu/libglut.so and it appears to have the offending functions.

Any thoughts as to what I'm doing wrong?

Also, I reinstalled glut (apt-get install --reinstall freeglut3-dev) and nothing changed.

Upvotes: 1

Views: 3099

Answers (1)

alesegdia
alesegdia

Reputation: 548

Libraries need to be after sources. Try with:

g++ glutsample.cpp -o glutsample -lglut -lGL -lGLU -lXi -lXmu

Upvotes: 6

Related Questions