3 ms·
Yes, it is possible to do it by just avoiding the runtime, e.g. the following works fine: #![crate_id="example_c"] #![crate_type="dylib"] #[no
by dbaupp 13y ago
Yes, it is possible to do it by just avoiding the runtime, e.g. the following works fine:
#![crate_id="example_c"]
#![crate_type="dylib"]
#[no_mangle]
pub extern fn my_rust_function(x: i32, y: i32) -> i32 {
x + y
}
Then compiling that gives a libexample_c....so file, which can be linked against the following C:
#include<stdio.h>
extern int my_rust_function(int, int);
int main() {
printf("%d", my_rust_function(1, 2));
return 0;
}
printing 3. (There's no interaction with a runtime here at all.)
In fact, it's even possible to "manually" start a runtime[1] inside another process (and if it's started on its own thread then I think it will work flawlessly, if not, then it might break in some corner cases).
[1]: http://static.rust-lang.org/doc/master/guide-runtime.html http://static.rust-lang.org/doc/master/guide-runtime.html