nooneknowspeter/cpplings
A terminal based learning experience for C++, inspired by Rustlings
ec8e7bd481098472a4f6ae850894328d0632e343
90981c05aeafd9dd885ba8b49d6afa8f6df77adb
59bce23aa6ad232655a288852999d94a78edaf7f
cpplings, inspired by Rustlings, is a set of hands-on exercises to help you get comfortable reading and writing modern C++.
These exercises reference the excellent learncpp and are conceptually related to cplings, which adopted this idea earlier.
We recommend doing these exercises alongside reading learncpp and checking out the additional learning resources listed below and in the comments of the source code.
This project uses Zig as a toolchain instead of CMake or any other build system used for C++ development. This is a deliberate choice to test Zig’s practical usefulness in real-world C++ workflows.
NOTE
Although Nix technically solves the problem with managing dependencies and their versions, I strongly support using Zig over CMake as a toolchain for its vector processing (building wth SIMD) and for being an elegant and minimalistic language.
Start by cloning this repository on your machine.
NOTE
If you want to store and save your progress you can fork the repository or change the remote orgin URI to your own repository
This project uses a Makefile
to simplify common development tasks.
You will need make
installed on your system.
If you do not want to install make (make is provided in the Nix shell environment)
or don't have it installed, you can use the commands specified in the Makefile
.
If you would like to compile using your own package manager; homebrew
, pacman
, apt
, etc.
You can install the following:
If you're using asdf
, you can install the dependencies using:
asdf install
Check .tool-versions
and ensure you have the plugins added such as:
asdf plugin add <plugin-name>
If you're using NixOS or Nix installed, a flake based nix shell environment is provided with all required dependencies, such as Zig, and more.
NOTE
If you do not have Nix installed, install it here.
Ensure that you install the multi-user version of Nix
NOTE
If you do not want to install Nix on your system but have Docker installed and running, you can use the Docker approach which uses an Arch Linux container with Nix installed.
Start the environment with:
NOTE
Ensure you have extra-experimental features on with
experimental-features = nix-command flakes
in your/etc/nix/nix.conf
or~/.config/nix/nix.conf
or
use it inline
nix --extra-experimental-features "nix-command flakes" develop
nix develop
This gives you a fully configured development shell without installing the dependencies globally on your system.
NOTE
A
.envrc
is provided to allow the nix shell to be used automaticallyMake sure to have
direnv
installed and usedirenv allow
when in the project directoryIf it gets a little annoying, you can use
direnv disallow
to disable it and manually enter the shell withnix develop
If you are unfarmiliar with Nix, it is a package manager and system configuration tool. It prevents the "it works on my machine" problem by allowing declerative and reproducible builds of environments.
You can read more about it here and the extra experimental features (flakes) here.
A Dockerfile
is provided for containerized development.
The container is based off the Arch Linux image with a multi-user version of Nix installed.
The container is purely made for compiling, it uses a shared volume with the cpplings folder on your system. This allows coding and editing to be done in a decoupled/modular way. It can be done directly on your machine in the cpplings directory with the installed development tools on your system or another environment (Code editor running in another container or some sort of system).
You can build and run the container locally using:
make docker-build && \
make docker-run
The image will be tagged as cpplings
.
The container mounts your current project directory into /cpplings
in the container.
This setup is ideal for isolated or reproducible builds.
A pre-built image is also available via GitHub Container Registry by running:
make docker-run
NOTE
If you would like to set up an environment as quick as possible and haven't cloned this repository, you can use:
Unix
# change CPPLINGS_DIR to your liking \ # example: ~/projects/cpplings or ~/git/cpplings \ export CPPLINGS_DIR=~/cpplings && \ export CPPLINGS_OCI_IMAGE_URL=ghcr.io/nooneknowspeter/cpplings:main && \ git clone https://github.com/nooneknowspeter/cpplings.git ${CPPLINGS_DIR} && \ cd ${CPPLINGS_DIR} && \ docker run -itd -v .:/cpplings --name cpplings ${CPPLINGS_OCI_IMAGE_URL} && \ # uncomment the line below if you're using VS Code \ # code . -r && \ # or replace command with editor of choice \ # nvim . && \ # execute the line below seperately (editor's terminal or seperate terminal instance) # if you are running the editor command above docker exec -it -w "/cpplings" cpplings bash
Windows (Powershell)
# change CPPLINGS_DIR to your liking \ # example: $HOME\projects\cpplings or $HOME\git\cpplings \ $env:CPPLINGS_DIR = "$HOME\cpplings"; \ $env:CPPLINGS_OCI_IMAGE_URL = "ghcr.io/nooneknowspeter/cpplings:main"; \ git clone https://github.com/nooneknowspeter/cpplings.git $env:CPPLINGS_DIR; \ cd $env:CPPLINGS_DIR; \ docker run -itd -v .:/cpplings --name cpplings $env:CPPLINGS_OCI_IMAGE_URL; # uncomment the line below if you're using VS Code \ # code . -r; \ # or replace command with editor of choice \ # nvim .; \ # execute the line below seperately (editor's terminal or seperate terminal instance) # if you are running the editor command above docker exec -it -w "/cpplings" cpplings bash
Copy and paste, then edit to your liking.
This will launch the shell of the container instantly, use it in your VS Code terminal, or a split pane if you're using terminal multiplexer, or just your terminal.
NOTE
Run the container's shell using:
docker exec -it -w "/cpplings" cpplings bash
NOTE
Using
nix develop
will result in an ownership problemInside the docker container's shell run:
chown -R root:users /cpplings && \ chmod a+rwX /cpplings
Enter inside the nix development shell using:
nix develop
NOTE
You may encounter a problem with not being able to execute a few commands such as
git
on your local machine because the owner of the folder will be rootYou will have to change the owner to your local machine's user
chown -R <username> /path/to/cpplings
For VS Code users who are unfirmiliar with C++, an extensions.json
file is included.
It has the recommended extensions for writing and debugging C++, and the other tools in this project.
For Neovim users using Nix as their package manager (NixCats, etc.),
can use nix develop
for a nix dev shell with all project related
LSPs, DAPs, formatters and linters.
If you're on NixOS and still use Mason as a package manager for Neovim LSPs and so on, you can still use this shell to install the required packages (clangd, zsl, ...) through Mason.
You can use whichever tools you prefer and may want to try.
Have fun.
learncpp — Comprehensive and beginner-friendly C++ guide.
cppreference — The canonical C++ reference.
cppbyexample — C++ concepts via concise examples.
learnxinyminutes: C++ — Fast overview for experienced programmers.
zig's build system — Documentation on Zig's build system
ziglings — Zig version of Rustlings, worth checking out if you're curious about Zig.
Maintain It With Zig — A well written article by Loris Cro, explaining the benefits of Zig's build system in the world of C/C++
Why Zig When There is Already C++, D, and Rust? — Another article on the benefits of Zig
allyourcodebase - C/C++ projects built with Zig
clangd compiler flags - Make Clangd work with C++ using Zig as a build system