Stream: beginners

Topic: Rust and Dynamic libraries


view this post on Zulip Luke Boswell (Dec 19 2023 at 02:11):

This is a bit of a strange question... but its not easy to find the relevant information I'm looking for. I'm doing some research and just wanting to know if I'm in the right direction. Am I thinking about this the right way? is it worth trying to build something like this or am I totally misunderstanding how roc, dynamic libraries and rust etc work.

// load the roc app
use libloading::{Library, library_filename, Symbol};
unsafe {

    // roc app built into a dylib using `roc build --lib color.roc`
    let lib = Library::new(library_filename("colors")).unwrap();

    // I expect the following signature
    // pub unsafe extern "C" fn roc__mainForHost_1_exposed_generic(*RocStr, *(u8,u8,u8,u8)) { ... etc }
    let func: Symbol<fn()> = lib.get(b"roc__mainForHost_1_exposed_generic").unwrap();

    // can I now simply call roc?? is it that easy
    let rgba : *(u8,u8,u8,u8) = func(&RocStr.from("Hello World"));

}

view this post on Zulip Brendan Hansknecht (Dec 19 2023 at 02:22):

Yeah, something along those line should work just fine. We do it for glue

view this post on Zulip Brendan Hansknecht (Dec 19 2023 at 02:27):

Probably need to put the right type in the symbol line though

view this post on Zulip Brendan Hansknecht (Dec 19 2023 at 02:27):

You labeled it as a function that takes no args and returns nothing

view this post on Zulip Luke Boswell (Dec 19 2023 at 02:41):

Alright then, I'll make a minimal demo to test it out

view this post on Zulip Brendan Hansknecht (Dec 19 2023 at 02:47):

Can see an example here: https://github.com/roc-lang/roc/blob/main/crates/glue/src/load.rs

view this post on Zulip Luke Boswell (Dec 19 2023 at 02:49):

Thank you for sharing that ❤


Last updated: Jul 06 2025 at 12:14 UTC