rust/src/test/run-pass/native-fn-linkname.rs
Haitao Li 88f29aab27 Use attributes for native module ABI and link name
This patch changes how to specify ABI and link name of a native module.

Before:
  native "cdecl" mod llvm = "rustllvm" {...}

After:
  #[abi = "cdecl"]
  #[link_name = "rustllvm"]
  native mod llvm {...}

The old optional syntax for ABI and link name is no longer supported.

Fixes issue #547
2011-11-16 11:35:13 -08:00

23 lines
408 B
Rust

use std;
import std::vec;
import std::str;
#[link_name = ""]
#[abi = "cdecl"]
native mod libc {
#[link_name = "strlen"]
fn my_strlen(str: *u8) -> uint;
}
fn strlen(str: str) -> uint unsafe {
// C string is terminated with a zero
let bytes = str::bytes(str) + [0u8];
ret libc::my_strlen(vec::unsafe::to_ptr(bytes));
}
fn main() {
let len = strlen("Rust");
assert(len == 4u);
}