Skip to content

Commit 2baf376

Browse files
committed
New commit
2 parents 02dde69 + cece42d commit 2baf376

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

README.MD

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
# Example of using [CXX](https://cxx.rs/) and [Corrosion](https://github.com/corrosion-rs/corrosion) in CMake project to call Rust functions from C++
22

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:
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:
44

55
### `cxx_corrosion.cmake` file:
6-
- Windows-related code is removed (Linux and MacOS are supported).
7-
- Get STEM function is removed in a favor of using one line `get_filename_component(_LIB_PATH_STEM ${lib_path} NAME)`.
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).
89
- Simplify by using a simple one CMakeLists file for the project.
910

1011
### Code examples to call Rust functions from C++:
1112
- [Example of using a different primitive types](https://github.com/geekbrother/cxx-corrosion-cmake/blob/main/src/main.cpp) for arguments and returns.
1213
- 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/).
1314
- [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++.
1419

1520
# Why?
1621

@@ -22,10 +27,6 @@ This repo can be used as a sandbox playground for testing simple ideas of the Ru
2227
- Linux or MacOS
2328
- CMake
2429
- Clang
25-
- Libiconv
26-
- Rust
27-
- [Corrosion](https://github.com/corrosion-rs/corrosion#installation)
28-
2930
## CMake
3031

3132
Run CMake and build commands in a project's root:
@@ -57,6 +58,43 @@ docker build . -t cxx-corrosion-cmake
5758

5859
Then you can run the example app by calling:
5960

61+
```
62+
docker run cxx-corrosion-cmake
63+
```
64+
- [Corrosion](https://github.com/corrosion-rs/corrosion#installation)
65+
# Todo
66+
## CMake
67+
- [ ] Make it vcpkg package
68+
- [ ] Make it nix package
69+
Run CMake and build commands in a project's root:
70+
71+
`cmake -B build . && make -C build -j4`.
72+
73+
Then you can run the example app by calling:
74+
75+
`build/cxx_cmake`.
76+
77+
## Nix
78+
79+
If you are using [Nix](https://nixos.org/download.html) as a development environment you can use a nix shell with all dependencies:
80+
81+
```
82+
nix-shell -p cmake -p clang -p rustup -p libiconv -p git --pure
83+
```
84+
85+
And then run build commands from CMake above.
86+
87+
## Docker
88+
89+
There is a Docker file to build and run the example app in Docker container.
90+
To build it run the build command from the project's root directory:
91+
92+
```
93+
docker build . -t cxx-corrosion-cmake
94+
```
95+
96+
Then you can run the example app by calling:
97+
6098
```
6199
docker run cxx-corrosion-cmake
62100
```

src/main.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,25 @@ int main()
3535
std::cout << "Testing a case of panic by running lib_cxxbridge_panicked_function:" << std::endl;
3636
std::cout << lib_cxxbridge_panicked_function();
3737

38+
return 0;
39+
// Return Rust Result Ok:
40+
std::cout << "A Rust `Result Ok` value given via generated cxxbridge for lib_cxxbridge_return_result_ok:\n"
41+
42+
// Return Rust Result Error:
43+
try
44+
{
45+
std::cout << "A Rust `Result Error` value given via generated cxxbridge for lib_cxxbridge_return_result_error:\n"
46+
<< lib_cxxbridge_return_result_error() << std::endl;
47+
}
48+
catch (rust::Error e)
49+
{
50+
std::cerr << "Got an error from Rust function `Result`:" << std::endl;
51+
std::cerr << e.what() << std::endl;
52+
}
53+
54+
// Panic in a function call:
55+
std::cout << "Testing a case of panic by running lib_cxxbridge_panicked_function:" << std::endl;
56+
std::cout << lib_cxxbridge_panicked_function();
57+
3858
return 0;
3959
}

0 commit comments

Comments
 (0)