-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #104549 - folkertdev:unstable-book-export-executable-…
…symbols, r=WaffleLapkin add -Zexport-executable-symbols to unstable book This flag has been extremely useful to me, but it's hard to discover. The text contains a bunch of terms that hopefully a search engine will pick up on when someone searches for this functionality.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/doc/unstable-book/src/compiler-flags/export-executable-symbols.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# `export-executable-symbols` | ||
|
||
The tracking issue for this feature is: [#84161](https://github.com/rust-lang/rust/issues/84161). | ||
|
||
------------------------ | ||
|
||
The `-Zexport-executable-symbols` compiler flag makes `rustc` export symbols from executables. The resulting binary is runnable, but can also be used as a dynamic library. This is useful for interoperating with programs written in other languages, in particular languages with a runtime like Java or Lua. | ||
|
||
For example on windows: | ||
```rust | ||
#[no_mangle] | ||
fn my_function() -> usize { | ||
return 42; | ||
} | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
} | ||
``` | ||
|
||
A standard `cargo build` will produce a `.exe` without an export directory. When the `export-executable-symbols` flag is added | ||
|
||
```Bash | ||
export RUSTFLAGS="-Zexport-executable-symbols" | ||
cargo build | ||
``` | ||
|
||
the binary has an export directory with the functions: | ||
|
||
```plain | ||
The Export Tables (interpreted .edata section contents) | ||
... | ||
[Ordinal/Name Pointer] Table | ||
[ 0] my_function | ||
[ 1] main | ||
``` | ||
(the output of `objdump -x` on the binary) | ||
|
||
Please note that the `#[no_mangle]` attribute is required. Without it, the symbol is not exported. | ||
|
||
The equivalent of this flag in C and C++ compilers is the `__declspec(dllexport)` annotation or the `-rdynamic` linker flag. |