Hide menu

GNU GCC C++ on IDA's systems for education

Compiling and linking with g++

Most C++ compilers accept files with suffix .cc, .C, .c, .cp, .cpp, .c++ and .cxx as source files written in C++. Files with suffix .o contains object code which can be given directly to the linker, ld. Note, even if you only have .o files to be linked, use g++ to give the link command, not ld, to acquire the correct linking environment.

When giving the command g++ without any options, for example

   % g++ hello.cc 

all translation steps from source code to an executable program and you receive (if no errors are detected) an executable file named a.out. With options you can control more in detail how the translation is to be done.

Error messages from the compiler and linker can be voluminous and hard to read and interpret, especially whe it concerns errors related to templates (and that's a lot more than you may expect). gccfilter is installed to make this easier, se below.

There is a large number of option which can be given in compile and link commands. The basic and most useful ones are the following.


No linking, stop after compilation. For each source file an .o-file is produced instead , containing object code. This option is ued for example when compiling parts of a program, especially parts not containing the function main.

-o namn

The executable program is written to file namn, not to the default file, typically a.out.


Compile the program to be runned under the control of a debugger (gdb, or som more sofisticated debugging environment (for example ddd). If the progra consists of several source files, all to be debugged must be compiled with -g.


Give warnings for all questionable constructs. Otherwise, warnings are only issued for constructs which most prpbable will give problems.


Even more controls than using -Wall, for example warnings for semicolons as in "if (e) ; ".


Disallows extensions in GCC being in conflict with the C++ ISO standard.


Experimental support for the next revision of the C++ standard (expected to be C++14) is made available, see C++14.


All warnings required if not conforming to strict ISO standard.


Warnings are regarded as errors, which means that no executable will be created if varnings.

It is recommended to always use -Wpedantic, -Wall, -Wextra, and -Werror. This will catch a lot of thing that otherwise is not reported but can lead to fatal errors. A program should not be concidered correct if any warnings!

The following command will compile hello.cc for debugging, C++11, and very pedantic, producing the executable on file hello, but only if no warnings are issued:

   % g++ -g -std=c++11 -Wpedantic -Wall -Wextra -Werror -o hello hello.cc

To avoid writing such long commands you can create one or more aliases, see Command aliases below.

The following options can be used to include and link code wich is not found in the default search paths for such (the must not be any spaces within each option):

-I pathname   

(capital i) Add pathname to the list of directories searched for include filers. More than one -I-directive can be given in the same command. The preprocessor looks firts in the working directory, then in the directories specified with -I, and las in the default directories which are always examined by the preprocessor.

-L directory

Adds directory to the list of directories searched for link libraries (used by the linker).

-l lib

(small "el") Use libray lib when linking. If lib is not found in the default search path, this option must be combined with -L, see above. Note: the -l options must be given after other options.

For more information about these options, and other possible to specify for g++, see the manual page for g++ (man g++).


gccfilter is strongly recommende. It simplifies and colourizes the often voluminuos and hard to read diagnostic messages from g++ (gcc). Simple to use, just prefix your ordinary g++ command with "gccfilter -c -a".

   gccfilter g++ -Wpedantic -Wall -Wextra hello.cc

On some platforms you instead use "gccfilter -c -a" to prefix your ordinary g++ command.

   gccfilter -c -a g++ -Wpedantic -Wall -Wextra hello.cc

Compiling templates with GCC

All code for templates must be included in the files using them. If one, for example, have a class template on an inclusion file (.h) and an accompanying implementation file (.tcc), one should let class.h, at the end, include the class.cc. Then, programs unsing the template only need to include class.h. In this case the implementation file (.tcc) shall also have an inclusion guard.

More information about GCC

Ses the online manual for g++ and gcc (man g++, or man gcc in a terminal window) and the info documentation which can be read in Emacs (command M-X info). Instructions how to read info in Emacs and which info is available is found in the info. If you gave a menu list in the Emacs window, you will find tools for navigating among the info documentation there.

See also GCC, the GNU Compiler Collection.

More information about compilers for different plattforms

See The home of Standard C++ on the web: Get a Compiler

Command aliases

As noted above one may have to write long kommand lines because of all options one may want to, or must, use. To facilitate work one can define one or more aliases in ones ~/.cshrc (~/.cshrc.private at IDA), or corresponding file for other command shells (.cshrc is for C Shell). The following lines defines the aliases g++ and g++filer to also include the options definied by the variables CCFLAGS (compile options) and LDFLAGS (link options):

   set CCFLAGS="-std=c++11 -Wpedantic -Wall -Wextra"
   # Note the use of single quotes to prevent (delay) expansion of ${GCC4_V}
   set LDFLAGS='-L/sw/gcc-${GCC4_V}/lib -static-libstdc++'
   alias g++ "g++ ${CCFLAGS} ${LDFLAGS}"
   alias g++filter "gccfilter -a -c g++ ${CCFLAGS} ${LDFLAGS}"

The simple command g++ hello.cc will now be the same as

  g++ -std=c++11 -Wpedantic -Wall -Wextra -L/sw/gcc-${GCC4_V}/lib -static-libstdc++ hello.cc

and corresponding for the command g++filter hello.cc.

Page responsible: The UPP group
Last updated: 2015-09-22