linking

Link C to Rust

Hello world

src/main.rs

#[link(name="hello", kind="static")]
extern {
  fn hello_hello () -> i32;
}

fn main () {
  unsafe {
    hello_hello();
  };
}

build.rs

extern crate gcc;

fn main() {
  gcc::Config::new()
    .file("src/hello.c")
    .include("src")
    .compile("libhello.a");
}

Cargo.toml

[package]
name = "hello-world"
links = "libhello"
build = "build.rs"
version = "1.0.0"

[build-dependencies]
gcc = "0.3"

src/hello.c

#include <stdio.h>

int hello_hello () {
  printf("Hello World\n");
  return 0;
}

See Also

results matching ""

    No results matching ""