diff --git a/cpp-to-rust/Cargo.toml b/cpp-to-rust/Cargo.toml new file mode 100644 index 0000000..49c42c7 --- /dev/null +++ b/cpp-to-rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "cpp-to-rust" +version = "0.1.0" +authors = ["timmonfette1 "] + +[lib] +name = "double_input" +crate-type = ["dylib"] diff --git a/cpp-to-rust/Makefile b/cpp-to-rust/Makefile new file mode 100644 index 0000000..ad22524 --- /dev/null +++ b/cpp-to-rust/Makefile @@ -0,0 +1,16 @@ +ifeq ($(shell uname),Darwin) + EXT := dylib +else + EXT := so +endif + +all: target/debug/libdouble_input.$(EXT) + g++ src/main.cpp -L ./target/debug/ -ldouble_input -o run + LD_LIBRARY_PATH=./target/debug/ ./run + +target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml + cargo build + +clean: + rm -rf target + rm -rf run diff --git a/cpp-to-rust/src/example.h b/cpp-to-rust/src/example.h new file mode 100644 index 0000000..78b6a46 --- /dev/null +++ b/cpp-to-rust/src/example.h @@ -0,0 +1,13 @@ +#ifndef _EXAMPLE_H +#define _EXAMPLE_H + +#ifdef __cplusplus +extern "C"{ +#endif + +int double_input(int input); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/cpp-to-rust/src/lib.rs b/cpp-to-rust/src/lib.rs new file mode 100644 index 0000000..bf573d1 --- /dev/null +++ b/cpp-to-rust/src/lib.rs @@ -0,0 +1,4 @@ +#[no_mangle] +pub extern fn double_input(input: i32) -> i32 { + input * 2 +} diff --git a/cpp-to-rust/src/main.cpp b/cpp-to-rust/src/main.cpp new file mode 100644 index 0000000..e4c5ced --- /dev/null +++ b/cpp-to-rust/src/main.cpp @@ -0,0 +1,12 @@ +#include +#include "example.h" + +using namespace std; + +int main() { + int input = 10; + int output = double_input(input); + cout<"] + +[lib] +name = "double_input" +crate-type = ["dylib"] diff --git a/julia-to-rust/Makefile b/julia-to-rust/Makefile new file mode 100644 index 0000000..cb8c7e7 --- /dev/null +++ b/julia-to-rust/Makefile @@ -0,0 +1,14 @@ +ifeq ($(shell uname),Darwin) + EXT := dylib +else + EXT := so +endif + +all: target/debug/libdouble_input.$(EXT) + julia src/main.jl + +target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml + cargo build + +clean: + rm -rf target diff --git a/julia-to-rust/src/lib.rs b/julia-to-rust/src/lib.rs new file mode 100644 index 0000000..bf573d1 --- /dev/null +++ b/julia-to-rust/src/lib.rs @@ -0,0 +1,4 @@ +#[no_mangle] +pub extern fn double_input(input: i32) -> i32 { + input * 2 +} diff --git a/julia-to-rust/src/main.jl b/julia-to-rust/src/main.jl new file mode 100644 index 0000000..f3e5d40 --- /dev/null +++ b/julia-to-rust/src/main.jl @@ -0,0 +1,7 @@ +input = Int32(10) +output = ccall((:double_input, "target/debug/libdouble_input"), + Int32, (Int32,), input) + +print(input) +print(" * 2 = ") +println(output)