16021
|
1 |
# Quick Start
|
|
2 |
|
|
3 |
You can add corrosion to your project via the `FetchContent` CMake module or one of the other methods
|
|
4 |
described in the [Setup chapter](setup_corrosion.md).
|
|
5 |
Afterwards you can import Rust targets defined in a `Cargo.toml` manifest file by using
|
|
6 |
`corrosion_import_crate`. This will add CMake targets with names matching the crate names defined
|
|
7 |
in the Cargo.toml manifest. These targets can then subsequently be used, e.g. to link the imported
|
|
8 |
target into a regular C/C++ target.
|
|
9 |
|
|
10 |
The example below shows how to add Corrosion to your project via `FetchContent`
|
|
11 |
and how to import a rust library and link it into a regular C/C++ CMake target.
|
|
12 |
|
|
13 |
```cmake
|
|
14 |
include(FetchContent)
|
|
15 |
|
|
16 |
FetchContent_Declare(
|
|
17 |
Corrosion
|
|
18 |
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
|
|
19 |
GIT_TAG v0.5 # Optionally specify a commit hash, version tag or branch here
|
|
20 |
)
|
|
21 |
# Set any global configuration variables such as `Rust_TOOLCHAIN` before this line!
|
|
22 |
FetchContent_MakeAvailable(Corrosion)
|
|
23 |
|
|
24 |
# Import targets defined in a package or workspace manifest `Cargo.toml` file
|
|
25 |
corrosion_import_crate(MANIFEST_PATH rust-lib/Cargo.toml)
|
|
26 |
|
|
27 |
add_executable(your_cool_cpp_bin main.cpp)
|
|
28 |
|
|
29 |
# In this example the the `Cargo.toml` file passed to `corrosion_import_crate` is assumed to have
|
|
30 |
# defined a static (`staticlib`) or shared (`cdylib`) rust library with the name "rust-lib".
|
|
31 |
# A target with the same name is now available in CMake and you can use it to link the rust library into
|
|
32 |
# your C/C++ CMake target(s).
|
|
33 |
target_link_libraries(your_cool_cpp_bin PUBLIC rust-lib)
|
|
34 |
```
|
|
35 |
|
16038
|
36 |
|
16021
|
37 |
Please see the [Usage chapter](usage.md) for a complete discussion of possible configuration options.
|