View Full Version : GL: Why is -lGL -lglut required for compiling?
tehet
April 13th, 2008, 09:04 AM
Hi,
How come you need to set these flags in order to compile stuff with GL and GLUT? Why is #include <GL/gl.h> and #include <GL/glut.h> not enough?
I think I have read an explanation for math.h and the -lm flag which perhaps has a similar cause but I can't find the explanation again.
Thanks
nvteighen
April 13th, 2008, 09:19 AM
Because header files only include the protoypes of functions, not the functions themselves, which are defined in the proper shared library. In summary, those flags tell the linker (which is the 4th phase in the compiling process) which libraries are needed for that executable.
LaRoza
April 13th, 2008, 09:19 AM
Hi,
How come you need to set these flags in order to compile stuff with GL and GLUT? Why is #include <GL/gl.h> and #include <GL/glut.h> not enough?
I think I have read an explanation for math.h and the -lm flag which perhaps has a similar cause but I can't find the explanation again.
Thanks
Look at the contents of gl.h and glut.h and you will see ;)
http://en.wikipedia.org/wiki/Dynamic_linking#Dynamic_linking
tehet
April 13th, 2008, 09:40 AM
Because header files only include the protoypes of functions, not the functions themselves, which are defined in the proper shared library. In summary, those flags tell the linker (which is the 4th phase in the compiling process) which libraries are needed for that executable.
As opposed to, lets say, stdlib.h or does gcc silently link in glibc and such by default? Looking At gl.h, glut.h and stdlib.h I think I'm only seeing prototypes but then again, I'm kind of a noob.
LaRoza
April 13th, 2008, 09:47 AM
As opposed to, lets say, stdlib.h or does gcc silently link in glibc and such by default? Looking At gl.h, glut.h and stdlib.h I think I'm only seeing prototypes but then again, I'm kind of a noob.
The standard library is statically linked (I think), so stdio.h declares the functions so the compiler will know how to handle the declarations. The definitions of the functions are already known.
All other libraries must be specified. The header needs to be found so the use of the functions can be determined by the compiler, and the library needs to be found for the linker.
tehet
April 13th, 2008, 09:51 AM
Ah, I think I understand :)
Thanks nvteighen and LaRoza!
stroyan
April 13th, 2008, 10:01 AM
gcc does link with libc by default.
You can see the link line by adding -v to the gcc options.
There is a -nodefaultlibs gcc option that will suppress the default use of libraries.
tehet
April 13th, 2008, 11:00 AM
gcc does link with libc by default.
You can see the link line by adding -v to the gcc options.
There is a -nodefaultlibs gcc option that will suppress the default use of libraries.
Compiling a small test program with -nodefaultlibs return
undefined reference to `printf'
so that confirms it. Thanks.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.