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

Explaining how to link mixed C/Rust binaries. #838

Merged
merged 1 commit into from
Nov 12, 2024
Merged
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
17 changes: 17 additions & 0 deletions src/linkage.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,23 @@ a statically linked binary on MSVC you would execute:
RUSTFLAGS='-C target-feature=+crt-static' cargo build --target x86_64-pc-windows-msvc
```

## Mixed Rust and foreign codebases

If you are mixing Rust with foreign code (e.g. C, C++) and wish to make a single
binary containing both types of code, you have two approaches for the final
binary link:

* Use `rustc`. Pass any non-Rust libraries using `-L <directory>` and `-l<library>`
rustc arguments, and/or `#[link]` directives in your Rust code. If you need to
link against `.o` files you can use `-Clink-arg=file.o`.
* Use your foreign linker. In this case, you first need to generate a Rust `staticlib`
target and pass that into your foreign linker invocation. If you need to link
multiple Rust subsystems, you will need to generate a _single_ `staticlib`
perhaps using lots of `extern crate` statements to include multiple Rust `rlib`s.
Multiple Rust `staticlib` files are likely to conflict.

Passing `rlib`s directly into your foreign linker is currently unsupported.

[`cfg` attribute `target_feature` option]: conditional-compilation.md#target_feature
[configuration option]: conditional-compilation.md
[procedural macros]: procedural-macros.md