Skip to content

Commit

Permalink
Add example of c calling rust (#74)
Browse files Browse the repository at this point in the history
* Add example of c calling rust staticlib.

* Remove trailing newlines.

* Set visibility to private , run formatter.
  • Loading branch information
mfarrugi authored and acmcarther committed Mar 7, 2018
1 parent d20f528 commit cebb2a9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/ffi/c_calling_rust/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package(default_visibility = ["//visibility:private"])

load("@//rust:rust.bzl", "rust_library", "rust_test", "rust_binary")

rust_library(
name = "rusty",
srcs = ["lib.rs"],
crate_type = "staticlib",
)

# cc_* rules expect cc_* deps, so we need to wrap our rust staticlib.
cc_library(
name = "wrapper",
srcs = [":rusty"],
)

cc_test(
name = "main",
srcs = ["main.c"],
deps = [":wrapper"],
)
4 changes: 4 additions & 0 deletions examples/ffi/c_calling_rust/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[no_mangle]
pub extern fn my_favorite_number() -> i32 {
4
}
9 changes: 9 additions & 0 deletions examples/ffi/c_calling_rust/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <assert.h>
#include <stdint.h>

extern int32_t my_favorite_number();

int main(int argc, char** argv) {
assert(my_favorite_number() == 4);
return 0;
}

0 comments on commit cebb2a9

Please sign in to comment.