Foreign Function Interface (FFI)

Other topics

Calling libc function from nightly rust

The libc crate is 'feature gated' and can only be accessed on nightly Rust versions until it is considered stable.

#![feature(libc)]
extern crate libc;
use libc::pid_t;

#[link(name = "c")]
extern {
    fn getpid() -> pid_t;
}

fn main() {
    let x = unsafe { getpid() };
    println!("Process PID is {}", x);
}

Syntax:

  • #[link(name = "snappy")] // the foreign library to be linked to (optional)

    extern { ... } // list of function signatures in the foreign library

Contributors

Topic Id: 6140

Example Ids: 21345

This site is not affiliated with any of the contributors.