Function export in bin crate

I want to create a binary crate on Windows and the result PE should export some functions (my purpose is that the PE can be used by LoadLibrary as a dll). I've tried with:

#[inline(never)]
#[no_mangle]
pub extern "C" fn hello() {
    println!("hello");
}

fn main() {
   ///
}

In the Cargo.toml, I've used:

crate-type = ["bin", "cdylib"]

But the function hello is not exported (I cannot found it in the export table of the PE).

Thanks for any help.

You can't export items from binaries. You need to export them from lib.rs, which will be used to create a DLL.

1 Like

Thank you, but in that case I would have two binaries, an executable and a dll.

Then the question then becomes how to set up the main entry point for a rust library,
there is a thread on the forum setting that up, appears to involve relinking an rlib into a pe while adding the entry point.

Not having a windows machine, I haven't managed yet to repeat this on linux an elf PIE executable where it should be possible as well...

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.