Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

book: document splitting a project into crates #763

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions book/src/concepts/build_systems.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,18 @@ When using QML with CXX-Qt [QML modules](https://doc.qt.io/qt-6/qtqml-writing-a-
This allows for attributes such as `#[qml_element]` to register the QObject with the QML type system without any C++ code.

See [`QmlModule` documentation](https://docs.rs/cxx-qt-build/latest/cxx_qt_build/struct.QmlModule.html) for more details.

## Splitting a project into multiple crates

As your project grows, it can be helpful to organize your code into multiple Rust crates. If your `main` function is
in Rust ([Cargo is your only build system](../getting-started/4-cargo-executable.md)), simply add the crates as
dependencies of the top level binary crate in its Cargo.toml file.

If your `main` function is in C++, you can only link one staticlib Rust crate into C++, otherwise linking
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't know if referring to the CXX section on linking between C++ and Rust is useful too? https://cxx.rs/build/other.html#linking-the-c-and-rust-together

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think linking that would be too much information which isn't very relevant for the audience. Though that was helpful to realize extern crate is more appropriate than pub use

would fail with duplicate symbol errors from multiple Rust runtimes. So, create one top level staticlib crate to link
into the C++ application. Specify your other crates as normal Rust library (rlib) dependencies
in the staticlib crate's Cargo.toml. You must reference the symbols of the Rust dependencies within the staticlib crate;
if you don't need those symbols in Rust code, you can add `extern crate crate_name;` statements in the staticlib's lib.rs file.
Refer to the [meta_project example](https://github.com/KDAB/cxx-qt/blob/main/examples/meta_project) for how to set this up.
Note that this requires Rust compiler features that were [stabilized](https://github.com/rust-lang/rust/pull/113301)
in Rust 1.74.