Add rust to go dynamic library example.
This commit is contained in:
10
rust-to-go-dynamic/Cargo.toml
Normal file
10
rust-to-go-dynamic/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "rust-to-go"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Andrey Voronkov <voronkovaa@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
libc = "0.2"
|
||||||
13
rust-to-go-dynamic/build.rs
Normal file
13
rust-to-go-dynamic/build.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!(r"cargo:rustc-link-search=target/debug");
|
||||||
|
let os = Command::new("uname")
|
||||||
|
.output().unwrap();
|
||||||
|
let ext = match String::from_utf8_lossy(os.stdout.as_slice()).into_owned().trim_end().as_ref() {
|
||||||
|
"Darwin" => "dylib",
|
||||||
|
_ => "so"
|
||||||
|
};
|
||||||
|
Command::new("go").args(&["build", "-o", &format!("target/debug/libdouble_input.{}", ext), "-buildmode=c-shared", "src/double.go"])
|
||||||
|
.status().unwrap();
|
||||||
|
}
|
||||||
10
rust-to-go-dynamic/src/double.go
Normal file
10
rust-to-go-dynamic/src/double.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
//export DoubleInput
|
||||||
|
func DoubleInput(input int32) int32 {
|
||||||
|
return input * 2
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {}
|
||||||
12
rust-to-go-dynamic/src/main.rs
Normal file
12
rust-to-go-dynamic/src/main.rs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
extern crate libc;
|
||||||
|
|
||||||
|
#[link(name = "double_input")]
|
||||||
|
extern {
|
||||||
|
fn DoubleInput(input: libc::c_int) -> libc::c_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let input = 2;
|
||||||
|
let output = unsafe { DoubleInput(input) };
|
||||||
|
println!("{} * 2 = {}", input, output);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user