GCC Survival Guide

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

Having a compiler is nice, but when all that compiler offers is a bunch of command line tools new people can get a bit discouraged. Integrated development environments usually offer a way to do the compiler-calling for you, but still, it is useful to know what is going on there.

This article describes GCC, but a lot of the arguments that GCC accepts are common to all Unix-based compilers, and some are even supported by Microsoft compilers...

GCC in its totality is a huge beast, with hundreds of different command line switches. Fortunately, in everyday life you'll only need a small subset of those. The basic way to call GCC is:

gcc program.c -o program

This compiles 'program.c' into the executable file 'program' ('program.exe' on Windows). If no output (-o) file is specified the executable will be 'a.out'. The first thing that comes up next is libraries, you need to link certain libraries with your program. Say we need OpenGL and X11 libraries:

gcc program.c -o program -lGL -lX11

The names used after the '-l' arguments will cause GCC to look for files with names like libGL.a and libX11.a in its default library path. This path can be extended with the -L option, and similarly the path that is searched for files included by the code can be extended with -I (capital i).

gcc program.c -o program -lGL -lX11 -L/usr/local/my-gl/lib -I/usr/local/my-gl/include

Building multiple source files into a single executable can be done by just adding more files where it currently says 'program.c'. Compiling this way requires everything to be compiled every time though, which is rather slow. Source files can also be compiled into 'object' files, and later a bunch of object files can be linked together to produce an executable. This is what the '-c' option is for.

gcc -c program.c -o program.o
gcc -c utility.c -o utility.o
gcc utility.o program.o -lGL -lX11

Note that the libraries only need to be passed when linking objects together.

A few switches can come in handy when you are trying to compile lean mean fast code. Firstly, you'll want to turn on optimizations (note that this does slow down compiling significantly). This is done with the '-O' switch, after '-O' comes a number indicating how much effort the compiler has to spend on optimizing. '-O2' is usually good. When linking you can pass the '-s' option, which causes the compiler to remove everything that is unnecessary from the executable. This can seriously trim down executable size.

gcc -O2 -c program.c -o program.o
gcc -O2 -c utility.c -o utility.o
gcc utility.o program.o -s -lGL -lX11

To compile C++ programs on modern (version 3 and higher) GCC's you must use the 'g++' command instead of 'gcc'. G++ takes mostly the same arguments as GCC.

g++ program.cpp -o program

Calling the compiler manually every time you want to compile something is awfully cumbersome, which is why makefiles were invented. Those are basically a rather clever kind of scripts that automatically recompile things when needed. Writing them is a whole other subject though. (See http://www.gnu.org/software/make/manual/html_chapter/make_toc.html for more about make and makefiles.)