C++ Exercises
Local test
Project file tree
A minimal CMake project is present under the file tree:
gitlabciintroduction/
└── cxx
├── bench.cpp
├── cmake
│ └── utils.cmake
├── CMakeLists.txt
├── Sphere.cpp
├── Sphere.hpp
└── tests
├── CMakeLists.txt
└── TestSphere.cpp
This CMake project provides the framework needed to build and test a
versionized, dynamic shared library named sphere
.
The CMake configuration is specified in two CMakeLists.txt files:
- One under the main directory: the main CMakeLists.txt
- One under the tests directory
The API provided by this library is at this level composed by a single Sphere class. The Sphere class offers an object constructor with the radius as parameter and a volume method. A benchmark program named bench.cpp is also built and linked with the library. Although the computation is very simple, this benchmark program may be used to compare the influence of some compiler optimization flags. Check that you can build the project and run the test
In the docker command line :
mkdir -p build # Create a build directory
cd build
cmake .. # Create the build environment with CMake
make # Build the project
make test # Run the unitary test
Run the benchmark
CMake provides pre-defined build configurations that may be chosen with the CMAKE_BUILD_TYPE variable.
Among them we are going to consider:
- Debug build type which sets the debug flags (-g with GNU C++)
- Release build type which sets some optimization flags (-O3 -DNDEBUG with GNU C++)
Once a directory has been given as argument to CMake it remains in a cache, the CMakeCache.txt file in your build directory. This cache file is a text file and may be edited by hand.
CMake variables may be set on the command line with -D arguments:
cmake .. -D<VARIABLE_NAME>=<VALUE>
The value remains in the cache file, so when a variable has been modified once with a call to CMake, it is not necessary to define its value anymore on the command line.
To configure our build to produce a Debug type build:
cmake .. -DCMAKE_BUILD_TYPE=Debug
make
./bench
For an optimized build, we change its configuration to:
cmake .. -DCMAKE_BUILD_TYPE=Release
make
./bench
Over this very simple computation, the benchmark time difference between Debug and Release is not dramatic.
With GNU C++ compiler, we can go beyond -O3 optimization flag with the fast-math option, which implies -funsafe-math-optimizations and may break some of the requirements of IEEE and ANSI standards.
To pass a specific argument to the GNU C++ compiler, we use the variable CMAKE_CXX_FLAGS. The arguments passed on the command line to this variable are added to the other compiler arguments. In doubt with the generated specification, one can use the argument VERBOSE=1 with make tool, in order to see all the flags passed to the compiler.
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=-ffast-math
make VERBOSE=1
./bench
You should see a difference on time.
Now, let's keep this configuration in our build directory.
Home | C++ Home | << C++ Previous - Branch creation | >> C++ Next - GitLab CI setup