Compilation and linking
Compilation and linking
Broadly speaking, compilation of C, C++, or Fortran programs requires a command of the form:
gcc HelloWorld.C -o HelloWorld
which will compile the code in HelloWorld.C
into an
executable called HelloWorld
.
Compiler warnings
You are strongly advised to enable all compiler warnings when compiling your own code.
With gcc, the options are -Wall -Wextra
. You should not ignore any warnings that
are thrown up as they often contain suggestions that your code will not perform exactly as you expect.
Optimization
In order to automatically optimize your code, you should add the -O2
or -O3
option to the compilation command. The compiler is not intelligent, but it will apply
many standard optimizations to your code which will (very likely) improve its performance
dramatically, particularly if you are using C/C++. However, this flag is no substitute for
your own optimization. You may be able to find a better algorithm to do the same operation,
or make improvements to the code that allow the optimizer to do its job better.
Machine architecture
It is possible to optimize your code for a particular machine architecture, using
the -march=native
option for gcc. However, using this means that your code
may not be able to run on all machines in the group. For example, compiling in this way
on hydra03
means that the executable will not be able to run on hydra01
, hera
,
or several other machines. You may wish to consider (and test) whether having to recompile for multiple machines
is worth it (i.e. you get significantly better performance) or whether you should compile
for the lowest common denominator. See the GCC manual for
details.
Debugging
In order to debug code compiled with gcc
you should use the gdb
debugger. The manual for this is available. If while debugging you find that information has been optimized out, you should try re-compiling your program using the -O0
option.
gdb and C++
gdb
can recognize C++ STL objects and print them
appropriately.
Intel compilers
Under Intel's licensing agreement (of November 2020), the oneAPI compilers are installed on CSC machines, at /lsc/opt/intel
.
The licence terms can be found at /lsc/opt/intel/licensing/latest/licensing/2024.2/license.htm
.
Enabling the compilers
In order to set all necessary environment variables, run: source /lsc/opt/intel/setvars.sh
Compilers
The available executables are then:
Executable | Function |
icx | C Compiler |
icpx | C++ Compiler |
ifort | Fortran Compiler |