|
GCC Homepage:
GCC is an Open Source compiler available on all major platforms. The GCC homepage is at
http://gcc.gnu.org/
GCC Manual:
The gcc manual is available online at
http://gcc.gnu.org/onlinedocs/
Compiling and Linking with GCC:
Single Source File:
If you only have one source file, for example myproggy.cc , then you can compile and link it in one step with the command
g++ myproggy.cc -o myproggy
If there are no errors, this will produce an executable file named myproggy in the current directory. To run it, type
./myproggy ("./" tells Linux to find the program in the current directory.)
You can add many options to g++ ; see the documentation for a list. Some of the most common are -Wall , to display all possible warnings about potentially incorrect code in the source, -g , to insert debugging information in the compiled code (for use with the debugger gdb ), -On , where n is a number 0 to 3, to request optimisation (faster and/or smaller executable -do not use together with option -g).
For example, the file myproggy.cc could be compiled with warnings and debugging information by executing
g++ -Wall -g myproggy.cc -o myproggy
or with full optimisation, but no debugging information:
g++ -Wall -O3 myproggy.cc -o myproggy
Multiple Source Files:
If your program consists of several source files, you would need to compile each of them into an object file and then link the resulting object files together into an executable. This is tedious and is usually better simplified by writing a Makefile and automating the process with the "make" utility (see below).
"make" and "Makefiles" :
The "make" utility takes its commands from a "Makefile" to build programs of any complexity.
You can learn how to write makefiles from various tutorials on the internet. A simple "make" tutorial is published by the Little Unix Programming Group. The GNU "make" manual is also available online.
A Makefile Template:
A Makefile is a plain text file. When "make" is executed, it looks by default for a file called "Makefile" in the current directory and executes its commands.
You can start using "make" to develop programs even before you understand the ins and outs of Makefiles:
Download a basic Makefile to use as a template. It is designed to build C++ programs with GCC and the GNU "make" utility. Open it in an editor and follow the indications given in the template comments to tailor the template for your purpose. Save it in the directory where you are developing your program.
( Note that indentation in Makefiles is done with tabs. You must never replace these tabs with spaces !!! )
- Call "make all" or just "make" from a console prompt in your development directory in order to build the program. Make will compile all the files that have changed since the last compilation. If there are no compilation errors, it will also link all the object files and required libraries together in order to produce the executable.
- You can call "make clean" from your development directory to remove all object files and the executable from the directory if you want to start from fresh.
This Makefile relies on a shell script called "gccmakedep " that comes with Xwindows. If it does not exist on your system, download gccmakedep here, and add it to a directory on your path (under Linux, do this as root, and give it execute permissions for every user (e.g. chmod 751 gccmakedep ).
More help on compiling with GCC / G++ under Unix/Linux:
is available from the "Little Unix Programmers Group" (LUPG) on this page: 
Updated 23 July 2006
|