C++ compilers on IDA's Solaris system
There are several C++ compilers available at IDA: GNU GCC and Sun CC. Since the GCC compiler g++ is the most used we keep with that one here.
Setting up GCC in your environment
To be able to use g++ and the debugger gdb module prog/gcc must be loaded. This is default at IDA but the default version must for the time being be changed to be able to get use to the latest features of C++11. It's simplest done if you put the lines below in the file .login in your home directory (and then log out and then in again). By this, the latest version of GCC 4 available at IDA will be loaded automatically when you log in.
module rm prog/gcc module add prog/gcc/4
This also sets the environment variable GCC4_V. It can be used in make files and otherwise when the current version of GCC needs to be referred to.
Note: to find out which modules you have loaded, give the command module list.
Compiling and linking with GCC
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.
-c |
No linking, stop after compilation. For each source file an
|
| -o namn |
The executable program is written to file namn, not to the
default file, typically
|
| -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 |
| -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 |
| -std=c++11 |
Disallows extensions in GCC being in conflict with the C++ ISO standard. |
| -pedantic |
All warnings required if not conforming to strict ISO standard. |
It is recommended to always use -pedantic, -Wall
and -Wextra. This will catch a lot of thing that otherwis is not
reported but can lead to fatal errors. The following command will compile
hello.cc for debugging, C++11, and very pedantic, producing the executable
on file hello (and you could prefix this with "gccfiler -c -a"):
% g++ -g -std=c++11 -pedantic -Wall -Wextra -o hello hello.cc
To avoid writing such long commands one can create one or more aliases, but more about that firther down.
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 |
| -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
|
For more information about these options, and other possible to specify for g++, see the manual page for g++ (man g++).
Local 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 -pedantic, -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++ -pedantic -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.
Page responsible: The UPP group
Last updated: 2013-03-26
