How to make an executable opengl file?

Hello, I am totally new to opengl on linux.
I wrote a sample code ‘test.c’ generating a rectagle, but the command ‘g++ test.c’ produced several errors like:

/tmp/ccadyyws.o(.text+Oxf): undefined reference to ‘glClearColor’

I, of course, included glut.h and checked the existence of glut.h in the folder /usr/include/GL.

What shall I do to make a executabe file?

The code is from the redbook and as follow:

#include <GL/glut.h>
#include <stdlib.h>

void init( void )
{
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glShadeModel( GL_FLAT ) ;
}

void display( void )
{
glClear( GL_COLOR_BUFFER_BIT );
glColor3f( 1.0, 1.0, 1.0 );
glLoadIdentity();

gluLookAt( 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
glScalef( 1.0, 1.0, 1.0 );
glutWireCube( 1.0 );
glFlush();

}

void reshape( int w, int h )
{
glViewport( 0, 0, (GLsizei) w, (GLsizei) h ) ;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glFrustum( -1.0, 1.0, -1.0, 1.0, 1.5, 20.0 ) ;
glMatrixMode( GL_MODELVIEW );
}

int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize( 500, 500 );
glutInitWindowPosition( 100, 100 );
glutCreateWindow( argv[ 0 ] );
init();
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutMainLoop();
return 0;
}
.

Try

gcc test.c -L/usr/X11R6/lib -lGL -lGLU -lglut -lX11

Just explaining what he did… You need to indicate which libraries you want the compiler to link with your application.

You probably want to use a makefile when you get more than one file going. Here is the one I use.

EXECUTABLE = demo
CPPFLAGS = -ggdb3 -Wall
LIBS = -lpthread -lGL -lGLU -lglfw -lXxf86vm
LIBPATH = -L/usr/X11R6/lib -L./lib
#################################################
CC = gcc
SRCS := (wildcard *.cpp) OBJS := (patsubst %.cpp,%.o,(SRCS)) DEPS := (patsubst %.cpp,%.d,(SRCS)) ################################################# all: (EXECUTABLE)

(EXECUTABLE): (DEPS) (OBJS) (CC) (CPPFLAGS) -o (EXECUTABLE) (OBJS) (LIBPATH) $(LIBS)

%.d: %.cpp
(CC) -M (CPPFLAGS) < | sed s/\\.o/.d/ > @

%.d: %.h
(CC) -M (CPPFLAGS) < | sed s/\\.o/.d/ > @

clean:
-rm (OBJS) (EXECUTABLE) $(DEPS) *~ &> /dev/null

explain:
@echo “--------Some Info--------”
@echo “Executable name: (EXECUTABLE)" @echo "Source files: (SRCS)”
@echo “Object files: (OBJS)" @echo "Dependency files: (DEPS)”

depends: $(DEPS)
@echo “Dependencies updated”

-include $(DEPS)

tags:
etags .h .cpp /usr/include/.h /usr/include/GL/.h /usr/include/SDL/*.h

run: all
./$(EXECUTABLE)

debug: all
gdb $(EXECUTABLE)

The first 4 lines are all that will need to be modified. I use GLFW, so you probably want to replace -lglfw with -lGLUT. You can do all of the following.

make make all
make clean make depends
make explain make tags
make run make debug

The make tags will probably only help you out if you use etags with emacs.

I succeeded it, thank you all guys.
But I still have a question.

Do the parameters ‘-lGL -lGLU -lglut’ mean the libraries? If so, where are they?

[This message has been edited by ogong (edited 02-24-2003).]

They’re probably in /usr/X11R6/lib, but it depends on your system.

‘-lGL’ means ‘look for and link a library called libGL.so or libGL.a in the directories specified by the library search paths’

‘-L/usr/X11R6/lib’ means ‘add /usr/X11R6/lib to the library search paths’

The library search paths contain (at least) /usr/lib and /usr/local/lib by default.

When you’re using dynamic linking you can figure out which libraries an executable links to at runtime and from where. This is particularly useful if you’re moving files across systems that may have different configurations or have something that won’t run on one of your systems and you’re trying to figure out why.

On Linux use ldd to report on libraries a executable links to and where it finds them. Here’s the man page online:
http://nodevice.com/sections/ManIndex/man0694.html

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.