Hi
I am trying to create a rust crate from mbedtls C library that I compiled separately for my Cortex M4. But I ran into issue for what I believe is no_std related. My steps are following.
-
I created a cargo lib project called mbedtls-sys with .toml below:
[package] name = "mbedtls-sys" version = "0.1.0" authors = ["feplooptest"] edition = "2018" build = "build.rs" [build-dependencies] bindgen = "*"
-
I created a binding using bindgen after looked at reference below:
A little C with your Rust - The Embedded Rust Book
Since I need this to be no_std, I used "--ctypes-prefix=cty" and "--use-core" flag for bindgen.
bindgen --ctypes-prefix=cty --use-core wrapper.h > src/lib.rs
-
Finally I created a build.rs under to build this crate by following bindgen user guide.
Create a build.rs File - The `bindgen` User Guide
And my build.rs is following:
extern crate bindgen; use std::env; use std::path::PathBuf; fn main() { println!("cargo:rustc-link-search=native=./mbedtls"); println!("cargo:rustc-link-lib=static=mbedtls"); println!("cargo:rustc-link-lib=static=mbedx509"); println!("cargo:rustc-link-lib=static=mbedcrypto"); // Tell cargo to invalidate the built crate whenever the wrapper changes println!("cargo:rerun-if-changed=wrapper.h"); // The bindgen::Builder is the main entry point // to bindgen, and lets you build up options for // the resulting bindings. let bindings = bindgen::Builder::default() // The input header we would like to generate // bindings for. .header("wrapper.h") // Tell cargo to invalidate the built crate whenever any of the // included header files changed. .parse_callbacks(Box::new(bindgen::CargoCallbacks)) // Finish the builder and generate the bindings. .generate() // Unwrap the Result and panic on failure. .expect("Unable to generate bindings"); // Write the bindings to the $OUT_DIR/bindings.rs file. let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); bindings .write_to_file(out_path.join("bindings.rs")) .expect("Couldn't write bindings!"); }
Now when I try to run cargo build it spits out a lot of error like this:
--> src/lib.rs:3180:43
|
3180 | pub fn mbedtls_rsa_self_test(verbose: cty::c_int) -> cty::c_int;
| ^^^ use of undeclared type or module `cty`
Does anyone know how to resolve this issue?
Thanks