Skip to content

Commit

Permalink
Explaining how to link mixed C/Rust binaries.
Browse files Browse the repository at this point in the history
  • Loading branch information
adetaylor authored and traviscross committed Nov 12, 2024
1 parent ab85b2d commit 67b104d
Showing 1 changed file with 17 additions and 0 deletions.
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

0 comments on commit 67b104d

Please sign in to comment.