Skip to content
System Status: All systems are operational • Services are available and operational.
Click for detailed status

C/C++

C and C++ are ISO-standardized, general-purpose, compiled programming languages widely used for system programming, embedded systems, and high-performance applications.

C/C++ compilers on Euler

On Euler the following C/C++ compilers are available via modules:

Compiler Module Command
gcc 12.2.0 module load stack/2024-06
gcc 8.5.0 module load stack/2024-04
Intel oneAPI 2023.2.0 module load stack/2024-06 intel-oneapi-compilers/2023.2.0

Note: Intel OneAPI 2023.2.0 provides both classical compilers (icpc, icc) and LLVM-based compilers (icpx, icx). This is the last release with classical compilers; newer releases will only provide LLVM-based compilers.

The worldwide C/C++ community

Discord

Reddit

YouTube

Example programs

Hello world

Create a file hello.c containing

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
Compile and run the program with
module load stack/2024-06 gcc/12.2.0
gcc hello.c -o hello
./hello
You should see
Hello, World!

Multi-threading with OpenMP

OpenMP enables multiprocessing and concurrent threads.

Create a file hello_omp.c containing

#include <omp.h>
#include <stdio.h>

int main() {
    #pragma omp parallel
    printf("Hello from thread %d of %d\n", omp_get_thread_num(), omp_get_num_threads());
    return 0;
}
Compile and run the program with
module load stack/2024-06
gcc -fopenmp hello_omp.c -o hello_omp
./hello_omp
You should see something similar to
Hello from thread 0 of 8
Hello from thread 3 of 8
Hello from thread 1 of 8
Hello from thread 4 of 8
Hello from thread 2 of 8
Hello from thread 7 of 8
Hello from thread 6 of 8
Hello from thread 5 of 8

Communication via MPI

Message Passing Interface (MPI) enables inter-process communication.

Create a file hello_mpi.c containing

#include <mpi.h>
#include <stdio.h>

int main(int argc, char** argv) {
    MPI_Init(&argc, &argv);

    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    printf("Hello from process %d out of %d!\n", world_rank, world_size);

    MPI_Finalize();
    return 0;
}
Compile and run the program with
module load stack/2024-06 gcc/12.2.0 openmpi/4.1.6
mpicc hello_mpi.c -o hello_mpi
srun -n 4 hello_mpi
You should see
Hello from process 0 out of 4!
Hello from process 2 out of 4!
Hello from process 3 out of 4!
Hello from process 1 out of 4!

Targeting GPUs with CUDA

CUDA enables GPU computing.

Create a file hello_cuda.cu containing

#include <stdio.h>

__global__ void cuda_hello() {
  printf("Hello World from GPU!\n");
}

int main() {
  printf("Hello World from CPU!\n");
  cuda_hello<<<1,1>>>();
  cudaDeviceSynchronize();
  return 0;
}
Compile and run the program with
module load stack/2024-06 gcc/12.2.0 cuda
nvcc hello_cuda.cu -o hello_cuda
srun --gpus=1 hello_cuda
You should see
Hello World from CPU!
Hello World from GPU!