Template for a modern C++ project using CMake.
Also read the justfile to see available commands.
- Only Linux is supported
- This project uses devcontainers to avoid dependencies polluting the host. Specifically, we use podman instead of Docker, which enables the devcontainer to run on a host that is itself containerized (ie. a GCP Cloud Workstation instance)
- The devcontainer uses
brewfor a few reasons:- We use a non-root user in the container so you can't use the system package manager
- The LLVM build in Fedora's repos lacks debug symbols
- The devcontainer is setup to use fish as the default shell
On the host:
git clone --recurse-submodules https://github.com/rdong8/cpp_project.git
cd cpp_project/Also make sure you have podman installed on the host. For example:
# Fedora
sudo dnf -y install podman
# Ubuntu
sudo apt update
sudo apt -y install podmanThen run id on the host to determine your user's UID and GID. Use that to fill in the build.dockerfile.args.HOST_UID and build.dockerfile.args.HOST_GID values in the devcontainer.json file.
Then build the devcontainer. All commands after this point are to be run in the devcontainer, not on the host.
just venv # Creates a virtual environment
just py-deps # Installs the Python dependencies. Use `just py-deps 1` to force a reinstall.In the devcontainer configuration, a volume has been configured for Conan. This helps persist Conan's cache and build profile even when the container is destroyed.
Conan profiles specify toolchain details for building packages. There are 2 kinds:
- Build profile: describes the system where the build is happening
- Used to build tools that will run on the build machine during the build process like CMake, Ninja, etc.
- Host profile: describes the system where the built binaries will run
- Used to build your project and its dependencies
In other words, the build profile is used to build tools, whereas the host profile is used to build your project. Conan can automatically detect an appropriate build profile for you:
just create-conan-profile buildYou can run this once and will basically never have to touch it again unless the toolchain provided by the container image changes (ie. you switch to a newer Fedora image).
On the other hand, the host profile specifies the things you care about like the C++ version, compiler, standard library, etc. you plan on using. An example is given in conan/profiles/host. The project is configured to use this profile by default. If you want to copy it into the standard Conan config location you can run:
just copy-conan-profile hostAnd then set the conan_host_profile variable in the justfile to its name (host) instead of a relative path.
You can edit a profile in the config location with just edit-conan-profile host.
Note that if your compiler.version in your host profile is too new, you may get an error from Conan. Just edit ~/.conan2/settings.yml and add it there.
This step needs to be run each time anything in conan/ is modified. Build the project's C++ dependencies with Conan:
just conan-installRun the CMake configure. Generates the underlying build system files.
just configAt this point, you may want to restart clangd (clangd: Restart language server in VSCode command palette) so it picks up the new compile commands.
Build a target:
just build target-nameYou can omit the target name to build everything.
The target will end up in ./build/src/path/to/target/build_type/target.
To open the documentation in the default browser (must be built first via just build docs):
just docsYou may also pass a command that will be used to open the index.html file:
just docs firefox
just docs 'flatpak run com.brave.Browser' # You need to use Flatseal to give the flatpak permission in this caseRun all tests:
just testAny flags will be forwarded to ctest, for example:
just test -R math # Run tests matching regular expression "math"Registers pre-commit hooks to run automatically.
just pre-commitClean the build directory and Conan generated files:
just cleanAfter cleaning, you have to re-run everything from just conan-install and after.
Clean Python files as well:
just clean-allAfter this you also have to run just venv py-deps.
You can also delete all installed Conan packages matching a pattern:
just clean-conan 'boost/*'
just clean-conan # Removes everything