Compile and Run C/C++ Programs on Linux

This post provides instructions how to compile and run c/C++ code on Linux.

Check whether gcc is installed

The following commands will display the installation path and version of gcc compiler.

$ whereis gcc
$ which gcc
$ gcc -v

 

Compile And Run C/C++ Programs In Linux

Write your code/program in your favorite text editor . Use extension .c for C programs or .cpp for C++ programs.

Here is a simple “C” program.

$ cat hellow.c
#include <stdio.h>
int main()
{
   printf("hello world!");
   return 0;
}

To compile the program, run:

$ gcc hello.c -o hello1

Or,

$ g++ hello.c -o hello1

In the above example, we used C++ compiler to compile the program. To use C compiler instead, run:

$ cc hello.c -o hello1

If there is any syntax or semantic errors in your code/program, they will be displayed on the screen. You need to fix them first to proceed further.

If there is no error then the compiler will successfully generate an executable file named hello1 in the current working directory (where you put the source c/c++ file).

Now you can execute the program using the following command:

$ ./hello1

To compile multiple source files (e.g., source1 and source2) into executable, run:

$ gcc sourcecode1.c sourcecode2.c -o executable

To allow warnings, debug symbols in the output:

$ gcc sourcecode.c -Wall -Og -o executable

To compile the source code into Assembler instructions:

$ gcc -S sourcecode.c

To compile the source code without linking:

$ gcc -c sourcecode.c

The above command will create a executable called sourcecode.o.

If your program contains math functions:

$ gcc sourcecode.c -o executable -lm

For more details, refer the man pages.

$ man gcc

 

Parallel programming on Ubuntu using OpenMP with C/C++

This post introduces the basic of parallel programming on Ubuntu using OpenMP with C/C++. (MP in OpenMP refers to as Multi-processing.)

OpenMP, is an an API that enables direct multi-threaded, shared memory parallelism. See here for a nice OpenMP tutorial. A good introduction of OpenMP can be found here.

See here for wiki intro to OpenMP.

OpenMP uses a portable, scalable model that gives programmers a simple and flexible interface for developing parallel applications for platforms ranging from the standard desktop computer to the supercomputer.

An application built with the hybrid model of parallel programming can run on a computer cluster using both OpenMP and Message Passing Interface (MPI), such that OpenMP is used for parallelism within a (multi-core) node while MPI is used for parallelism between nodes. There have also been efforts to run OpenMP on software distributed shared memory systems, to translate OpenMP into MPI and to extend OpenMP for non-shared memory systems.

1: Create a text file and paste the follow code into it

Writing the hello world code in C is the same everywhere. There are only minor differences in the code.

#include<stdio.h>

int main( int ac, char **av)

{

#pragma omp parallel // specify the code between the curly brackets is part of an OpenMP parallel section.

{

printf("Hello World!!!\n");

}

return 0;

}

Save the code as anyname.c.  Now let’s compile the code.

2:  Open a terminal (CTRL + ALT + T)

3: Compile the code

In your terminal, cd to the path where the c script you just created is located.

To compile your code, simply type this in the terminal

gcc anyname.c -o anyname.out

If there is any problem in the code, you must fix it and compile again. Now to next (big) step .

4: Number of threads and running in OpenMp

When the code is compiled correctly, we can run it parallel form using OpenMP. firstly we determine number of threads we are going to use. type this in your terminal:

export OMP_NUM_THREADS=4

you can try changing the number of threads into numbers you like. (Note that how many maximum threads you can use depends on your machine’s hardware.)

Then, we are going to compile it using the OpenMP. It is similar to normal compiling but with addition of a few words. The format is like this:

gcc -fopenmp anyname.c -o anyname.out

after that, you can run the program. To run the program, type this into your terminal:

./anyname.out

you will find out that the hello world prints out as many times as your thread number.

 

Notes for c++ with OpenMP :

Here I will just talk about how to compile cpp code, the other part with OpenMP is the same as c code, except for: need to change .c to .cpp and gcc to g++.

If you don’t have C/C++ compiler then first install it by  (Normally this comes with an installed Ubuntu OS):
sudo apt-get build-essential
Then open Gedit or any editor , write your code and save it as
“myprogram.cpp”. (See below for a simple example of c++ code)

#include <iostream>
int main()
{
  #pragma omp parallel 
  {
      std::cout << "hello world \n";
    }
    return 0;
}

Next step is to compile:
Make sure your terminal is cd to the folder where you have saved your .cpp file.
cd /path to/the cpp file
Once done you can compile it using G++
g++ myprogram.cpp
This will output a.out (by default) which is your executable code that can be run as
 ./a.out
If you want to name the output instead of a.out, you can pass a parameter to G++
g++ myprogram.cpp -o myprogram
This time it will output a file “myprogram” which again can be run in the same way
./myprogram

 

Notes for OpenMP:

You don’t install OpenMP. OpenMP is a feature of the compiler. Check that the compiler you are using implements OpenMP, which is an API for parallel programming in C/C++/Fortran.

Check with the OpenMP website, openmp.org for a list of compilers and platforms that support OpenMP programming.

OpenMP Directives:

  1. Directive Format
  2. C/C++ Directive Format
  3. Directive Scoping
  4. PARALLEL Construct

(Thanks Weiming for introducing this cool feature of OpenMP to the lab.)

Posts referenced: