This is a collection of simple examples on how to use C++ Contracts feature as describe in P2900.
This uses B2 build system for the implementation of the building with clang (or gcc).
You will need to install both B2 and the special Clang/LLVM that supports contracts (GCC should also work, but is not tested).
Note
|
Below you can adjust paths to your liking. |
Follow the instructions here https://contracts.efcs.ca/ that uses the Compiler Explorer pre-built image. You can control where the compiler is installed to avoid needing root privileges. For example:
./bin/ce_install --prefix=${HOME}/.local/opt --enable nightly install 'compilers/c++/nightly/clang ericwf-contracts-trunk'
Assuming the above Clang install with --prefix=${HOME}/.local/opt
on an
x86_64-unknown-linux-gnu
machine:
-
Get B2 source from https://github.com/bfgroup/b2/archive/refs/heads/main.zip (or clone the main branch).
-
Run
./bootstrap.sh
-
Run
./b2 install --prefix=${HOME}/.local
-
Add
${HOME}/.local/bin
to yourPATH
if needed. -
Create a
${HOME}/user-config.jam
. -
Add
using clang : 22.0~contracts : HOME/.local/opt/clang-ericwf-contracts-trunk/bin/clang++ ;
to the${HOME}/user-config.jam
. ReplaceHOME
with the appropriate path to your home directory.
To run the examples, assuming the above installations, do:
-
Go to the root of this repo.
-
Run
LD_LIBRARY_PATH=${HOME}/.local/opt/clang-ericwf-contracts-trunk/lib/x86_64-unknown-linux-gnu b2 toolset=clang-22.0~contracts
The LD_LIBRARY_PATH=
is needed to find the stdlib as it’s not in a system
findable location. And B2 doesn’t hard code the location for it.