From 77b32147ad13dbd5f33354604cce31834cc70868 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Fri, 2 Oct 2020 00:07:23 -0400 Subject: [PATCH] Add host.rs in hello-world example --- .../hello-world/platform/host/src/host.rs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 examples/hello-world/platform/host/src/host.rs diff --git a/examples/hello-world/platform/host/src/host.rs b/examples/hello-world/platform/host/src/host.rs new file mode 100644 index 0000000000..90fd7ced5f --- /dev/null +++ b/examples/hello-world/platform/host/src/host.rs @@ -0,0 +1,23 @@ +#![crate_type = "staticlib"] + +use std::ffi::CStr; +use std::os::raw::c_char; + +extern "C" { + #[link_name = "main_1"] + fn str_from_roc() -> *const c_char; +} + +// This magical #[export_name] annotation seems to be the only way to convince +// LLVM to emit this as the symbol "start" rather than "_start" on macOS. +// The linker will not recognize "_start" as the program entrypoint. +// +// See https://github.com/rust-lang/rust/issues/35052#issuecomment-235420755 +#[export_name = "\x01start"] +pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> isize { + let c_str = unsafe { CStr::from_ptr(str_from_roc()) }; + + println!("Roc says: \"{}\"", c_str.to_str().unwrap()); + + 0 +}