From c6e36fb4141886d8e5b8fb64cffd0e04e4c766aa Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 24 Jan 2021 23:10:46 +0800 Subject: [PATCH] feat: add rust link a --- __ffi/rust_link_a/.cargo/config.toml | 2 ++ __ffi/rust_link_a/Cargo.lock | 5 +++++ __ffi/rust_link_a/Cargo.toml | 9 +++++++++ __ffi/rust_link_a/README.md | 5 +++++ __ffi/rust_link_a/hello.c | 11 +++++++++++ __ffi/rust_link_a/hello.h | 2 ++ __ffi/rust_link_a/justfile | 21 +++++++++++++++++++++ __ffi/rust_link_a/src/hello.rs | 5 +++++ __ffi/rust_link_a/src/main.rs | 13 +++++++++++++ 9 files changed, 73 insertions(+) create mode 100644 __ffi/rust_link_a/.cargo/config.toml create mode 100644 __ffi/rust_link_a/Cargo.lock create mode 100644 __ffi/rust_link_a/Cargo.toml create mode 100644 __ffi/rust_link_a/README.md create mode 100644 __ffi/rust_link_a/hello.c create mode 100644 __ffi/rust_link_a/hello.h create mode 100644 __ffi/rust_link_a/justfile create mode 100644 __ffi/rust_link_a/src/hello.rs create mode 100644 __ffi/rust_link_a/src/main.rs diff --git a/__ffi/rust_link_a/.cargo/config.toml b/__ffi/rust_link_a/.cargo/config.toml new file mode 100644 index 0000000..dfcd7a8 --- /dev/null +++ b/__ffi/rust_link_a/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +rustflags = ["-C", "link-args=-L. -lhello"] diff --git a/__ffi/rust_link_a/Cargo.lock b/__ffi/rust_link_a/Cargo.lock new file mode 100644 index 0000000..1e1eee1 --- /dev/null +++ b/__ffi/rust_link_a/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rust_link_a" +version = "0.1.0" diff --git a/__ffi/rust_link_a/Cargo.toml b/__ffi/rust_link_a/Cargo.toml new file mode 100644 index 0000000..43f8fd9 --- /dev/null +++ b/__ffi/rust_link_a/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rust_link_a" +version = "0.1.0" +authors = ["Hatter Jiang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/__ffi/rust_link_a/README.md b/__ffi/rust_link_a/README.md new file mode 100644 index 0000000..d6c321c --- /dev/null +++ b/__ffi/rust_link_a/README.md @@ -0,0 +1,5 @@ + +```toml +[build] +rustflags = ["-C", "link-args=-Llib -l*LIBNAME* -std=c++11 -stdlib=libc++ -lc++ -lz"] +``` diff --git a/__ffi/rust_link_a/hello.c b/__ffi/rust_link_a/hello.c new file mode 100644 index 0000000..d9c3002 --- /dev/null +++ b/__ffi/rust_link_a/hello.c @@ -0,0 +1,11 @@ +#include +#include "hello.h" + +void print_line(const char* str) { + for (int i = 0; str[i] != 0; i++) { + char c = str[i]; + printf("PUT:%d - %c\n", c, c); + } + // str++; + printf("puts:%s\n", str); +} diff --git a/__ffi/rust_link_a/hello.h b/__ffi/rust_link_a/hello.h new file mode 100644 index 0000000..93b483a --- /dev/null +++ b/__ffi/rust_link_a/hello.h @@ -0,0 +1,2 @@ + +void print_line(const char* str); diff --git a/__ffi/rust_link_a/justfile b/__ffi/rust_link_a/justfile new file mode 100644 index 0000000..ad7fc97 --- /dev/null +++ b/__ffi/rust_link_a/justfile @@ -0,0 +1,21 @@ +_: + @just --list + +# generage .rs +create-rs: + bindgen hello.h -o src/hello.rs + +# create .a file +create-ar: + gcc -c -o hello.o hello.c + ar -cr libhello.a hello.o + +# re-run +re-run: + rm hello.o || true + rm libhello.a || true + rm -rf target/ || true + @just create-ar + @just create-rs + cargo r + diff --git a/__ffi/rust_link_a/src/hello.rs b/__ffi/rust_link_a/src/hello.rs new file mode 100644 index 0000000..3824ba3 --- /dev/null +++ b/__ffi/rust_link_a/src/hello.rs @@ -0,0 +1,5 @@ +/* automatically generated by rust-bindgen 0.56.0 */ + +extern "C" { + pub fn print_line(str_: *const ::std::os::raw::c_char); +} diff --git a/__ffi/rust_link_a/src/main.rs b/__ffi/rust_link_a/src/main.rs new file mode 100644 index 0000000..ad743a4 --- /dev/null +++ b/__ffi/rust_link_a/src/main.rs @@ -0,0 +1,13 @@ +mod hello; + +use std::ffi::CString; +use hello::print_line; + +fn main() { + let str = "Hello world!"; + let c_str = CString::new(str).unwrap(); + + unsafe { + print_line(c_str.as_ptr()); + } +}