-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows linking Rust sources in C/C++ programs as static object files, which means the Rust standard libraries/crates are not statically linked in the program, saving a huge amount of installation space. The caller is responsible for providing linkage to the standard library and/or any needed external crate, and for providing allocators stubs if needed.
- Loading branch information
Showing
7 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
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,23 @@ | ||
## Rust object crates | ||
|
||
In order to link Rust objects into C/C++ libraries/programs without static | ||
linking all crates/libraries used by the objects, the new 'object' type crate | ||
can be used. It will produce object files instead of libraries from the Rust | ||
sources. The caller is responsible for providing the linking parameters for | ||
any crate/library needed by the Rust objects. | ||
|
||
```meson | ||
libstd_rust = meson.get_compiler('c').find_library('std-abcdefgh') | ||
librust = static_library( | ||
'rust', | ||
'librust.rs', | ||
rust_crate_type : 'object', | ||
dependencies: libstd-rust | ||
) | ||
user = executable( | ||
'user, | ||
'user.c', | ||
link_with : librust) | ||
``` |
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
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
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
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,8 @@ | ||
use std::os::raw::c_int; | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn print_foo() -> c_int { | ||
let foo = "rust compiler is working"; | ||
println!("{}", foo); | ||
0 | ||
} |
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,22 @@ | ||
project('staticprog', 'c') | ||
|
||
rustc = find_program('rustc') | ||
add_languages('rust') | ||
|
||
# Requires a discoverable std library, most likely via pkg-config, | ||
# but there is no common standard so make it optional for now. | ||
libstd_rust = dependency('std-rust', required: false) | ||
|
||
librust = static_library( | ||
'rust', | ||
'librust.rs', | ||
dependencies: libstd_rust, | ||
rust_crate_type: 'object') | ||
|
||
if libstd_rust.found() | ||
e = executable('c-program', 'prog.c', | ||
link_with: librust, | ||
install : true | ||
) | ||
test('rusttest', e) | ||
endif |
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,8 @@ | ||
#include <stdio.h> | ||
|
||
int print_foo(void); | ||
|
||
int main(void) { | ||
print_foo(); | ||
return 0; | ||
} |