How can I access the global C variable / constant in Rust FFI?

I need to access the value of a constant exported from C to Rust.

I want to read a value from an actual character, not just a copy'n'paste value for Rust (in my case this value is a pointer and C is checking for pointer equality).

extern void *magic;

      

What is the syntax for reading magic: *const c_void

in Rust?

+3


source to share


1 answer


use std::os::raw::c_void;

extern "C" {
    #[no_mangle]
    static magic: *const c_void;
}

      

Optionally before extern

may be #[link(kind="static", name="<c library name>")]

to actually associate the symbol.



It is a little strange that the constant is declared as static

, but with the keyword const

it fails because "extern elements cannot be const

". ¯ \ _ (ツ) _ / ¯

+3


source







All Articles