Rust引用自定義c/c++庫
剛入坑Rust,因為公司專案需求,需要將libjpeg-turbo移植到Rust中,在使用NDK完成交叉編譯後,我對著幾個庫檔案不知所措。國內Rust相關的文章太少,無奈只能到github和Stack Overflow上找答案。這篇文章的內容其實是Rust FFI章節的相關知識,在這裡做一下總結。
FFI
ffi指的是 foreign function interface(我理解為外部函式介面) 說白了就是rust呼叫c/c++和c/c++呼叫rust。不管是各類書籍和各類的教學文章裡都已經寫明瞭 他們改怎樣做,這裡我們也就不再囉嗦了。但是在編譯、構建方面,提到的內容比較少,大部分是 使用rustc命令做編譯連結(rustc -L /path/to/lib xxx.rs)。 涉及到cargo配置的很少很少。 複製程式碼
相關連結:ofollow,noindex">doc.rust-lang.org/cargo/refer…
在cargo book的Build Script裡的Outputs of the Build Script一節,教我們如何配置build.rs來達到一些目的。在我最後做了一些嘗試後,找到了在cargo管理的專案中,如何配置連結我們自定義的c/c++庫檔案
實踐
首先準備好我們需要連結的庫檔案
$ touch test.c 複製程式碼
內容如下
#include<stdio.h> void say_hello() { printf("Hello Rust!\n"); } 複製程式碼
非常簡單的一個函式,接下來我們把它編譯成.a的靜態庫
$ cc -c test.c -o test.o $ ar -r libtest.a test.o 複製程式碼
接下來我們使用cargo建立一個新的Rust工程
$ cargo new link_test --bin 複製程式碼
這裡給大家安利一下IDE,我目前在使用AndroidStudio + Rust外掛。InterliJ IDEA + Rust外掛應該也不錯,沒試過。但AndroidStudio就是基於它開發的。
編輯Cargo.toml 內容如下
[package] name = "link_test" version = "0.1.0" authors = ["authors"] edition = "2018" build = "src/build.rs" [build-dependencies] dunce = "0.1.1" 複製程式碼
在src目錄中建立build.rs (放在其他目錄下也可以需要在Cargo.toml中配置)
前方高能
build.rs內容如下:
extern crate dunce; use std::{env, path::PathBuf}; fn main() { let library_name = "test"; let root = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); let library_dir = dunce::canonicalize(root.join("src")).unwrap(); println!("cargo:rustc-link-lib=static={}", library_name); println!("cargo:rustc-link-search=native={}", env::join_paths(&[library_dir]).unwrap().to_str().unwrap()); } 複製程式碼
主要是這兩句:
println!("cargo:rustc-link-lib=static={}", library_name); println!("cargo:rustc-link-search=native={}",env::join_paths(&[library_dir]).unwrap().to_str().unwrap()); 複製程式碼
第一句是告訴cargo,配置rustc庫檔案的型別和名稱,型別這裡我們寫的是static因為用的是靜態庫還有dylib和framework可以選,但是使用dylib連線動態庫我一直沒有成功,有搞過的大佬希望可以指點一二(使用rustc --help命令可以檢視更多內容)。第二句是告訴cargo,配置rustc庫檔案所在的目錄
接下來把我們準備好的庫檔案丟到src目錄下,來試試看我們的配置有沒有效果,此時目錄結構如下:
|____Cargo.lock |____Cargo.toml |____src | |____build.rs | |____main.rs | |____libtest.a 複製程式碼
開啟我們的main.rs新增一下程式碼:
fn main() { unsafe { say_hello(); } } #[link(name = "test", kind = "static")] extern "C" { pub fn say_hello(); } 複製程式碼
最後
$ cargo run $ Hello Rust! 複製程式碼
可以看到,我們c語言裡定義的函式已經被執行了!