Lazy Foo' Productions

SDL Forums external link SDL Tutorials Articles OpenGL Tutorials OpenGL Forums external link
Follow BlueSky Follow Facebook Follow Twitter Follow Threads
Donate
News FAQs Contact Bugs

Setting up freeGLUT on g++

Last Updated: Sep 8th, 2012

After installing freeGLUT on OS X, it's time to set up g++ for development.
1)Go download the source for lesson 01. Extract the source somewhere. Now compile by entering this huge command:

g++ LUtil.cpp main.cpp -I/usr/local/include -I/opt/X11/include -L/usr/local/lib -I/opt/X11/lib -w -framework OpenGL -lGLUT -o 01_hello_freeglut

As the programs get bigger and bigger, having to manually punch in this compilation command gets very tedious very quickly. This is why I recommend using Make.

2) GNU Make allows you to make build scripts that'll automate the compilation process.
From Basic Makefile
#OBJS specifies which files to compile as part of the project
OBJS = LUtil.cpp main.cpp

#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = 01_hello_freeglut

#This is the target that compiles our executable
all : $(OBJS)
    g++ $(OBJS) -I/usr/local/include -I/opt/X11/include -L/usr/local/lib -I/opt/X11/lib -w -framework OpenGL -lGLUT -o $(OBJ_NAME)
Here we have a basic Makefile. At the top we declare and set the "OBJS" macro which specifies which files we're compiling. Then we set the "OBJ_NAME" macro that specifies the name of our executable.

After setting these two macros, we have the "all" target which compiles the program. It's followed by the dependencies which as you can see is the OBJS macro, because obviously you need the source files to compile the program.

After specifying the name of the target and its dependencies, the command to create the target is on the next line. The command to create the target must begin with a tab or Make will reject it.

As you would expect, the command to compile the program is largely the same as the command we would compile it off the command line. A key difference is that we have macros that we insert into the command which makes things like adding new files to the project must easier since you only have to change the macro as opposed to changing the whole command.

In future tutorials, we will be using more libraries and more source files. We should probably use more macros to make the process of adding them easier.
From Makefile
#OBJS specifies which files to compile as part of the project
OBJS = LUtil.cpp main.cpp

#CC specifies which compiler we're using
CC = g++

#INCLUDE_PATHS specifies the additional include paths we'll need
INCLUDE_PATHS = -I/usr/local/include -I/opt/X11/include

#LIBRARY_PATHS specifies the additional library paths we'll need
LIBRARY_PATHS = -L/usr/local/lib -I/opt/X11/lib

#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -w

#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -framework OpenGL -lGLUT

#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = 01_hello_freeglut

#This is the target that compiles our executable
all : $(OBJS)
    $(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
Now our compilation command is much more flexible.

Near the top we have the macros that define the files we're compiling and the compiler we're using.

Next we have the "INCLUDE_PATHS" macro which specifies the additional directories we're getting header files from. As you can see, we're using the include directories from XQuartx X11 directory and the freeGLUT directory we installed. The "LIBRARY_PATHS" sets the additional library file paths. Notice how there's a -I before every include directory and a -L before every library directory.

The "COMPILER_FLAGS" macro are the additional options we use when compiling. In this case we're disabling all warnings. The "LINKER_FLAGS" macro specifies which libraries we're linking against. Since on OS X OpenGL is a framework library, we link with it using the -framework flag. We compiled and installed freeGLUT as a Unix style library, so we link it using -l flag. If we were to use -framework glut, it would link against the 32bit GLUT framework instead of the freeGLUT library we compiled.

Finally at the bottom we have our target compiling using all of our macros. Thanks to macros we can very easily change the macros to add more files and libraries as we need them.

Save this Makefile code to a file named "Makefile" (case sensitive with no file extension) or you can use the premade Makefile here. Open a command line in the directory with the source files and run the command make all. Make will search for a file named "Makefile" in the directory Make was called in and run the Makefile that will compile your code.

Now that you have OpenGL and freeGLUT compiling, it time to go onto part 2 of the tutorial.