【问题标题】:How do I create Rust bindings for the postgresql backend with rust bindgen?如何使用 rust bindgen 为 postgresql 后端创建 Rust 绑定?
【发布时间】:2021-07-24 21:41:36
【问题描述】:

我制作了一个带有标准 lib.rs 的 Cargo 包,一个带有 bindgen = "0.59"[build-dependencies] 下的 Cargo.toml,一个带有 #include "rewrite/rewriteDefine.h" 的 wrapper.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.0 (53cb7b09b 22 p>

运行货物构建:

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 替换为另一个错误。

【问题讨论】:

    标签: c postgresql rust clang bindgen


    【解决方案1】:

    #include postgres.h 添加到您的 wrapper.h:

    postgres.h
     PostgreSQL server.c 文件的主要包含文件

     这应该是 PostgreSQL 后端模块包含的第一个文件。
     客户端代码应该包含 postgres_fe.h。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-24
      • 2021-03-22
      • 2021-09-15
      • 2020-05-12
      • 2020-09-09
      • 1970-01-01
      • 2021-09-19
      • 2019-05-31
      相关资源
      最近更新 更多