Development/General compiler usage: Difference between revisions

From bwHPC Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 54: Line 54:
When the program crashes or doesn't produce the expected output the compiler can help you by printing warning messages:
When the program crashes or doesn't produce the expected output the compiler can help you by printing warning messages:
<pre>$ icc -Wall ex.c -o ex</pre>
<pre>$ icc -Wall ex.c -o ex</pre>

If the problem can't be solved this way you can inspect what exactly your program does using a debugger. To use the debugger properly with your program you have to compile it with debug information (option -g):
If the problem can't be solved this way you can inspect what exactly your program does using a debugger. To use the debugger properly with your program you have to compile it with debug information (option -g):
<pre>$ icc -g ex.c -o ex</pre>
<pre>$ icc -g ex.c -o ex</pre>
Although -Wall should always be set, the -g option should only be stated when you want to find bugs because it slows down the program and makes it larger.
Although -Wall should always be set, the -g option should only be stated when you want to find bugs, since it may slow down execution and enlarges the binary due to debugging symbols.

The usual and common way to compile your source is to apply compiler optimization. Since there are many optimization options, as a start for now the optimization level -O2 is recommended:
<pre>$ icc -O2 ex.c -o ex</pre>

Both compilers offer a multitude of options (with regard to the above and others), one may check the complete list of options with short explanation on both [[BwHPC_BPG_Compiler#GCC|GCC]] and
[[BwHPC_BPG_Compiler#Intel Suite|Intel Suite]] using option -v --help:
<pre>$ icc -v --help ex.c -o ex</pre>

[[Category:Compiler_software]][[Category:bwUniCluster]]
[[Category:Compiler_software]][[Category:bwUniCluster]]

Revision as of 09:31, 8 May 2014

The basic operations can be performed with the same commands for all available compilers. For advanced usage such as optimization and profiling you should consult the best practice guide of the compiler you intend to use (GCC, Intel Suite). To get a list of the compilers installed on the system execute

$ module avail compiler

Both Intel and GCC have compilers for different languages which will be available after the module is loaded.

Compiler Suite Language Command
Intel Composer C icc
C++ icpc
Fortran ifort
GCC C gcc
C++ g++
Fortran gfortran

The following compiler commands work for all the compilers in the list above even though the examples will be for icc only. When ex.c is a C source code file such as

#include <stdio.h>
int main() {
    printf("Hello world\n");
    return 0;
}

it can be compiled and linked with the single command

$ icc ex.c -o ex

to produce an executable named ex. This process can be divided into two steps:

$ icc -c ex.c
$ icc ex.o -o ex

When using libraries you must sometimes specify where the include files are (option -I) and where the library files are (option -L). In addition you have to tell the compiler which library you want to use (option -l). For example after loading the module numlib/fftw you can compile code for fftw using

$ icc -c ex.c -I$FFTW_INC_DIR
$ icc ex.o -o ex -L$FFTW_LIB_DIR -lfftw3

When the program crashes or doesn't produce the expected output the compiler can help you by printing warning messages:

$ icc -Wall ex.c -o ex

If the problem can't be solved this way you can inspect what exactly your program does using a debugger. To use the debugger properly with your program you have to compile it with debug information (option -g):

$ icc -g ex.c -o ex

Although -Wall should always be set, the -g option should only be stated when you want to find bugs, since it may slow down execution and enlarges the binary due to debugging symbols.

The usual and common way to compile your source is to apply compiler optimization. Since there are many optimization options, as a start for now the optimization level -O2 is recommended:

$ icc -O2 ex.c -o ex

Both compilers offer a multitude of options (with regard to the above and others), one may check the complete list of options with short explanation on both GCC and Intel Suite using option -v --help:

$ icc -v --help ex.c -o ex