我制作了一个带有标准 lib.rs 的 Cargo 包,一个带有 under 的 Cargo.toml,一个带有bindgen = "0.59"
under[build-dependencies]
的 wrapper.h#include "rewrite/rewriteDefine.h"
和一个 build.rs,如下所示:
extern crate bindgen;
use std::env;
use std::path::PathBuf;
fn main() {
// 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()
.clang_arg("-I../postgresql/src/include")
// 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!");
}
我在提交3779ac62d709467fe6331c8f0285d42e7487a01c和 clang 12.0.1、cargo 1.53.0(4369396ce 2021-04-27)和 rustc 1.53.02021 -06-17) 安装。
运行货物构建:
Compiling foo v0.1.0 (/home/manbearpig/Documents/foo)
error: failed to run custom build command for `foo v0.1.0 (/home/manbearpig/Documents/foo)`
Caused by:
process didn't exit successfully: `/home/manbearpig/Documents/foo/target/debug/build/foo-e5423fb71d62f5d9/build-script-build` (exit code: 101)
--- stdout
cargo:rerun-if-changed=wrapper.h
--- stderr
../postgresql/src/include/storage/block.h:31:9: error: unknown type name 'uint32'
../postgresql/src/include/storage/block.h:55:2: error: unknown type name 'uint16'
../postgresql/src/include/storage/block.h:56:2: error: unknown type name 'uint16'
../postgresql/src/include/storage/itemid.h:47:9: error: unknown type name 'uint16'
../postgresql/src/include/storage/itemid.h:48:9: error: unknown type name 'uint16'
../postgresql/src/include/storage/off.h:24:9: error: unknown type name 'uint16'
../postgresql/src/include/storage/itemptr.h:203:8: error: unknown type name 'bool'
../postgresql/src/include/storage/itemptr.h:204:8: error: unknown type name 'int32'
../postgresql/src/include/access/htup.h:64:2: error: unknown type name 'uint32'
../postgresql/src/include/access/htup.h:66:2: error: unknown type name 'Oid'
../postgresql/src/include/access/htup.h:81:8: error: unknown type name 'CommandId'
../postgresql/src/include/access/htup.h:82:8: error: unknown type name 'CommandId'
../postgresql/src/include/access/htup.h:84:12: error: unknown type name 'CommandId'
../postgresql/src/include/access/htup.h:84:29: error: unknown type name 'bool'
../postgresql/src/include/access/htup.h:87:8: error: unknown type name 'TransactionId'
../postgresql/src/include/nodes/bitmapset.h:44:9: error: unknown type name 'uint32'
../postgresql/src/include/nodes/bitmapset.h:45:9: error: unknown type name 'int32'
../postgresql/src/include/nodes/bitmapset.h:52:19: error: use of undeclared identifier 'FLEXIBLE_ARRAY_MEMBER'
../postgresql/src/include/nodes/bitmapset.h:79:8: error: unknown type name 'bool'
fatal error: too many errors emitted, stopping now [-ferror-limit=]
../postgresql/src/include/storage/block.h:31:9: error: unknown type name 'uint32', err: true
../postgresql/src/include/storage/block.h:55:2: error: unknown type name 'uint16', err: true
../postgresql/src/include/storage/block.h:56:2: error: unknown type name 'uint16', err: true
../postgresql/src/include/storage/itemid.h:47:9: error: unknown type name 'uint16', err: true
../postgresql/src/include/storage/itemid.h:48:9: error: unknown type name 'uint16', err: true
../postgresql/src/include/storage/off.h:24:9: error: unknown type name 'uint16', err: true
../postgresql/src/include/storage/itemptr.h:203:8: error: unknown type name 'bool', err: true
../postgresql/src/include/storage/itemptr.h:204:8: error: unknown type name 'int32', err: true
../postgresql/src/include/access/htup.h:64:2: error: unknown type name 'uint32', err: true
../postgresql/src/include/access/htup.h:66:2: error: unknown type name 'Oid', err: true
../postgresql/src/include/access/htup.h:81:8: error: unknown type name 'CommandId', err: true
../postgresql/src/include/access/htup.h:82:8: error: unknown type name 'CommandId', err: true
../postgresql/src/include/access/htup.h:84:12: error: unknown type name 'CommandId', err: true
../postgresql/src/include/access/htup.h:84:29: error: unknown type name 'bool', err: true
../postgresql/src/include/access/htup.h:87:8: error: unknown type name 'TransactionId', err: true
../postgresql/src/include/nodes/bitmapset.h:44:9: error: unknown type name 'uint32', err: true
../postgresql/src/include/nodes/bitmapset.h:45:9: error: unknown type name 'int32', err: true
../postgresql/src/include/nodes/bitmapset.h:52:19: error: use of undeclared identifier 'FLEXIBLE_ARRAY_MEMBER', err: true
../postgresql/src/include/nodes/bitmapset.h:79:8: error: unknown type name 'bool', err: true
fatal error: too many errors emitted, stopping now [-ferror-limit=], err: true
thread 'main' panicked at 'Unable to generate bindings: ()', build.rs:24:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
绝对路径:/home/manbearpig/Documents/postgresql/src/include
给出与相对路径相同的错误。
一些未知类型名称位于postgresql/src/include/c.h
. 我认为它们是使用来自postgresql/src/include/pg_config.h
.
在包含文件夹输出中运行bindgen c.h
,例如:pub type uint32 = ::std::os::raw::c_uint;
.
添加#include <stdbool.h>
到 wrapper.h 会替换../postgresql/src/include/storage/itemptr.h:203:8: error: unknown type name 'bool', err: true
为另一个错误。