Add Rust to C example
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
Cargo.lock
|
||||||
|
target
|
||||||
11
rust-to-c/Cargo.toml
Normal file
11
rust-to-c/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
[package]
|
||||||
|
name = "rust-to-c"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Alex Crichton <alex@alexcrichton.com>"]
|
||||||
|
build = "build.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
libc = "0.1"
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
gcc = "0.3"
|
||||||
5
rust-to-c/build.rs
Normal file
5
rust-to-c/build.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
extern crate gcc;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
gcc::Config::new().file("src/double.c").compile("libdouble.a");
|
||||||
|
}
|
||||||
3
rust-to-c/src/double.c
Normal file
3
rust-to-c/src/double.c
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
int double_input(int input) {
|
||||||
|
return input * 2;
|
||||||
|
}
|
||||||
11
rust-to-c/src/main.rs
Normal file
11
rust-to-c/src/main.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
extern crate libc;
|
||||||
|
|
||||||
|
extern {
|
||||||
|
fn double_input(input: libc::c_int) -> libc::c_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let input = 4;
|
||||||
|
let output = unsafe { double_input(input) };
|
||||||
|
println!("{} * 2 = {}", input, output);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user