Skip to content

Commit d55ce51

Browse files
committed
Explaining how to link mixed C/Rust binaries.
1 parent 04d5d5d commit d55ce51

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/linkage.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,23 @@ a statically linked binary on MSVC you would execute:
210210
RUSTFLAGS='-C target-feature=+crt-static' cargo build --target x86_64-pc-windows-msvc
211211
```
212212

213+
## Mixed Rust and foreign codebases
214+
215+
If you are mixing Rust with foreign code (e.g. C, C++) and wish to make a single
216+
binary containing both types of code, you have two approaches for the final
217+
binary link:
218+
219+
* Use `rustc`. Pass any non-Rust libraries using `-L <directory>` and `-l<library>`
220+
rustc arguments, and/or `#[link]` directives in your Rust code. If you need to
221+
link against `.o` files you can use `-Clink-arg=file.o`.
222+
* Use your foreign linker. In this case, you first need to generate a Rust `staticlib`
223+
target and pass that into your foreign linker invocation. If you need to link
224+
multiple Rust subsystems, you will need to generate a _single_ `staticlib`
225+
perhaps using lots of `extern crate` statements to include multiple Rust `rlib`s.
226+
Multiple Rust `staticlib` files are likely to conflict.
227+
228+
Passing `rlib`s directly into your foreign linker is currently unsupported.
229+
213230
[`cfg` attribute `target_feature` option]: conditional-compilation.md#target_feature
214231
[configuration option]: conditional-compilation.md
215232
[procedural macros]: procedural-macros.md

0 commit comments

Comments
 (0)