Stream: general

Topic: Build with weak linkage "extern_weak"


view this post on Zulip Enrico Loparco (Oct 31 2024 at 09:32):

Hello people, I've been trying to use the weak linkage nightly feature ("extern_weak", https://github.com/rust-lang/rust/issues/29603)

#![feature(linkage)]
use std::os::raw::c_char;

extern "C" {
    #[linkage = "extern_weak"]
    static External_API: Option<unsafe extern "C" fn() -> u64>;
}

#[no_mangle]
#[allow(unsafe_code)]
pub extern "C" fn main_fun(_argv: *const *const c_char, _argc: i32) -> i32 {
    let val = unsafe { External_API.unwrap()() };
    println!("val = {val}");
    0
}

I'm building with cargo +nightly build --lib --target wasm32-wasi, but I'm getting a panic when I try to use the exposed API: called Option::unwrap() on a None value.
Does anyone know if the feature is supposed to compile correctly to wasm targets? Thanks

I'm experimenting, I know I can just do:

extern "C" {
    pub fn External_API() -> u64;
}

#[no_mangle]
#[allow(unsafe_code)]
pub extern "C" fn run_main(_argv: *const *const c_char, _argc: i32) -> i32 {
   let val = unsafe { External_API() };
   println!("val = {val}");
   0
}

and it works fine

Tracks stabilization for the linkage attribute.

Last updated: Nov 22 2024 at 16:03 UTC