For some reason I need to translate a piece of C code to Rust with c2rust and then compile it to wasm. The Rust code is as follows:
extern "C" {
fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
fn time(__timer: *mut time_t) -> time_t;
fn ctime(__timer: *const time_t) -> *mut libc::c_char;
fn memcpy(
_: *mut libc::c_void,
_: *const libc::c_void,
_: libc::c_ulong,
) -> *mut libc::c_void;
fn strlen(_: *const libc::c_char) -> libc::c_ulong;
}
pub type __time_t = libc::c_long;
pub type time_t = __time_t;
unsafe fn main_0() -> libc::c_int {
let mut src: [libc::c_char; 14] = *::core::mem::transmute::<
&[u8; 14],
&mut [libc::c_char; 14],
>(b"Hello, world!\0");
let mut dest: [libc::c_char; 20] = [0; 20];
memcpy(
dest.as_mut_ptr() as *mut libc::c_void,
src.as_mut_ptr() as *const libc::c_void,
(strlen(src.as_mut_ptr())).wrapping_add(1 as libc::c_int as libc::c_ulong),
);
printf(b"src: %s\n\0" as *const u8 as *const libc::c_char, src.as_mut_ptr());
printf(b"dest: %s\n\0" as *const u8 as *const libc::c_char, dest.as_mut_ptr());
let mut cur_time: time_t = 0;
time(&mut cur_time);
printf(
b"Current local time: %s\0" as *const u8 as *const libc::c_char,
ctime(&mut cur_time),
);
return 0 as libc::c_int;
}
pub fn main() {
unsafe { ::std::process::exit(main_0() as i32) }
}
then I run:
cargo build --target=wasm32-wasi
Then the linker reported a function signature mismatch error:
= note: rust-lld: error: function signature mismatch: time
>>> defined as (i32) -> i32 in /home/ubuntu/demo/demo_rust/target/wasm32-wasi/debug/deps/main-975cb877ac86c0d4.3vi6reg7dgepzj1h.rcgu.o
>>> defined as (i32) -> i64 in /home/ubuntu/.rustup/toolchains/nightly-2022-08-08-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/self-contained/libc.a(time.o)
Is this caused by an inconsistency between wasm's libc and Rust's libc, is there a way to make it work?