我正在尝试通过rust-bindgen绑定tbs.h
(基于TPM的服务)。
我有以下标题:
#ifndef TPM_WRAPPER_H
#define TPM_WRAPPER_H
#include <tbs.h>
#endif
build.rs
文件包含Windows SDK包含目录的路径:
use std::env;
use std::path::PathBuf;
fn main() {
println!("cargo:rustc-link-lib=tbs");
println!("cargo:rerun-if-changed=include/tpm_wrapper.h");
let bindings = bindgen::Builder::default()
.header("include/tpm_wrapper.h")
.clang_arg("-IC:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/shared")
.clang_arg("-v")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("tpm_bindings.rs"))
.expect("Couldn't write bindings!");
}
当我尝试通过cargo build
创建绑定并运行构建脚本时,出现以下错误:
C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/shared\tbs.h:50:9: error: unknown type name 'UINT8'
C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/shared\tbs.h:99:5: error: unknown type name '_In_'
C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/shared\tbs.h:99:31: error: expected ')'
C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/shared\tbs.h:98:20: note: to match this '('
是否缺少一些clang配置?
答案 0 :(得分:0)
由此产生的错误是windows-gnu toolchain的一部分。切换到stable-x86_64-pc-windows-msvc
就可以了。
rustup toolchain add stable-x86_64-pc-windows-msvc
rustup default stable-x86_64-pc-windows-msvc
stable-x86_64-pc-windows-msvc
需要Visual Studio安装程序提供的Windows构建工具,甚至可以独立使用。
更新
tbs.h
对于所有Windows定义的类型显然都需要windows.h
。
由于bindgen中的bug,有一种解决方法将某些functions列入黑名单