Build C++ code examples

This book comes with an extensive set of examples that demonstrate how to use C++. The examples are organized by chapter and are located in directories prefixed with ch_. The examples are built using CMake, which is a cross-platform build system. CMake is a powerful tool that can generate build files for a variety of build systems, including Makefiles, Ninja, and Visual Studio solutions.

In the following sections shows how to build the examples using a special helper script called cpp-build. This script is located in the root directory of the book’s repository. The script builds all the examples in the book and places the executables in the bin directory and the build files in the build-debug and build-release directories.

Checking out the example code

To check out the example code, clone the book’s repository from GitHub:

git clone https://github.com/lunarc/cpp_course

Building on Windows

The Windows build relies on the package manager Vcpkg to install the necessary dependencies. The script automatically installs Vcpkg and the dependencies if they are not already installed. The script also sets the necessary environment variables to find the dependencies.

Before running the build script on Windows you will first need to install the Vcpkg tool. To install Vcpkg, follow these steps:

  1. Clone the Vcpkg repository from GitHub:

    git clone
    
  2. Change to the Vcpkg directory:

  3. Run the bootstrap script:

    .\bootstrap-vcpkg.bat
    
  4. Run the Vcpkg integration script:

  5. Run the Vcpkg integration script:

    .\vcpkg integrate install
    
  6. Add the Vcpkg directory to the system path:

    set PATH=%PATH%;C:\path\to\vcpkg
    

To build the dependencies go to the root directory of the book’s repository and run the following command:

vcpkg install

This will install all the dependencies needed to build the examples. This can take a while, so be patient.

To build the debug versions of the examples, run the following command:

cpp-build --build --build-type=Debug

To build the release versions of the examples, run the following command:

cpp-build --build --build-type=Release

The finished examples will be located in the bin directory.

To open the Visual Studio solution, go to the build-debug or build-release directory and open the cpp_examples.sln file by typing it in the command line or by double-clicking it in the file explorer.

Building on Linux

To build the examples on Linux, you will need to install the necessary dependencies. The examples will require a recent version of CMake, a C++ compiler, and the following libraries:

  • Eigen3

  • Qt

To install the dependencies on Ubuntu, run the following command:

sudo apt-get install cmake g++ libeigen3-dev qt5-default

To build the debug versions of the examples, run the following command:

./cpp-build --build --build-type=Debug

To build the release versions of the examples, run the following command:

./cpp-build --build --build-type=Release

The finished examples will be located in the bin directory.