Merge pull request #31 from Antiarchitect/add-go-to-rust
Add go-to-rust via cgo. Dynamic and static.
This commit is contained in:
8
go-to-rust-cgo-dynamic/Cargo.toml
Normal file
8
go-to-rust-cgo-dynamic/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "go-to-rust"
|
||||
version = "0.1.0"
|
||||
authors = ["Voronkov Andrey <voronkovaa@gmail.com>"]
|
||||
|
||||
[lib]
|
||||
name = "double_input"
|
||||
crate-type = ["dylib"]
|
||||
14
go-to-rust-cgo-dynamic/Makefile
Normal file
14
go-to-rust-cgo-dynamic/Makefile
Normal file
@@ -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
|
||||
15
go-to-rust-cgo-dynamic/src/double.go
Normal file
15
go-to-rust-cgo-dynamic/src/double.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
//#cgo LDFLAGS: -L${SRCDIR}/../target/debug -Wl,-rpath=${SRCDIR}/../target/debug -ldouble_input -ldl
|
||||
//#include <stdint.h>
|
||||
//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)
|
||||
}
|
||||
4
go-to-rust-cgo-dynamic/src/lib.rs
Normal file
4
go-to-rust-cgo-dynamic/src/lib.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
#[no_mangle]
|
||||
pub extern "C" fn double_input(input: i32) -> i32 {
|
||||
input * 2
|
||||
}
|
||||
8
go-to-rust-cgo-static/Cargo.toml
Normal file
8
go-to-rust-cgo-static/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "go-to-rust"
|
||||
version = "0.1.0"
|
||||
authors = ["Voronkov Andrey <voronkovaa@gmail.com>"]
|
||||
|
||||
[lib]
|
||||
name = "double_input"
|
||||
crate-type = ["staticlib"]
|
||||
8
go-to-rust-cgo-static/Makefile
Normal file
8
go-to-rust-cgo-static/Makefile
Normal file
@@ -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
|
||||
15
go-to-rust-cgo-static/src/double.go
Normal file
15
go-to-rust-cgo-static/src/double.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
//#cgo LDFLAGS: -L${SRCDIR}/../target/debug -ldouble_input -ldl
|
||||
//#include <stdint.h>
|
||||
//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)
|
||||
}
|
||||
4
go-to-rust-cgo-static/src/lib.rs
Normal file
4
go-to-rust-cgo-static/src/lib.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
#[no_mangle]
|
||||
pub extern "C" fn double_input(input: i32) -> i32 {
|
||||
input * 2
|
||||
}
|
||||
Reference in New Issue
Block a user