libmount.rs
c-langinfo
c-mounts
+
+rustlib.h
+main
--- /dev/null
+[package]
+name = "call-rust-from-c"
+version = "0.1.0"
+edition = "2018"
+
+[lib]
+crate-type = ['staticlib']
+
+[dependencies]
--- /dev/null
+RLIB = target/debug/libcall_rust_from_c.a
+
+main: src/main.o $(RLIB)
+ $(CC) -Wall -o $@ $^
+
+src/main.o: src/rustlib.h
+
+src/rustlib.h: src/lib.rs
+ cbindgen --lang c --output $@
+
+$(RLIB): src/lib.rs
+ cargo build
+
+run: main
+ ./main
+
+check:
+ cargo test
+
+.PHONY: run test
--- /dev/null
+use std::os::raw;
+
+/// Return The Answer.
+///
+/// # Example
+///
+/// ```
+/// let a = call_rust_from_c::answer();
+/// assert_eq!(a, 42);
+/// ```
+#[no_mangle]
+// C 'int'; it does not actually hurt to have more specific types such as int32_t, but it's good to know that plain ints work
+pub extern "C" fn answer() -> raw::c_int {
+ return 42;
+}
+
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_answer() {
+ assert_eq!(answer(), 42);
+ }
+}
--- /dev/null
+#include <stdio.h>
+#include "rustlib.h"
+
+int main()
+{
+ printf("The answer: %i\n", answer());
+ return 0;
+}