CMake file for using CXX and Corrosion in CMake project to call Rust functions from C++ and C++ from Rust
This is a fork of the rusty_cmake repository to use CXX and Corrosion in a CMake project with the following modifications:
- Function to get STEM is removed in a favor of one-line solution;
- Simplify by using a simple one CMakeLists file for the project;
- Windows-related code is removed (Linux and MacOS are supported);
- Minor code changes to simplify and use a modern C++.
This cmake/corrosion_cxx.cmake
can be imported in any CMake project to simplify Rust code usage in C++.
Current solution is a battle-tested and cmake/corrosion_cxx.cmake
file is used in a Comm application development.
Examples are located in examples
folder.
This repo examples can be used as a sandbox playground for testing simple ideas of the Rust library integration in the current C++ CMake project (examples/rust-from-cpp
) and as examples of the usage and calling C++ code from Rust (examples/cpp-from-rust
).
- Example of using a different primitive types for arguments and returns;
- Example of using Rust Result type and Anyhow in return to C++;
- Example of using panics in Rust when calling from C++.
- Linux or MacOS
- CMake
- Clang
- Corrosion
- Rustup
- Nix (optional) or Docker (optional)
Run CMake and build commands in the examples/rust-from-cpp
folder:
cmake -B build . && make -C build -j4
.
Then you can run the example app by calling:
build/cxx_cmake
.
If you are using Nix as a development environment you can use a nix shell with all dependencies:
nix-shell -p cmake -p clang -p rustup -p corrosion -p libiconv -p git --pure
And then run build commands from the CMake section above.
There is a Docker file to build and run the example app in Docker container. To build it run the build command from the project root directory:
docker build . -t cxx-corrosion-cmake -f examples/Dockerfile
Then you can run the example app by calling:
docker run cxx-corrosion-cmake
Create a packages in package managers to help tracking updates in the project instead of manually track the changes.
- Make it nix package
- Make it vcpkg package
Comm.app is a crypto-native chat (think "Web3 Discord").