I'm taking a graphics class, and we have just started using OpenGL. My professor gave us a simple program that draws a purple line, that we were supposed to compile. I took it home and tried to compile it, and i got a lot of undefined symbol errors. Any help as to why this won't compile would be appreciated.

Also, I've never written anything in C++, only C and Java, but I'm reading up on C++ now, so if theres something stupid that I'm doing because of not knowing the language, I'm sorry.

The argument i used at my command line was

g++ -o PurpleLine PurpleLine.cpp

// This is a very minimal demonstration of OpenGL. It draws a purple
// line from (-0.5,-0.5,-1) to (0.5,0.5,-1), thus exercising OpenGL
// color, lines, and vertices, but almost nothing else.
// Written September 3, 2010 by CSci 335.

#include <GLUT/glut.h>

// The display callback. This function clears the frame buffer (which
// in this program really just involves clearing the color portion),
// sets the drawing color to purple, draws the line, and flushes all
// OpenGL output to the display.

void display( void ) {
    glClearColor( 1.0, 1.0, 1.0, 1.0 );
    glClear( GL_COLOR_BUFFER_BIT );
    glColor3d( 1.0, 0.0, 1.0 );
    glBegin( GL_LINES );
        glVertex3d( -0.5, -0.5, -1.0 );
        glVertex3d( 0.5, 0.5, -1.0 );

// The main method. This initializes GLUT and creates a window,
// registers just one callback (the display function above), and
// then hands control over to GLUT.

int main( int argc, char* argv[] ) {
    glutInit( &argc, argv );
    glutInitWindowPosition( 0, 0 );
    glutInitWindowSize( 600, 600 );
    glutInitDisplayMode( GLUT_RGBA | GLUT_SINGLE );
    glutCreateWindow( "Purple Line" );
    glutDisplayFunc( display );
    return 0;

You have to link the proper libraries for any OpenGL application. Your prof should have given instructions on how to compile. I'm not sure, it has been a while, but I think you need to link with:
for Linux: libglut.a (add -lglut to the command line) and libopengl.a (-lopengl)
for windows: glut32.lib and opengl32.lib (in command line: -lglut32 -lopengl32)

You should browse the web for instructions for your OS on linking to the GLUT libraries and OpenGL (probably only GLUT is needed). And, of course, you need to have GLUT installed on your system too!

hope this helped a bit.

This article has been dead for over six months. Start a new discussion instead.