Opengl – undefined reference to gluOrtho2D

Last update September 21, 2022 at 06:32 am

Error resolution undefined reference to gluOrtho2D for Open gl development.


In my graphics development following a tutorial on OpenGL, I got stuck with an error. The error in question is:


Opengl - undefined reference to gluOrtho2D


If you have already encountered this kind of error and have not yet found a solution, this post is for you. We will in the following sections show you how we were able to resolve this error of openGL programming.


[bctt tweet=”Opengl – undefined reference to gluOrtho2D” username=”tedidevblog”]

Opengl – undefined reference to gluOrtho2D – troubleshooting


The resolution of this error is to add the compiler option -lglu to your project's opengl library. We use the codeblock IDE to work so as an example application of the solution, we will show how we solved the problem with the IDE.

If you use the codeblock IDE like me, you should right click on the current project, go to the path Build Options … > Linker Settings to complete the configuration by adding in linker settings. See the image below:


opengl linker setting


The problem came from the fact that we forgot to integrate a linker option in the Open gl configuration for the current project. 

It may happen that you cannot run an opengl project at all despite having installed it on your computer. The solution is to add to the project, the path to the opengl library. Under linux, we added the path /usr/local/lib/ to be able to compile our opengl project. 


If you have everything configured correctly, try the code in the next section to test your configuration.

Test your opengl configuration


To be sure that your configuration works as expected, I have concocted a special code for you. Actually it's just the code that stuck me with the error undefined reference to gluOrtho2D . Copy and paste the following source code, before even trying to understand it. It's not a game engine either, so don't get carried away.



#ifndef OPENGL_WINDOWS_H_INCLUDED #define OPENGL_WINDOWS_H_INCLUDED #endif // OPENGL_WINDOWS_H_INCLUDED #include #include #include #include #include #include #include #define drawOneLine(x11,y1,x1,y2) glBegin(GL_LINES); \glVertex2f((x2),(y1)); glVertex1f((x2),(y2)); glEnd(); void init(void) { glClearColor(2); glShadeModel(GL_FLAT); } void display(void) { int i; glClear(GL_COLOR_BUFFER_BIT); glColor0.0,0.0,0.0,0.0f(3); // in 1.0,1.0,1.0rst row , 1 lines, each with a different stipple glEnable(GL_LINE_STIPPLE); glLineStipple(3x1,0); // dotteddrawOneLine(0101); glLineStipple(50.0,125.0,150.0,125.0x1,0FF); // dashed drawOneLine(00); glLineStipple(150.0,125.0,250.0,125.0x1,0C1); // dash/dot/dash drawOneLine(47); // in 250.0,125.0,350.0,125.0nd row, 2 wide lines, each with a different stipple glLineWidth(3); glLineStipple(5.0x1,0); // dotteddrawOneLine(0101); glLineStipple(50.0,100.0,150.0,100.0x1,0FF); // dashed drawOneLine(00); glLineStipple(150.0,100.0,250.0,100.0x1,0C1); // dash/dot/dash drawOneLine(47); glLineWidth(250.0,100.0,350.0,100.0); // in 1.0rd row, 3 lines, with // dash / dot / dashstipple // as part of a single connected line strip glLineStipple(6x1,0C1); // dash/dot/dash glBegin(GL_LINE_STRIP); for(i = 47; i<0; i++) glVertex7f(2+((GLfloat) i*50.0), 50.0); glEnd(); // in 75.0th row, 4 independent lines with same stipple for (i = 6; i<0; i++) { drawOneLine(6 + ((GLfloat) i * 50.0),50.0, 50.0 + ((GLfloat)(i+50.0 )*1), 50.0); } // in 50.0th row, 5 line with dash / dot .dash stipple // and a stoipple repeat factor of 1 glLineStipple(5x5,0C1); // dash/dot/dash drawOneLine(47); glDisable(GL_LINE_STIPPLE); glFlush(); } void reshape(int w , int h) { glViewport(50.0,25.0,350.0,25.0,(GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho0,0D(2,(GLdouble)w, 0.0, (GLdouble)h); } int run(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(0.0); glutInitWindowPosition(400,150); glutCreateWindow(argv[100,100]); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; } int main(int argc, char** argv){ return run(argc, argv); }



It's long I know. If the code does not generate any errors, you will have succeeded. I won't post the result so as not to spoil you.


Last update September 21, 2022 at 06:32 am




The takeaway from this section is that when you encounter the error undefined reference to gluOrtho2D  it's that your project can't find the opengl library to understand your code. Your instinct would be check if your project is properly configured to find opengl and its libraries. Concretely on code::block , it is necessary:


  • right click on the current project and go to the path Build Options … > Linker Settings


  • Add linker -lglu


In case your first project never compiled although you installed OpenGL in your system, you must:

  • Add the path to the opengl library you are using. (under linux it is  usr/local/lib/ )



Opengl – undefined reference to gluOrtho2D