Previous: Inheritance Up: C++ basics Next: Templates

This is an automatically generated documentation by LaTeX2HTML utility. In case of any issue, please, contact us at info@cfdsupport.com.

Multiple files

In actual practice (and in OpenFOAM), program source code is split into multiple files. Obviously, a source code for the most of the real-world applications contains thousands and more lines and having them all in one file leads to be messy. This also promotes code re-usability, since it is much easier to extract a piece of code responsible for certain functionality and use it elsewhere. Most classes are thus defined in two files: header file with extension .h (or .H in case of OpenFOAM) and source file with extension .cpp (or .C in case of OpenFOAM), see example in directory multiple_files. There are three files: the header file Array.h, the source file Array.cpp and the file main.cpp containing the main function.

The example uses just a simple class wrapping up array functionality, in this case an array of type double (real number in double precision). The header file contains only declarations and must be included everywhere, where the class defined in files is going to be used.

#ifndef ARRAY_H
#define ARRAY_H
#include  // for assert()
class Array
    int m_length;
    double *m_data;
    Array(int length);

    double& operator[](int index);

    int getLength();

The source file Array.cpp contains the actual functionality and implements functions declared in the header file Array.h. The Array.cpp gets compiled separately and will be linked to the main executable. This has another advantage, because when user makes changes in the main code, the class has not to be compiled again. And vice versa, when implementation of class itself is changed (however, declaration and thus header file have to stay the same), only file containing the class needs to be recompiled. Note, that the Array.cpp also needs to have the header file Array.h included.

#include "Array_int.h"

Array::Array(int length)
    m_data = new double[length];
    m_length = length;

    delete[] m_data;

double& Array::operator[](int index)
    assert(index >= 0 && index < m_length);
    return m_data[index];

int Array::getLength() { return m_length; }

And finally, the main source file with simple code using the Array class:


#include "Array_int.h"

int main()
    Array arrayOfInt = Array(10);

    for(int i = 0; i<10; ++i)
        arrayOfInt[i] = 2*i;

    for(int i = 0; i<10; ++i)
        std::cout << i << "\t" << arrayOfInt[i] << std::endl;

Compilation of this programs will have two steps. First, we compile Array.cpp and main.cpp into object files with extension .o. Second, those object files are linked together into one executable. Actually, compilation of previous programs also had these two steps, but they were performed in one command. Now, they will be executed separately.

# g++ -c Array.cpp -o Array.o
# g++ -c main.cpp -o main.o


# x86_64w64-mingw32-g++ -c Array.cpp -o Array.o
# x86_64w64-mingw32-g++ -c main.cpp -o main.o

This yields files main.o and Array.o. These files need to be linked together to one executable:

# g++ main.o Array.o -o Array


# x86_64w64-mingw32-g++ main.o Array.o -o Array

This results into executable file Array. Running the program should produce following output:

# ./Array
# 0 0
# 1 2
# 2 4
# 3 6
# 4 8
# 5 10
# 6 12
# 7 14
# 8 16
# 9 18

Previous: Inheritance Up: C++ basics Next: Templates