This project contains examples and notes based on Bjarne Stroustrup's "A Tour of C++, 3rd Edition" book. It's organized by chapters, with each chapter containing example code that demonstrates the concepts discussed in the book.
a-tour-of-cpp/
├── CMakeLists.txt # Main CMake configuration
├── CMakePresets.json # CMake presets configuration
├── vcpkg.json # vcpkg manifest file
└── src/ # Source files, organized by chapter
└── ch01/ # Chapter 1: The Basics
├── CMakeLists.txt
├── hello_world.cpp
├── types_variables.cpp
├── constants.cpp
├── minimal.cpp
└── pointers_arrays_references.cpp
- CMake 3.20 or higher
- Clang compiler (automatically selected in CMakeLists.txt)
- vcpkg (for package management)
Currently this project depends on:
- fmt: Modern formatting library for C++
-
Clone this repository:
git clone https://github.com/wangqin0/a-tour-of-cpp.git cd a-tour-of-cpp
-
Set up vcpkg:
# If you don't have vcpkg installed: git clone https://github.com/microsoft/vcpkg.git ./vcpkg/bootstrap-vcpkg.sh # On Unix-like systems ./vcpkg/bootstrap-vcpkg.bat # On Windows
You'll need to set up the following environment variable:
-
VCPKG_ROOT: Path to vcpkg installation
Windows:
set VCPKG_ROOT=C:\path\to\vcpkg
Unix/macOS/Linux:
export VCPKG_ROOT=/path/to/vcpkg
- Open Start menu and search for "Environment Variables"
- Click "Edit the system environment variables"
- In the System Properties window, click "Environment Variables"
- Add or modify the variables in the User variables section
- Click OK to save
Add to your ~/.bashrc
, ~/.zshrc
, or appropriate shell configuration file:
export VCPKG_ROOT=/path/to/vcpkg
This project uses CMake Presets to simplify the build process. The CMakePresets.json
file defines standardized build configurations.
# Configure and build using CMake presets
cmake --preset=default
cmake --build --preset=default
When you run these commands:
- CMake processes your vcpkg.json file
- vcpkg automatically downloads and builds the dependencies
- The packages are installed to the build directory
- CMake finds and uses these packages
- Install the CMake Tools extension
- Open the project folder in VS Code
- The extension will automatically detect and use the CMake presets
After building, you can run any of the examples. From the build directory:
# Run the Hello World example
./src/ch01/ch01_hello_world
# Run the Types and Variables example
./src/ch01/ch01_types_variables
# Run the Constants example
./src/ch01/ch01_constants
# Run the Minimal example
./src/ch01/ch01_minimal
# Run the Pointers, Arrays, and References example
./src/ch01/ch01_pointers_arrays_references
The vcpkg.json
file specifies your project's dependencies:
{
"dependencies": [
"fmt"
]
}
To add a new dependency, simply add its name to the "dependencies" array.
As you progress through the book, you can add more chapters by:
- Creating a new directory under
src/
(e.g.,src/ch02/
) - Adding example code files
- Creating a
CMakeLists.txt
file in the new chapter directory - Adding the new chapter directory to the main
CMakeLists.txt
usingadd_subdirectory()
This project is licensed under the MIT License - see the LICENSE file for details.