Start off with some Rust source code
hw.rs :
#[link(name = "extern")]
extern {
fn hello();
}
fn hw_from_a_fn() {
println!("Hello World! (from a function)");
}
fn main() {
// The statements here will be executed when the compiled binary is called
// Print text to the console
println!("Hello World!");
hw_from_a_fn();
unsafe {
hello();
}
}
And some C++ code :
#include <stdio.h>
extern "C" {
void hello() {
printf("Hello, World!\n");
}
}
Compile the C++ code into a static archive:
$ g++ -c extern.cpp
$ ar rvs libextern.a extern.o
Now compile your Rust-to-C++ program
$ rustc -L . hw.rs
... and run your program ...
$ ./hw
Hello World!
Hello World! (from a function)
Hello, World!
$