GNU Compilers
GNU Compilers
GNU, a recursive acronym for "GNU is Not Unix", compilers from GCC (GNU Compiler Collection) consists of many compilers such as C, C++, Java, FORTRAN etc.
Important Notes
If you need the newer version than the repo version, you need to load the gcc module using "moldule load gcc"
Compiler Version
To check the version of GNU Compilers, type:
gcc --version
output:
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2010 Free Software Foundation, Inc.
Compiling a Code & Executing
Compiling a C or C++ Program
Interactive:
Request the compute node:
srun -t 1:00:00 --mem=4gb --pty /bin/bash
To compile in C:
Copy the content below in a "hello.c" file hello.c:
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
Compile the code to create "hello" as an executable
gcc -o hello hello.c
Run the Executable
./hello
Your output will be: Hello World
To compile in C++:
Create a cpp file called "hello.cpp" with the following content:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World\n";
return 0;
}
To compile the C++ source file "hello.cpp" with the GNU C++ compiler, use the command
g++ -o hello hello.cpp
Run the Executable
./hello
Your output will be: Hello World
Note: GNU compilers use different commands for compiling C and C++ source files.
Compiling a FORTRAN Program
To compile the FORTRAN 77 source file "test.f" with the GNU FORTRAN 77 compiler, use the command
g77 -o test test.f
To compile Fortran 90/95 source file "test.f90" with GNU Fortran compiler, use the command
gfortran -o test test.f90
Using BATCH
We are going to compile and execute our C++ code in hello.cpp using BATCH:
Copy the following code in a file called "hello.slurm":
#!/bin/bash
#SBATCH -o hello.o%j
#SBATCH --time=00:02:00
#SBATCH -N 1
#SBATCH -n 1
#Compiling section
module load gcc
g++ -o hello hello.cpp
#Copying files to scratch
cp -r hello $PFSDIR
#Running job
cd $PFSDIR
./hello
cp -ru * $SLURM_SUBMIT_DIR
Submit the job:
sbatch hello.slurm
You will find the output at "hello.o<jobid>"
References:
To find a general information about Gnu compilers, please see this Wikipedia page:
Gnu compiler website:
Gnu compiler documentation: