Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The following exercises will help you understand how to build your own software on the ATOS HPCF or ECS.

Table of Contents

...

Before we start...

  1. Ensure your environment is clean by running:

    No Format
    module reset


  2. Create a directory for this tutorial and cd into it:

    No Format
    mkdir -p compiling_tutorial
    cd compiling_tutorial


Building simple programs

  1. With your favourite editor, create three hello world programs, one in C, one in C++ and one in Fortran

    Code Block
    languagecpp
    titlehello.c
    collapsetrue
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
        printf("Hello World from a C program\n");
        return 0;
    }


    Code Block
    languagecpp
    titlehello++.cc
    collapsetrue
    #include <iostream>
    
    int main()
    {
      std::cout << "Hello World from a C++ program!" << std::endl;
    }


    Code Block
    languagecpp
    titlehellof.f90
    collapsetrue
           program hello
              print *, "Hello World from a Fortran Program!"
           end program hello


  2. Compile and run each one of them with the GNU compilers (gcc, g++, gfortran)

    Expand
    titleSolution

    We can build them with:

    No Format
    gcc -o hello hello.c
    g++ -o hello++ hello++.cc
    gfortran -o hellof hellof.f90

    All going well, we should have now the executables that we can run

    No Format
    $ for exe in hello hello++ hellof; do ./$exe; done
    Hello World from a C program
    Hello World from a C++ program!
     Hello World from a Fortran Program!



  3. Now, use the generic environment variables for the different compilers ($CC, $CXX, $FC) and rerun, you should see no difference to the above results.

    Expand
    titleSolution

    We can rebuild them with:

    No Format
    $CC -o hello hello.c
    $CXX -o hello++ hello++.cc
    $FC -o hellof hellof.f90

    We can now run them exactly in the same way:

    No Format
    $ for exe in hello hello++ hellof; do ./$exe; done
    Hello World from a C program
    Hello World from a C++ program!
     Hello World from a Fortran Program!


    Tip
    titleAlways use the environment variables for compilers

    We always recommend using the environment variables since it will make it easier for you to switch to a different compiler.



...