Last active
August 29, 2015 14:08
-
-
Save mboeh/e2552deb81e12d4ee3e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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