diff --git a/rust-to-fortran/Cargo.toml b/rust-to-fortran/Cargo.toml new file mode 100644 index 0000000..4d923c8 --- /dev/null +++ b/rust-to-fortran/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "rust-to-fortran" +version = "0.1.0" +build = "build.rs" + +[dependencies] +libc = "0.2" + +[build-dependencies] +cc = "1.0" diff --git a/rust-to-fortran/build.rs b/rust-to-fortran/build.rs new file mode 100644 index 0000000..912f395 --- /dev/null +++ b/rust-to-fortran/build.rs @@ -0,0 +1,7 @@ +extern crate cc; + +fn main() { + cc::Build::new() + .file("src/double.f90") + .compile("libdouble.a"); +} diff --git a/rust-to-fortran/src/double.f90 b/rust-to-fortran/src/double.f90 new file mode 100644 index 0000000..6a5f562 --- /dev/null +++ b/rust-to-fortran/src/double.f90 @@ -0,0 +1,16 @@ +module wrapper + + implicit none + private + public double_input + +contains + + function double_input(input) + implicit none + integer :: input, double_input + + double_input = 2*input + end function + +end module wrapper diff --git a/rust-to-fortran/src/main.rs b/rust-to-fortran/src/main.rs new file mode 100644 index 0000000..616627f --- /dev/null +++ b/rust-to-fortran/src/main.rs @@ -0,0 +1,13 @@ +extern crate libc; + +// Consider using the `nm` utility on compiled Fortran files to get the function names + +extern { + fn __wrapper_MOD_double_input(input: &libc::c_int) -> libc::c_int; +} + +fn main() { + let input = 4; + let output = unsafe { __wrapper_MOD_double_input(&input) }; + println!("{} * 2 = {}", input, output); +}