Skip to content

Commit 5110ba4

Browse files
authored
Merge pull request #838 from adetaylor/mixed_codebases
Explaining how to link mixed C/Rust binaries.
2 parents ab85b2d + 67b104d commit 5110ba4

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/linkage.md

+17
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,23 @@ a statically linked binary on MSVC you would execute:
252252
RUSTFLAGS='-C target-feature=+crt-static' cargo build --target x86_64-pc-windows-msvc
253253
```
254254

255+
## Mixed Rust and foreign codebases
256+
257+
If you are mixing Rust with foreign code (e.g. C, C++) and wish to make a single
258+
binary containing both types of code, you have two approaches for the final
259+
binary link:
260+
261+
* Use `rustc`. Pass any non-Rust libraries using `-L <directory>` and `-l<library>`
262+
rustc arguments, and/or `#[link]` directives in your Rust code. If you need to
263+
link against `.o` files you can use `-Clink-arg=file.o`.
264+
* Use your foreign linker. In this case, you first need to generate a Rust `staticlib`
265+
target and pass that into your foreign linker invocation. If you need to link
266+
multiple Rust subsystems, you will need to generate a _single_ `staticlib`
267+
perhaps using lots of `extern crate` statements to include multiple Rust `rlib`s.
268+
Multiple Rust `staticlib` files are likely to conflict.
269+
270+
Passing `rlib`s directly into your foreign linker is currently unsupported.
271+
255272
[`cfg` attribute `target_feature` option]: conditional-compilation.md#target_feature
256273
[configuration option]: conditional-compilation.md
257274
[procedural macros]: procedural-macros.md

0 commit comments

Comments
 (0)