- Compiling an OpenGL/GLUT program requires you to use a number of libraries.
- GLUT (library "glut" on Linux)
- OpenGL (library "GL" on Linux)
- GL Utilities (library "GLU" on Linux)
- Windowing specific libraries needed by GLUT. For X-Windows, these include:
- X11
- Xmu
- Xi
- X11
- Often you'll also need the math library (library "m" on Linux)
- You need to include (more) libraries if you encounter error messages such as:
- /tmp/ccKfrBZc.o(.text+0x1a6): In function `main':
: undefined reference to `glutInitDisplayMode'
- /tmp/ccKfrBZc.o(.text+0x1a6): In function `main':
- GLUT (library "glut" on Linux)
- Including libraries is easy with gcc or g++, just at "-l< lib-name >" to the command line.
- So, a compilation including all these libraries would look like:
- gcc -o myProg myProg.c -lglut -lGL -lGLU -lX11 -lXmu -lXi -lm
- The "-o myProg" tells gcc to call the executable "myProg"
- After compilation, you can run by typing "./myProg" or if "." is in your path just "myProg" will work.
- gcc -o myProg myProg.c -lglut -lGL -lGLU -lX11 -lXmu -lXi -lm
- On many machines, gcc doesn't know where to find the correct include
files (those you added to your code using #include"...")
- Adding "-I < include-path >" tells gcc where to find additional include files.
- You'll need to add -I commands to eliminate errors such as:
- myProg.c:1:17: Xm.h: No such file or directory
- myProg.c:1:17: Xm.h: No such file or directory
- Adding "-I < include-path >" tells gcc where to find additional include files.
- Often (including on the lab machines), the linker doesn't know where to find
some of the libraries if they're stored in unusual locations (like outside of /usr/lib)
- Adding "-L < library-path >" tells the linker where to find additional library files.
- You'll need to add -L commands to eliminate errors such as:
- /usr/bin/ld: cannot find -lX11
- /usr/bin/ld: cannot find -lX11
- Adding "-L < library-path >" tells the linker where to find additional library files.
Suggested Command Line to Compile on Lab Machines:
gcc -o myProg myProg.c -I /usr/X11R6/include/ -L /usr/X11R6/lib64/ -lglut -lGL -lGLU -lX11 -lXmu -lXi -lm
You should just be able to copy/paste. The only changes you need to make are:
- Change the filename "myProg.c" to the filename of your program
- (If you have more than one C file, include more than one on the command line)
- (If you have more than one C file, include more than one on the command line)
- Change "gcc" to "g++" if you're using C++.
NOTE: If you are compiling at home, you may need to change /usr/X11R6/lib64 to
/usr/X11R6/lib. It turns out the lab machines in MLH 301 have both 32- and 64-bit versions
of OpenGL and GLU, but only a 64-bit version of GLUT.
Chris Wyman (cwyman@cs.uiowa.edu)