How to install OpenGL headers in Linux and compile your source files

Particularly in Ubuntu, to be accurate. I’ve spent my whole morning trying to find a way, and I’ve found a lot of nonsense. So here it is. Open Synaptic Package Manager, search for glut, or freeglut, and install the “-dev” version. As simple as that….

synaptic package manager with freeglut-dev selected

Screenshot with the freeglut package selected

Yes, you are ready. Your header files (the one you installed right now, such us stdio.h, or iostream.h, etc.) can be found insinde /usr/include/GL.

You may encounter and another problem during the compile process. Assume you have the following source file as simple.c .


#include <GL/freeglut.h>



void display(void)

{
	/* clear window */

	 glClear(GL_COLOR_BUFFER_BIT);


	/* draw unit square polygon */

	glBegin(GL_POLYGON);
	 	glVertex2f(-0.5, -0.5);
	 	glVertex2f(-0.5, 0.5);
	 	glVertex2f(0.5, 0.5);
	 	glVertex2f(0.5, -0.5);
	glEnd();

	/* flush GL buffers */

	glFlush();

}


void init()
{

	/* set clear color to black */

	/* 	glClearColor (0.0, 0.0, 0.0, 0.0); */
	/* set fill  color to white */

	/* 	glColor3f(1.0, 1.0, 1.0); */

	/* set up standard orthogonal view with clipping */
	/* box as cube of side 2 centered at origin */
	/* This is default view and these statement could be removed */

	/* glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);  */
}

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

	/* Initialize mode and open a window in upper left corner of screen */
	/* Window title is name of program (arg[0]) */

	/* You must call glutInit before any other OpenGL/GLUT calls */
	glutInit(&argc,argv);
	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(500,500);
	glutInitWindowPosition(0,0);
	glutCreateWindow("simple");
	glutDisplayFunc(display);
	init();
	glutMainLoop();

}

if you try to ‘gcc simple.c’ errors such as ‘undefined reference’ will pop up. That is because the compiler doesn’t know where to find your library files (yeah, its a bit messy. I have no idea either why this is happening but i found the solution). To link it correctly you will have to manually tell gcc or g++ the name of you library  files and this can happen by adding -l and your library  file. for example


gcc simple.c -lGL -lGLU -lglut

I also have no idea why they have to be capitals and lower case, and why i cannot add freeglut in the parameters but for some strange reason, even if i include freeglut as a header in my source file, it is working…. and that’s what I want. You can also try my OpenGL Pong (greek, english posts) if you’d like.

Well, that’s it. Congratulations, and keep it up ;)

Advertisements

Posted on 30 October, 2010, in Linux, OpenGL, Programming and tagged , , , , , , , . Bookmark the permalink. 7 Comments.

  1. I was trying to compile the same example “simple.c” from http://www.csse.monash.edu.au/~jonmc/CSE3313/Resources/SampleCode/index.html and then I found your post :).
    Anyway on Mac it’s even easier, because it contains OpenGL and GLUT framework already in /System/Library/Frameworks/ so you can just call “gcc -o simple -framework OpenGL -framework GLUT simple.c” and it’s done.

  2. You are awesome!!! I’ve wasted an entire day trying to get my compiler to recognize the OpenGL libraries, until I came across your page. Thank you very much!

  3. Bismillahirrahmanirrahim. Thank you for this tutorial.

  4. You are welcome :)

  1. Pingback: Cómo instalar StepMania desde Terminal en Linux | Andrés Peña

  2. Pingback: Cómo instalar StepMania 5 desde Terminal en Linux | Andrés Peña

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: