Compiling Code

De Wiki de Calcul Québec
Aller à : Navigation, rechercher
Cette page est une traduction de la page Compilation de code et la traduction est complétée à 100 % et à jour.

Autres langues :anglais 100% • ‎français 100%



In order to compute something on one of our servers, you will likely need to compile a source code - in Fortran, C or C++ for example - to produce a binary executable for Linux. To do so, you will use a compiler. Many compilers are available on our servers. On all servers, we at least provide the GCC compiler suite as well as that from Intel. Some servers also provide other compilers such as Sun, PGI, or PathScale. To get a list of compilers supported by your server, refer to the list of modules installed on each of Calcul Québec servers . For documentation relative to each compiler, see the following pages:

If the application that you want to execute has been written by someone else, it's possible that its compilation has been simplified by means of one of the following tools:

General Principles

Compiling an application is normally a two stage process. First, the compiler compiles each of the source code files into an object file (.o). Next, the linker links together all of these object files into an executable file or binary. Compilers accept various options when they're called and you need to refer to the documentation for the specific compiler that you're using to ensure that you choose the right options. Nevertheless, the majority of compilers accept the following options or flags: -o, -c, -I, -L, -O1, -O2, -O3, -g, -D, -l. The chart below explains in greater detail the effect of these options. Note that the examples use gcc but apply equally well to compilers from Intel or Portland Group.

Option Description Example
-o Sets the output file (name of the executable)
gcc helloworld.c -o app.exe
-c Performs compiling and linking steps separately. Most useful to compile various modules independantly before linking them into an executable.
gcc -c helloworld.c -o helloworld.o
-I Indicates to the compiler which directories to crawl to find external source codes (compiling step)
gcc -I$MPIDIR/include ...
-L Indicates to the linker which directories to crawl to find compiled binaries (librairies for example)
gcc -L$MPIDIR/lib ...
-O0 to -O3 Enables more and more agressive optimizations. Starting with -O3, some optimizations may actually slow down the code in some cases.
gcc -O3 helloworld.c -o app.exe
-g Includes debuging information within the binaries. This makes it easier to use a debugger to track down bugs. Note that the option must be given both at the compiling and the linking steps.
gcc -g helloworld.c -o app.exe
-D Defines macros which will used by the precompiler to change the compiled code. Generally, such macros are used to increase portability of a code. For example, some parts of the code may be chosen by the precompiler wether you compile serial or parallel, on Linux or Windows, and so on.
gcc -DHAVE_MPI ...
-l This option indicates a library required for linking. For example, if you add the option -lpthread, the linker will look for a file named or libpthread.a to find symbols used in your code.
gcc -lpthread -lm ...
-w Asks the compiler to hide warnings (warning).
gcc -w ...

Note that unlike Windows, in Linux the order of the libraries is important. The compiler examines a library only once and will only register those objects whose symbols were encountered during the compilation. Thus, because the LAPACK library uses functions defined in the BLAS library, you have to first list Lapack, so that the line for linking a program that contains Lapack functions would have the form,

[name@server $] gcc linear_algebra_code.o -o app.exe -llapack -lblas -lpthread -lm -lc

To choose the exact name of the libraries to use, visit the pages on LAPACK, BLAS and MKL.

Compiling an MPI Program

To compile an MPI program, you should normally use the compiler that is encapsulated in a script provided by the MPI distribution, such as mpicc, mpicxx or mpif90. For more details, check the page that corresponds to the MPI library you're using, such as Open MPI, MVAPICH2 or MPICH.

Compiling an OpenMP program

To compile an OpenMP program, just adding a compiler option on the command line is enough. For compilers of the GCC suite, add option -fopenmp; for those of the Intel suite, add option -openmp; for those of the PGI and PathScale suites, add option -mp.

Outils personnels
Espaces de noms

Ressources de Calcul Québec