26

I try to compile some "hello world" glut application:

#include <stdlib.h>

#include <GL/gl.h>

#include <GL/glu.h>

#include <GL/glut.h>

GLint Width = 512, Height = 512;

const int CubeSize = 200;

void Display(void)
{
    int left, right, top, bottom;

    left  = (Width - CubeSize) / 2;
    right = left + CubeSize;
    bottom = (Height - CubeSize) / 2;
    top = bottom + CubeSize;

    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3ub(255,0,0);
    glBegin(GL_QUADS);
      glVertex2f(left,bottom);
      glVertex2f(left,top);
      glVertex2f(right,top);
      glVertex2f(right,bottom);
    glEnd();

    glFinish();
}

void Reshape(GLint w, GLint h)
{
    Width = w;
    Height = h;
    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, w, 0, h, -1.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void Keyboard(unsigned char key, int x, int y)
{
#define ESCAPE '\033'

    if( key == ESCAPE )
        exit(0);
}

main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowSize(Width, Height);
    glutCreateWindow("Red square example");

    glutDisplayFunc(Display);
    glutReshapeFunc(Reshape);
    glutKeyboardFunc(Keyboard);

    glutMainLoop();
}

The compile command is:

gcc -lGL -lGLU hw_opengl.cpp -o hw_opengl

I've got the following errors:

/tmp/ccbnBFHC.o: In function `Display()':
hw_opengl.cpp:(.text+0x6c): undefined reference to `glClearColor'
hw_opengl.cpp:(.text+0x78): undefined reference to `glClear'
hw_opengl.cpp:(.text+0x94): undefined reference to `glColor3ub'
hw_opengl.cpp:(.text+0xa0): undefined reference to `glBegin'
hw_opengl.cpp:(.text+0xb4): undefined reference to `glVertex2f'
hw_opengl.cpp:(.text+0xc8): undefined reference to `glVertex2f'
hw_opengl.cpp:(.text+0xdc): undefined reference to `glVertex2f'
hw_opengl.cpp:(.text+0xf0): undefined reference to `glVertex2f'
hw_opengl.cpp:(.text+0xf5): undefined reference to `glEnd'
hw_opengl.cpp:(.text+0xfa): undefined reference to `glFinish'
/tmp/ccbnBFHC.o: In function `Reshape(int, int)':
hw_opengl.cpp:(.text+0x134): undefined reference to `glViewport'
hw_opengl.cpp:(.text+0x140): undefined reference to `glMatrixMode'
hw_opengl.cpp:(.text+0x145): undefined reference to `glLoadIdentity'
hw_opengl.cpp:(.text+0x173): undefined reference to `glOrtho'
hw_opengl.cpp:(.text+0x17f): undefined reference to `glMatrixMode'
hw_opengl.cpp:(.text+0x184): undefined reference to `glLoadIdentity'
/tmp/ccbnBFHC.o: In function `main':
hw_opengl.cpp:(.text+0x1c1): undefined reference to `glutInit'
hw_opengl.cpp:(.text+0x1cd): undefined reference to `glutInitDisplayMode'
hw_opengl.cpp:(.text+0x1e4): undefined reference to `glutInitWindowSize'
hw_opengl.cpp:(.text+0x1f0): undefined reference to `glutCreateWindow'
hw_opengl.cpp:(.text+0x1fc): undefined reference to `glutDisplayFunc'
hw_opengl.cpp:(.text+0x208): undefined reference to `glutReshapeFunc'
hw_opengl.cpp:(.text+0x214): undefined reference to `glutKeyboardFunc'
hw_opengl.cpp:(.text+0x219): undefined reference to `glutMainLoop'
collect2: ld returned 1 exit status

I've install GLUT: sudo apt-get install freeglut3 freeglut3-dev

There are in /usr/lib: libglut.a
libglut.so
libglut.so.3
libglut.so.3.9.0

locate glu.h
/home/goran/QtSDK/QtSources/4.8.0/src/3rdparty/webkit/Source/ThirdParty/glu/internal_glu.h
/usr/include/GL/glu.h

/usr/include/GL/gl.h

locate gl.h
...
/usr/include/GL/gl.h

What do I do incorrectly?

G-71
  • 3,626
  • 12
  • 45
  • 69

6 Answers6

64

The GCC linker may scan libraries in the order they are on the command line, which means for you it may scan the libraries first and sees no one using them, and therefore you get the errors. To be sure, place the libraries last on the command line:

gcc hw_opengl.cpp -o hw_opengl -lGL -lGLU -lglut
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 5
    Out of interest why would it be that my machine can successfully compile the target without reordering the options? Is this functionality version specific? –  Dec 02 '11 at 06:31
  • please see this question http://stackoverflow.com/questions/8353478/installation-of-all-opengl-libraries-for-development-in-ubuntu-11-10 – G-71 Dec 02 '11 at 08:40
7
g++ filename.cpp -lGL -lglut -o exfilename

and then

./exfilename
Sanoob
  • 2,466
  • 4
  • 30
  • 38
Sudip Das
  • 1,178
  • 1
  • 9
  • 24
4

It is because that xlibmesa-gl-dev and xlibmesa-glu-dev do not make soft link to file libGL.so and libGLU.so, so ld cannot find them to link with your code.

shadowglen
  • 66
  • 1
  • Because ld will look for libGL.so and libGLU.so for the options -lGL and -lGLU, but xlibmesaxxx do not make these two soft links to their own libs. – shadowglen Dec 02 '11 at 07:17
2

You're missing -lglut.

The correct compilation command is gcc -lGL -lglut -lGLU hw_opengl.cpp -o hw_opengl

  • 2
    I try this compile command: gcc -lGL -lglut -lGLU hw_opengl.cpp -o hw_opengl. There are the same errors. – G-71 Dec 02 '11 at 05:48
  • Hmm that's very odd. I ran the command as you've posted it, and it compiles fine. I ran your program, and I see a red square, as the code indicates. This would indicate that your compiler isn't aware of the library - try running `ldconfig` (as root), or possibly rebooting. What OS are you running? –  Dec 02 '11 at 05:54
  • I use kubuntu 11.10 $ gcc -lgl -lglut -lglu hw_opengl.cpp -o hw_opengl /usr/bin/ld: cannot find -lgl /usr/bin/ld: cannot find -lglu – G-71 Dec 02 '11 at 05:56
  • Have you also installed libgl-dev (it may also be called libgl)? –  Dec 02 '11 at 06:00
  • i've installed xlibmesa-gl-dev and xlibmesa-glu-dev – G-71 Dec 02 '11 at 06:03
  • I'd suggest installing libgl. I'm unfamiliar with xlibmesa. –  Dec 02 '11 at 06:05
  • sudo apt-get install libgl-dev Reading package lists... Done Building dependency tree Reading state information... Done Package libgl-dev is a virtual package provided by: libgl1-mesa-swx11-dev 7.11-0ubuntu3 libgl1-mesa-dev 7.11-0ubuntu3 You should explicitly select one to install. E: Package 'libgl-dev' has no installation candidate – G-71 Dec 02 '11 at 06:19
  • Go with libgl1-mesa-dev. –  Dec 02 '11 at 06:23
  • sudo apt-get install libgl1-mesa-dev Reading package lists... Done Building dependency tree Reading state information... Done libgl1-mesa-dev is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 252 not upgraded. – G-71 Dec 02 '11 at 06:30
  • Hmm. The Ubuntu package manager confuses me at times. Possibly try replacing it with the other package, `libgl1-mesa-swx11-dev`. –  Dec 02 '11 at 06:34
  • pls see this question http://stackoverflow.com/questions/8353478/installation-of-all-opengl-libraries-for-development-in-ubuntu-11-10 – G-71 Dec 02 '11 at 08:41
0

Check whether you installed binutils-gold (Gold linker) or not if not, install it and if you've already that then remove it and try at terminal,

g++ sourcefile.cpp -lglut
fancyPants
  • 50,732
  • 33
  • 89
  • 96
xIN3N
  • 9
  • 2
0
g++ -lglut -lGLU -lGL helloWorld.cpp -o helloWorld

g++, not gcc, for cpp

Julie
  • 9
  • 1