5

i'm trying to get into opengl programming, but fail to compile my first very very simple program. the linking process fails every time. i found this answer on stackoverflow, and have had all packages installed and told g++ which libraries to link against.

here's my sample program:

#include <GL/glut.h>
#include <GL/gl.h>

int main(int argc, char **argv) {
  glutInit(&argc, argv);
  return 0;
}

compiling results in the following error from the linker:

$ g++ -Wall -lglut -lGL -lGLU opengl.cpp
/tmp/cc1UAFPU.o: In function `main':
opengl.cpp:(.text+0x3b): undefined reference to `glutInit'
collect2: ld returned 1 exit status

anybody got any idea on this issue? there must be something which i am missing, but i just cannot see what. any hints to resolve this issue are highly appreciated!

Community
  • 1
  • 1
knittl
  • 246,190
  • 53
  • 318
  • 364
  • 2
    might be order - either reorder libs, or put them after opengl.cpp – fazo Mar 05 '11 at 15:47
  • It works for me with Debian testing, g++-4.4, freeglut3-2.6 – Philipp T. Mar 05 '11 at 15:49
  • @knittl:Hello,have you tried to do "Project->Build options->Linker settings-->Add " and then go to /usr/lib and add the "libGLU.so and libglut.a" – George Mar 05 '11 at 15:53
  • @george: i'm not using any IDE … – knittl Mar 05 '11 at 15:54
  • @fazo: can you please add that as an answer, it did work for me!!! unbelievable. i thought usually filenames come last for commands? i don't understand why it will not link properly when i put the libs first – knittl Mar 05 '11 at 15:58
  • 4
    @knittl: Because when the linker processes a library, it only pulls in the pieces that satisfy undefined references. If the first thing it processes is a library, there aren't any references yet, and it won't use the library. Then it sees the next library, it still doesn't have any references, and won't use it either. Not only do libraries need to follow objects, but libraries need to be in the correct order as well. (Note: Some linkers are multi-pass and figure out the order for you) – Ben Voigt Mar 05 '11 at 16:03
  • @ben: ok, that explains a lot! thank you very much – knittl Mar 05 '11 at 16:09

1 Answers1

6

might be order - either reorder libs, or put them after opengl.cpp

fazo
  • 1,807
  • 12
  • 15
  • thanks a lot! although i don't see why it won't link when i put libs before the file – knittl Mar 05 '11 at 16:02
  • @knittl http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html look at explanation of '-l' - line starting with 'It makes a difference ' – fazo Mar 05 '11 at 16:08
  • yes i've read the manpage (after your comment to my question), but did not understand the reasoning behind this. plus, it seemed like the linker works in reverse order. ben voigt's comment to my question resolved my confusion – knittl Mar 05 '11 at 16:11