diff --git a/go-to-rust-cgo-dynamic/Cargo.toml b/go-to-rust-cgo-dynamic/Cargo.toml new file mode 100644 index 0000000..7a6a11e --- /dev/null +++ b/go-to-rust-cgo-dynamic/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "go-to-rust" +version = "0.1.0" +authors = ["Voronkov Andrey "] + +[lib] +name = "double_input" +crate-type = ["dylib"] diff --git a/go-to-rust-cgo-dynamic/Makefile b/go-to-rust-cgo-dynamic/Makefile new file mode 100644 index 0000000..feb101d --- /dev/null +++ b/go-to-rust-cgo-dynamic/Makefile @@ -0,0 +1,14 @@ +ifeq ($(shell uname),Darwin) + EXT := dylib +else + EXT := so +endif + +all: target/debug/libdouble_input.$(EXT) + go run src/double.go + +target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml + cargo build + +clean: + rm -rf target diff --git a/go-to-rust-cgo-dynamic/src/double.go b/go-to-rust-cgo-dynamic/src/double.go new file mode 100644 index 0000000..bec8ab5 --- /dev/null +++ b/go-to-rust-cgo-dynamic/src/double.go @@ -0,0 +1,15 @@ +package main + +//#cgo LDFLAGS: -L${SRCDIR}/../target/debug -Wl,-rpath=${SRCDIR}/../target/debug -ldouble_input -ldl +//#include +//extern int32_t double_input(int32_t input); +import "C" +import ( + "fmt" +) + +func main() { + input := 2 + double := C.double_input(C.int32_t(input)) + fmt.Printf("%d * 2 = %d\n", input, double) +} diff --git a/go-to-rust-cgo-dynamic/src/lib.rs b/go-to-rust-cgo-dynamic/src/lib.rs new file mode 100644 index 0000000..54ac836 --- /dev/null +++ b/go-to-rust-cgo-dynamic/src/lib.rs @@ -0,0 +1,4 @@ +#[no_mangle] +pub extern "C" fn double_input(input: i32) -> i32 { + input * 2 +} diff --git a/go-to-rust-cgo-static/Cargo.toml b/go-to-rust-cgo-static/Cargo.toml new file mode 100644 index 0000000..ebf1d07 --- /dev/null +++ b/go-to-rust-cgo-static/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "go-to-rust" +version = "0.1.0" +authors = ["Voronkov Andrey "] + +[lib] +name = "double_input" +crate-type = ["staticlib"] diff --git a/go-to-rust-cgo-static/Makefile b/go-to-rust-cgo-static/Makefile new file mode 100644 index 0000000..1530b98 --- /dev/null +++ b/go-to-rust-cgo-static/Makefile @@ -0,0 +1,8 @@ +all: target/debug/libdouble_input.a + go run src/double.go + +target/debug/libdouble_input.a: src/lib.rs Cargo.toml + cargo build + +clean: + rm -rf target diff --git a/go-to-rust-cgo-static/src/double.go b/go-to-rust-cgo-static/src/double.go new file mode 100644 index 0000000..c545ace --- /dev/null +++ b/go-to-rust-cgo-static/src/double.go @@ -0,0 +1,15 @@ +package main + +//#cgo LDFLAGS: -L${SRCDIR}/../target/debug -ldouble_input -ldl +//#include +//extern int32_t double_input(int32_t input); +import "C" +import ( + "fmt" +) + +func main() { + input := 2 + double := C.double_input(C.int32_t(input)) + fmt.Printf("%d * 2 = %d\n", input, double) +} diff --git a/go-to-rust-cgo-static/src/lib.rs b/go-to-rust-cgo-static/src/lib.rs new file mode 100644 index 0000000..54ac836 --- /dev/null +++ b/go-to-rust-cgo-static/src/lib.rs @@ -0,0 +1,4 @@ +#[no_mangle] +pub extern "C" fn double_input(input: i32) -> i32 { + input * 2 +}