Hide menu

TDDD38 Advanced Programming in C++

Practical hints

Compiling and linking C++ programs with g++

To get the latest version of GCC, the default module must be replaced by prog/gcc/4. This is done in a terminal window (only valid in that window) with the following module commands:

   module add prog/gcc/4

Prefer to have the corresponding lines in .login in your home directory, after the ordinary module command found there. Then you don't have to bother with this in every new terminal window. To make the g++ compiler extra pedantic and also only allow standard C++11, the following options are strongly recommended:

    g++ -std=c++11 -pedantic -Wall -Wextra lab1.cc

In compiler versions before 4.7, option -std=c++0x was used instead of -std=c++11. Option -g is given to prepare for debugging, e.g. using the debugger DDD.

Because of problems with the link libraries, the following link options must be supplied at present:

   -L/sw/gcc-${GCC4_V}/lib -static-libstdc++

To avoid typing so many options in commands an alias kan be defined in file .cshrc.private in your home directory. PLace, for example, the following alias command there (the options -pedantic, -Wall, and -Wextra can be something one want to experiment with):

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

Do not use command 'gcc' to compile/link C++ programmes, since it will not, e.g., take C++ specific link libraries into account.

Printing program code

The command a2ps ("ascii-to-postscript") can be used for nice printouts of tedx files, e.g. program code. Example (output from a2ps is piped to lp for printing on printer sub2w):

    a2ps -nh -ns -p -n -fCourier9 file.cc | lp -d sub2w

Some switches for a2ps:

  -nh  No header is printed on the pages.
  -ns  No frame is marking the page margin ("no shit"?-).
  -p   Portrait, otherwise landscape and printout in two columns.
  -n   Line numbers are prnted.
  -f   Font.

End-of-file for interactive input

When running a program reading input from standard input (cin), and you are inputting data on the keyboard, you have to supply end-of-file in a special way.

Unix: End-of-file is given as Control-D first on a new line. I.e. after the last inputted data, hit RETURN (ENTER) and then Control-D first on the new line.

PC: End-of-file is given as Control-Z (new line is not necessary).


Page responsible: Tommy Olsson
Last updated: 2014-01-21