feat: add rust link a

This commit is contained in:
2021-01-24 23:10:46 +08:00
parent db388a6e37
commit c6e36fb414
9 changed files with 73 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
[build]
rustflags = ["-C", "link-args=-L. -lhello"]

5
__ffi/rust_link_a/Cargo.lock generated Normal file
View File

@@ -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"

View File

@@ -0,0 +1,9 @@
[package]
name = "rust_link_a"
version = "0.1.0"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@@ -0,0 +1,5 @@
```toml
[build]
rustflags = ["-C", "link-args=-Llib -l*LIBNAME* -std=c++11 -stdlib=libc++ -lc++ -lz"]
```

11
__ffi/rust_link_a/hello.c Normal file
View File

@@ -0,0 +1,11 @@
#include <stdio.h>
#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);
}

View File

@@ -0,0 +1,2 @@
void print_line(const char* str);

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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());
}
}