You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Example of using [CXX](https://cxx.rs/) and [Corrosion](https://github.com/corrosion-rs/corrosion) in CMake project to call Rust functions from C++
1
+
# CMake file for using [CXX](https://cxx.rs/) and [Corrosion](https://github.com/corrosion-rs/corrosion) in CMake project to call Rust functions from C++
2
2
3
-
This is a fork of the [rusty_cmake](https://github.com/trondhe/rusty_cmake) repository to use CXX and Corrosion in a CMake project to use CXX and Corrosion in a CMake project with the following modifications:
3
+
This is a fork of the [rusty_cmake](https://github.com/trondhe/rusty_cmake) repository to use CXX and Corrosion in a CMake project with the following modifications:
4
4
5
5
### `cxx_corrosion.cmake` file:
6
-
### `cxx_corrosion.cmake` file:
7
-
- Windows-related code is removed (Linux and MacOS are supported) (Linux and MacOS are supported).
8
-
- Windows-related code is removed (Linux and MacOS are supported) (Linux and MacOS are supported).
9
-
- Simplify by using a simple one CMakeLists file for the project.
10
-
11
-
### Code examples to call Rust functions from C++:
12
-
-[Example of using a different primitive types](https://github.com/geekbrother/cxx-corrosion-cmake/blob/main/src/main.cpp) for arguments and returns.
13
-
- Example of using [Rust Result type](https://cxx.rs/binding/result.html#returning-result-from-rust-to-c) in return and [Anyhow](https://docs.rs/anyhow/latest/anyhow/).
14
-
-[Example of using panics](https://github.com/geekbrother/cxx-corrosion-cmake/blob/2a981b2ec34ee4d4ffe261b1be07691f74c31a04/src/main.cpp#L15) in Rust + C++.
15
-
### Code examples to call Rust functions from C++:
16
-
-[Example of using a different primitive types](https://github.com/geekbrother/cxx-corrosion-cmake/blob/main/src/main.cpp) for arguments and returns.
17
-
- Example of using [Rust Result type](https://cxx.rs/binding/result.html#returning-result-from-rust-to-c) in return and [Anyhow](https://docs.rs/anyhow/latest/anyhow/).
18
-
-[Example of using panics](https://github.com/geekbrother/cxx-corrosion-cmake/blob/2a981b2ec34ee4d4ffe261b1be07691f74c31a04/src/main.cpp#L15) in Rust + C++.
6
+
- Function to get STEM is removed in a favor of one-line solution;
7
+
- Simplify by using a simple one CMakeLists file for the project;
8
+
- Windows-related code is removed (Linux and MacOS are supported);
9
+
- Minor code changes to simplify and use a modern C++.
19
10
20
11
# Why?
21
12
22
-
This repo can be used as a sandbox playground for testing simple ideas of the Rust library integration in the current C++ CMake project and as examples of the usage.
23
-
24
-
# Usage
25
-
26
-
## Dependencies
27
-
- Linux or MacOS
28
-
- CMake
29
-
- Clang
30
-
## CMake
31
-
32
-
Run CMake and build commands in a project's root:
33
-
34
-
`cmake -B build . && make -C build -j4`.
13
+
This `cmake/corrosion_cxx.cmake` can be imported in any CMake project to simplify Rust code usage in C++.
35
14
36
-
Then you can run the example app by calling:
15
+
Current solution is a battle-tested and `cmake/corrosion_cxx.cmake` file is used in a [Comm](https://comm.app) application development.
37
16
38
-
`build/cxx_cmake`.
17
+
# Examples
39
18
40
-
## Nix
19
+
Example code is located in `examples` folder.
41
20
42
-
If you are using [Nix](https://nixos.org/download.html) as a development environment you can use a nix shell with all dependencies:
This repo example can be used as a sandbox playground for testing simple ideas of the Rust library integration in the current C++ CMake project and as examples of the usage.
47
22
48
-
And then run build commands from CMake above.
49
-
50
-
## Docker
51
-
52
-
There is a Docker file to build and run the example app in Docker container.
53
-
To build it run the build command from the project's root directory:
54
-
55
-
```
56
-
docker build . -t cxx-corrosion-cmake
57
-
```
58
-
59
-
Then you can run the example app by calling:
23
+
### Code examples to call Rust functions from C++:
24
+
-[Example of using a different primitive types](https://github.com/geekbrother/cxx-corrosion-cmake/blob/main/src/main.cpp) for arguments and returns;
25
+
- Example of using [Rust Result type](https://cxx.rs/binding/result.html#returning-result-from-rust-to-c) and [Anyhow](https://docs.rs/anyhow/latest/anyhow/) in return to C++;
26
+
-[Example of using panics](https://github.com/geekbrother/cxx-corrosion-cmake/blob/2a981b2ec34ee4d4ffe261b1be07691f74c31a04/src/main.cpp#L15) in Rust when calling from C++.
0 commit comments