Development/General compiler usage: Difference between revisions

From bwHPC Wiki
Jump to navigation Jump to search
(remove heading, as lemma name already provides it)
No edit summary
Line 57: Line 57:
<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 because it slows down the program and makes it larger.
[[Category:Compiler_Software]]
[[Category:Compiler_software]]

Revision as of 14:20, 2 April 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 because it slows down the program and makes it larger.