This example demonstrates how to integrate rusty-cpp into a CMake-based C++ project.
Recommended: Add as a git submodule (allows easy updates with git submodule update):
cd your-project
git submodule add https://github.com/shuaimu/rusty-cpp.git third-party/rusty-cpp
git submodule update --init --recursiveThen set the path in your CMakeLists.txt:
set(RUSTYCPP_DIR "${CMAKE_SOURCE_DIR}/third-party/rusty-cpp")The CMake module will check for these automatically:
- Rust/Cargo: Install from https://rustup.rs/
- LLVM/Clang (libclang): (higher versions should also work)
- Ubuntu/Debian:
sudo apt-get install llvm-16-dev libclang-16-dev - macOS:
brew install llvm
- Ubuntu/Debian:
- Z3 Solver:
- Ubuntu/Debian:
sudo apt-get install libz3-dev - macOS:
brew install z3
- Ubuntu/Debian:
mkdir build && cd build
cmake ..
makeThe build will automatically:
- Build
rusty-cpp-checkerif not already built - Run safety checks on all source files
- Fail if any violations are found
- Compile the project
Minimal example:
cmake_minimum_required(VERSION 3.16)
project(myproject CXX)
set(CMAKE_CXX_STANDARD 20)
# Point to rusty-cpp (as submodule or external path)
set(RUSTYCPP_DIR "${CMAKE_SOURCE_DIR}/third-party/rusty-cpp")
# Include the CMake module
include(${RUSTYCPP_DIR}/cmake/RustyCppSubmodule.cmake)
# Enable borrow checking
enable_borrow_checking()
# Your target
add_executable(myapp src/main.cpp src/utils.cpp)
# Add rusty:: types (Box, Arc, Vec, etc.)
target_include_directories(myapp PRIVATE ${RUSTYCPP_DIR}/include)
# Enable checking for this target
add_borrow_check_target(myapp)# Disable borrow checking
cmake -DENABLE_BORROW_CHECKING=OFF ..
# Use debug build of rusty-cpp (slower but more debug info)
cmake -DRUSTYCPP_BUILD_TYPE=debug ..
# Make borrow check failures fatal (stop on first error)
cmake -DBORROW_CHECK_FATAL=ON ..Since rusty-cpp is rapidly evolving, update your submodule frequently:
cd third-party/rusty-cpp
git pull origin main
cd ../..
git add third-party/rusty-cpp
git commit -m "Update rusty-cpp"Or update all submodules at once:
git submodule update --remote --merge| Function | Description |
|---|---|
enable_borrow_checking() |
Enable checking, verify dependencies, create build target |
add_borrow_check_target(target) |
Add checks for all sources in a target |
add_borrow_check(file.cpp) |
Add check for a single file |
See src/safe_example.cpp for examples of:
@safefunction annotations@unsafeblocks for STL operations- Borrow checking patterns
- Scope-based lifetime management