Hide menu

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

Setting up GCC in your environment

To be able to use the GCC compilers and other GCC software, module prog/gcc must be loaded. This is default at IDA, so normally you don't have to take any action to use g++ (GCC).

The prog/gcc module sets the environment variable GCC4_V, which denotes the default version (e.g. 4.8.1) of GCC on the system you are using. GCC4_V can, e.g., be used in make files, and otherwise, when you want to refer to the version number of the GCC you are using.

There may be later versions avilable on the educational systems, and to use such you have to take som action. For having a certain version of GCC loaded automatically at login, give the following command in a terminal window.

   module initadd prog/gcc/4.9.1

You must then log out and in again for this to take effekt.

For having a certain version of GCC loaded just for a certain terminal window, give the following command in that terminal window.

   module add prog/gcc/4.9.1

To find out which modules you have loaded, give the command module list. To find out which modules are available in the module system, use the command module avail.

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.

See also Local link problems with GCC and their solution 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.

-c

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.

-g

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.

-Wall

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

-Wextra

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

-std=c++11    

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

-std=c++1y    

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

-Wpedantic

All warnings required if not conforming to strict ISO standard.

-Werror

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++).

Solaris link problems with GCC and their solution

At present there are problems with the current verion of Solaris used at IDA and the link library for C++, resulting in errors. This is solved by using -L/sw/gcc-4.7.1/lib and -static-libstdc++. The options -Wpedantic, -Wall and -Wextra, which were mentioned above, one can prefer to experiment with, so the following alias could be a good idea to add to your file .cshrc.private in your home directory (-std=c++11 was also mentione above):

   alias g++ "g++ -std=c++11 -L/sw/gcc-${GCC4_V}/lib -static-libstdc++"

This saves some typing and beside this one can make good use of the command histori in the terminal window when repeating the same command.

gccfilter

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

   gccfilter -c -a g++ -Wpedantic -Wall -Wextra -Werror 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 -Werror"
   # 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 -Werror -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: 2014-09-05