Edit Note
Since Rust (ver.: 1.42) still doesn't have a stable ABI, it is recommended to use extern
(which is currently equivalent to extern "C"
(may change in the future)) Otherwise, it may be necessary to recompile the libraries.
This article explains how to change the calling convention.
The goal is to be able to functions with mangled names (which would allow the coexistence of functions from different modules/namespaces that have the same identifiers) inside the crate that uses the library.
I have noticed that Rust (ver.: 1.42) automatically assumes that function identifiers of the exported functions are not mangled.
At the moment I can successfully link it and use it when using #[no_mangle]
and #[export_name="..."]
on the functions.
I am using the stable-x86_64-windows-pc-msvc
toolchain.
Working example
rslib/lib.rs:
#[no_mangle] /* Exports it as "func" */
pub extern fn func() { ... }
app/main.rs:
#[link(name="rslib.dll", kind="dylib")]
extern { fn func(); }
fn main() {
unsafe { func(); }
}
This compiles and runs without a problem.
Not working example
rslib/lib.rs:
pub extern fn func() { ... }
Using the same "app/main.rs" causes the linking to fail with:
error LNK2019: unresolved external symbol __imp_func referenced in function _ZN8rust_app4main17h52189e261ef80b93E
.