Skip to content

Instantly share code, notes, and snippets.

@mboeh
Last active August 29, 2015 14:08
Show Gist options
  • Save mboeh/e2552deb81e12d4ee3e5 to your computer and use it in GitHub Desktop.
Save mboeh/e2552deb81e12d4ee3e5 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
extern int32_t twice(int32_t, int32_t(*)(int32_t));
int32_t dbl(int32_t x) {
/* x = arbitrary number here */
return x+x;
}
int main() {
printf("%d\n", twice(10, dbl));
return 0;
}
#[no_mangle]
pub extern "C" fn twice(x: i32, f: extern "C" fn(i32) -> i32) -> i32 {
// x = 10 here
f(x) + f(x)
}
extern "C" fn dbl(x: i32) -> i32 { x+x }
#[allow(dead_code)]
fn main() {
println!("{}", twice(10, dbl));
}
all: 2wice 2wice-c
clean:
rm -f 2wice 2wice-c lib2wice.a
2wice: 2wice.rs
rustc $<
lib2wice.a: 2wice.rs
rustc --crate-type=staticlib $<
2wice-c: 2wice.c lib2wice.a
gcc -Wall 2wice.c lib2wice.a -ldl -lpthread -lgcc_s -lpthread -lc -lm -o 2wice-c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment