use tcc;
extern "C" fn say_hello_rust(who: *const ::std::os::raw::c_char) {
let who = unsafe { ::std::ffi::CStr::from_ptr(who) };
let who = who.to_str().unwrap();
println!("Hello {} from Rust!", who);
}
fn main() -> Result<(), ()> {
let say_hello_c = {
let mut tcc = tcc::Tcc::new();
tcc.set_output_type(tcc::OutputType::Memory)?;
tcc.compile_string(r#"
#include <tcclib.h>
void say_hello(const char *who) {
printf("Hello %s from C!\n", who);
}
"#)?;
match tcc.relocate() {
Ok(mut tcc) => tcc.get_symbol("say_hello")?,
Err(tcc) => panic!("Relocation failed for {:?}", tcc)
}
};
let mut tcc = tcc::Tcc::new();
tcc.set_error_func(Some(Box::new(|string| eprintln!("tcc: {}", string))));
tcc.set_output_type(tcc::OutputType::Memory)?;
tcc.compile_string(r#"
#include <tcclib.h>
void hello1(const char *who);
void hello2(const char *who);
int main(int argc, char *argv[]) {
const char *arg = (argc > 1) ? argv[1] : "world";
hello1(arg);
hello2(arg);
}
"#)?;
tcc.add_symbol("hello1", &say_hello_c)?;
tcc.add_symbol("hello2", &(say_hello_rust as *const ::std::os::raw::c_void))?;
tcc.run(&["argv[0]", "you"])?;
Ok(())
}