Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for cpp calling rust by including header #2174

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/ffi/cpp_calling_rust_with_header/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "main",
srcs = ["main.cpp"],
deps = ["//ffi/cpp_calling_rust_with_header/rust:librusty_for_cc"],
)
6 changes: 6 additions & 0 deletions examples/ffi/cpp_calling_rust_with_header/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "rusty.h"

int main() {
print_c_hello_rust();
return 0;
}
17 changes: 17 additions & 0 deletions examples/ffi/cpp_calling_rust_with_header/rust/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
load("@rules_cc//cc:defs.bzl", "cc_import")
load("@rules_rust//rust:defs.bzl", "rust_static_library")

rust_static_library(
name = "librusty",
srcs = ["librusty.rs"],
crate_name = "rusty",
visibility = ["//visibility:public"],
)

cc_import(
name = "librusty_for_cc",
hdrs = ["rusty.h"],
includes = [package_name()],
static_library = ":librusty",
visibility = ["//visibility:public"],
)
6 changes: 6 additions & 0 deletions examples/ffi/cpp_calling_rust_with_header/rust/librusty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! A simple hello world example that can be called from C

#[no_mangle]
pub fn print_c_hello_rust() {
println!("Hello Rust!");
}
6 changes: 6 additions & 0 deletions examples/ffi/cpp_calling_rust_with_header/rust/rusty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef SIMPLE_PRINTER_H
#define SIMPLE_PRINTER_H

extern "C" void print_c_hello_rust();

#endif