g++ TUTORIAL

g++ is known as a compiler, a program that will take your C++ source code and compile it into a binary file that can be executed to actually run your program. This page will give you the basics on how to take a source code file and compile it into an executable, as well as show you some of the options you have when you compile a source code file.



HOW TO COMPILE A SOURCE CODE FILE

To compile your source code, you must first reside in the directory that your source code files are in. For example, if you wanted to compile the file ~/projects/proj2/proj2.cc, you must first change directories to ~/projects/proj2 using the cd command. (If you need more information on basic Linux commands, go to the Linux Commands tutorial.)
Once you are in the correct directory, compile the source code in the file proj2.cc by typing the following command at the xterm prompt:

g++ proj2.cc

This is the most basic way to compile your source code. After you type this command, 1 of 2 things will happen. Either your source code will be without error and the exectuable will be written, returning you back to the command prompt:


OR your code will have a few errors in it and a display of the errors will be shown:


in which case no executable file will be written and you will be returned to your command prompt.

For now, let's assume that your source code had no errors in it. Using the command above, an executable file will be created in the current directory called a.out. To actually run the program, simply type at the command prompt ./a.out:



(Note that if you have "." added to your PATH environment you do not need the ./ in front of a.out.)

g++ FLAG OPTIONS

g++ allows for options to be flagged when you compile your source code. This discussion will show how to use 3 of them: -g, -Wall, and -o .

-g
To use the -g flag, simply type it in after g++ and before your source code file:

g++ -g source_file.cc

What the -g option will do is allow the executable file to be "debugged" using the GDB debugger. For a quick tutorial on the GDB debugger, click on the GDB link to the left.

-Wall
To use the -Wall flag, type it in just as you did with -g flag:

g++ -Wall source_file.cc

What the -Wall flag will do is show ALL the warning messages you may get that describe possible errors in your source code. If warnings are the only messages you receive when you compile your source code, and executable will still be created. Only if there are ERROR messages will an executable not be created. However, many professors in the Department will deduct points if your source code produces warnings when compiled.

-o
The -o flag is one of the more useful ones. This flag will allow you to rename the executable file from something other than a.out. To use the -o option, place it after the source code file, followed by the name you wish to give the executable. For example, to compile a source code file named proj2.cc into an executable file called proj2.exe, type the following command:

g++ proj2.cc -o proj2.exe

Doing so will create the executable proj2.exe that you would type at the command prompt to run your program.

It is possible to use more than one flag at once when you compile. For example, let's say you entered the following g++ command:

g++ -g -Wall proj2.cc -o proj2

This command will compile the source code file proj2.cc such that the executable will be able to be used with the GDB debugger (-g), will show all warning messages when compiled (-Wall), and will create the executable into a file called proj2 (-o proj2).

COMPILING MUTLIPLE-FILE PROGRAMS

g++ also allows you to compile a program that is contained in multiple files and create a single executable. To do so, simply list all the source code files one after the other, like so:

g++ source_file_1 source_file_2 source_file_3...

For example, let's say that you have a program contained in 3 files: proj2.cc contains the main() function and is the backbone of your program, functions.h contains the prototypes for functions that your program uses, and functions.cc contains the implementations for those functions. To compile these 3 files into one executable, type at the prompt:

g++ functions.cc proj2.cc

The above command will then compile the two source files and create one executable a.out.
NOTE - YOU DO NOT PUT ANY .h FILES IN THE g++ COMMAND! DOING SO WILL GIVE ERRORS!
Also, just like you would with single-file programs, you can add options flags when you compile:

g++ -g -Wall functions.cc proj2.cc -o proj2

This command will compile the two source files functions.cc and proj2.cc so that the executable can be used with the GDB debugger (-g), all compilation warnings will be displayed (-Wall), and the executable file will be called proj2 (-o proj2).

Of course, g++ has more functionality than what is described in this tutorial. To see a full listing of g++ commands, type at the command prompt:

man g++

to load up the "man" (manual) page for g++.