Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

After updating to 11.10 I can't link code against libgl/glut. freeglut3-dev etc. are installed and it looks like they can be found too. I'm really not sure what's going on.

Makefile

gltest: gltest.cc
    gcc -Wall -lglut -lGLU -lGL gltest.cc -o gltest

gltest.cc

#include <GL/glut.h>

int main(int argc, char **argv) {

  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DEPTH |     GLUT_DOUBLE | GLUT_RGBA);
  glutInitWindowPosition(100,100);
  //glutInitWindowSizendowSize(320,320);
  glutCreateWindow("test");

  return 1;
}

Output

jonas@fortress:~/dev/checkouts/test$ make
gcc -Wall -lglut -lGLU -lGL gltest.cc -o gltest
/tmp/cct1HevY.o: In function `main':
gltest.cc:(.text+0x1e): undefined reference to `glutInit'
gltest.cc:(.text+0x28): undefined reference to `glutInitDisplayMode'
gltest.cc:(.text+0x37): undefined reference to `glutInitWindowPosition'
gltest.cc:(.text+0x41): undefined reference to `glutCreateWindow'
collect2: ld returned 1 exit status
make: *** [gltest] Error 1

When linking with --verbose

attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../../lib/libglut.so succeeded

And then when checking the symbols in that file

$ readelf -Ws /usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../../lib/libglut.so|grep glutInit
   278: 000000000001dfc0  1581 FUNC    GLOBAL DEFAULT   11 glutInit

I'm very confused now.

share|improve this question
1  
It works when I put the -lGLU -lGL -lglut to the end of the gcc call. I'm still quite sure that this worked in the past. –  Jonas Wagner Oct 16 '11 at 19:27
    
Jonas: I've encountered the exact same problem, even after having triple-checked that I have all the necessary developer packages. This fix worked for me, thanks! –  Yuri Oct 23 '11 at 13:08

2 Answers 2

Your Makefile does not work anymore because of a toolchain transition (the transition was planned for natty but the change which causes your problem was made in oneiric). The linker flag --as-needed is activated by default in Ubuntu 11.10. This causes the problem you observed. The solution is exactly what you described in your answer.

The --as-needed option makes the linker sensitive to the ordering of libraries on the command-line. You may need to move some libraries later in the command-line, so they come after other libraries or files that require symbols from them.

share|improve this answer

It works when I put the -lGLU -lGL -lglut to the end of the gcc call. I'm still quite sure that this worked in the past.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.