Programming in C++

Working with Multiple Files

Gerald Senarclens de Grancy

Programming Projects

  • Growing projects raise the need to use multiple source files
  • Functionality belonging together is kept in the same file (aka. module)
  • Rule of thumb: different concerns go into different files; eg.
    • Code for user interaction
    • Retrieving data from and storing data in a database
    • Business logic and algorithms

Working with Multiple Files

Which problems do multiple files solve?

  • Sharing code for re-use between programs becomes possible
  • Functionality can be logically grouped together into modules
  • Multiple programmers can work on a project without frequent conflicts
  • Compile time can be drastically reduced (only re-compile affected code)
  • Multiple processors / cores can be used for compilation
  • Interface code can be clearly separated out into header files

Which New Problems Do They Cause?

  • Larger projects increase complexity
  • Splitting functionality requires experience (software architecture)
  • Compilation becomes complicated => we need build automation tools
  • Build tools need to know about dependencies
  • Intermediate files are created and might need to be cleaned up

Compiling and Executing a C++ Program

Executable program files are created from readable source code.

[cpp source icon]
main.cpp
[cpp source icon]
executable

For creating executables, a compiler toolchain is needed.

  • GCC - GNU Compiler Collection
  • Clang - C language family frontend for LLVM
  • MSVC - Microsoft Visual C and C++ compiler

Creating a C++ executable is a four step process.

  1. Preprocessor
  2. Compiler
  3. Assembler
  4. Linker

Example

Download hello.cpp

By default, all four stages (preprocessor, compiler, assembler and linker)
are done

clang++ hello.cpp  # or ` hello.cpp`
./a.out

Provide a name for the created executable (instead of the default a.out

clang++ hello.cpp -o hello
./hello

Compilation can be done with either clang++ or .

clang++ $INFILE -o $PROGRAM_NAME
 $INFILE -o $PROGRAM_NAME

1. Preprocessor

  • Essentially a pure text processor
  • Removes comments, performs includes etc.
  • Usually never done separately

Stop after preprocessor and send output to stdout

clang++ -E hello.cpp

2. Compiler

  • Translates high level code to a lower level (assembly language)
  • Parses the source code
  • Performs type checking
  • Usually never done separately
  • Must recognize all identifiers (function declarations etc.)

Creates assembler files (usually *.s)

clang++ -S hello.cpp

3. Assembler

  • Translates assembly language to object code (machine language)
  • Creates *.o files
  • Usually done separately when working with multiple input files
    • Impossible to create executables without main(.)
    • Impossible to create executables with multiple main(.)
clang++ -c hello.cpp

4. Linker

  • Combines object files into a unified executable program, resolving all symbols
  • Should be run separately when working with multiple input files
clang++ -o hello hello.o

Header Files

Why do we Need Header Files?

Imagine a project with three files: main.cpp and modules array.cpp and ui.cpp

[cpp source icon]
main.cpp
[cpp source icon]
array.cpp
[cpp source icon]
ui.cpp
=>
[cpp source icon]
executable

What do we have to do to satisfy the compiler when using functionality of array.cpp in all other files?

What if we have thousands of files and change array.cpp?

Solution

Write the declarations exported by a source file into a separate header file.

Include the header file where the declarations are used.

Header files do not need to be compiled separately since their content is copied into the source files by the preprocessor.

Example

  1. main.cpp uses functions defined in array.cpp and ui.cpp
  2. ui.cpp uses functions defined in array.cpp
project overview

#include Guards

A construct used to avoid the problem of double inclusion when dealing with the include directive.

#ifndef FILENAME_
#define FILENAME_

// ... your header file's code

#endif // FILENAME_

#include guards can also be referred to as macro guards, header guards or file guards

Compiling Multiple Files

Compiling all source files produces a single executable.
Remember: header files are already included in the *.cpp files.

clang++ *.cpp

It would be much faster to re-compile only the files that need to be re-compiled and then link the object files.
How do we know what needs re-compilation?

Everything affected by a change must be re-compiled. If, for example, a header file is changed, we need to re-compile all *.cpp files that #include that header file.

Example 1/2

Download main.cpp, ui.cpp, ui., array.cpp and array. (or data/cpp/multiple_files).
Compile all files at once:

clang++ *.cpp -o array_tool

Since we did not create object files, any change would require complete re-compilation. Instead, we should create object files and link them together.

clang++ -c *.cpp
clang++ *.o -o array_tool

Example 2/2

Now, make a change to main.cpp. Check the return value of get_dimension(.) and issue a warning if the value is 0.

Once done, we can just re-compile main.cpp and link to the existing object files.

clang++ -c main.cpp
clang++ *.o -o array_tool

Exercise

Extend the program to contain another function
int amax(int* array, size_t dimension)
that returns the maximum of the given array. Add the function declaration to array. and the definition to array.cpp. Call the function in main.cpp and print the result to stdout.

Which files need to re-compiled to object files before a new executable can be linked? Once you know the answer, compile them and create a new executable.

Build Automation Tools

Build tools allow you to re-compile exactly the files that need to be re-compiled with a single command or the click of a button.

There are many different build tools for each programming language.
For C and C++, popular examples are

  • Visual Studio (also takes care of this)
  • GNU make
  • CMake
  • ...

GNU Make

  • Controls the generation of executables and other non-source files
  • + Independent of IDE
  • + Open source
  • + Allows compiling files in parallel
  • - Platform dependent
  • - Requires a complicated Makefile for the project

GNU make quick reference, Makefile tutorial

GNU Make Example 1/2

all: array_tool  # default target

array_tool: main.o ui.o array.o
	g++ -o array_tool main.o ui.o array.o

array.o: array.cpp array.hpp
	g++ -std=c++20 -Wall -c array.cpp
ui.o: ui.cpp ui.hpp array.hpp
	g++ -std=c++20 -Wall -c ui.cpp
main.o: main.cpp ui.hpp array.hpp
	g++ -std=c++20 -Wall -c main.cpp

.PHONY: clean
clean:
	find . -name '*~' -o -name '*.o' -o -name 'array_tool' | xargs rm

Download Makefile

Makefiles quickly get complicated: example Makefile using more features

GNU Make Example 2/2

To make a build, run make -f YourMakeFile

If no -f option is present, make will look for the makefiles GNUmakefile, makefile, and Makefile, in that order.

make  # run the default (first) target
make array_tool  # run the target explicitly (usually it is aliased to `all`)
make clean  # run the target clean
make array.o  # run the target to build array.o if that is needed
make -j8  # run 8 jobs in parallel (use up to 8 CPU cores)

CMake

  • + Powerful, cross-platform build environment
  • + Independent of IDE
  • + Open source
  • + Harnesses the power of GNU Make by generating the Makefile
  • + Builds "out of source" in a separate directory by default

CMake Documentation

CMake Example 1/2

cmake_minimum_required(VERSION 3.18)
project(array_tool VERSION 1.0)
set(CPP_SRCS  # all cpp source files
  array.cpp
  main.cpp
  ui.cpp
)
set(CMAKE_CPP_STANDARD 20)
add_executable(${PROJECT_NAME} ${CPP_SRCS})

Download CMakeLists.txt

CMake isn't trivial either: example CMakeLists.txt using more features

CMake Example 2/2

To make a build, run the following commands

mkdir build  # create directory for all files created during build
cd build
cmake ..  # create the Makefile for your setup
make -j8  # start actual build on 8 cores; alternatively `cmake --build . -j8`

Questions
and feedback...