c++ - 无法使用bindgen进行llvm绑定(bind)

标签 c++ c binding rust llvm

我正在尝试完成Kaleidoscope Rust 教程。

但我似乎对 codegen 感到震惊。

我的build.rs:

extern crate bindgen;

use std::env;
use std::path::PathBuf;

fn main(){
    println!("cargo:rustc-link-lib=llvm");
    println!("cargo:rerun-if-changed=wrapper.h");

    let bindings = bindgen::Builder::default()
        .header("wrapper.h")
        .clang_arg("llvm-config --cxxflags --ldflags --system-libs --libs core")
        .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("bindings.rs"))
        .expect("couldn't write bindings!");
}

我的wrapper.h:

#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"

我希望能够传递教程中的以下标志 clang++ `llvm-config --cxxflags --ldflags --system-libs --libs core`

我收到的当前错误:

build-script-build` (exit code: 101)
--- stdout
cargo:rustc-link-lib=llvm
cargo:rerun-if-changed=wrapper.h

--- stderr
warning: llvm-config --cxxflags --ldflags --system-libs --libs core: 'linker' input unused [-Wunused-command-line-argument]
/usr/include/llvm/Support/Compiler.h:19:10: fatal error: 'new' file not found
warning: llvm-config --cxxflags --ldflags --system-libs --libs core: 'linker' input unused [-Wunused-command-line-argument], err: false
/usr/include/llvm/Support/Compiler.h:19:10: fatal error: 'new' file not found, err: true
thread 'main' panicked at 'unable to generate bindings: ()', src/libcore/result.rs:1188:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

最佳答案

您正在将llvm-config --cxxflags --ldflags --system-libs --libs core传递给clang,但您需要做的是执行llvm-config --cxxflags --ldflags --system-libs --libs core 并将结果参数传递给 clang (这就是反引号符号在 shell 中的作用)。

因此,在调用 bindgen 之前,请执行以下操作(未测试):

use std::{process::Command, str::from_utf8};

let llvm_config_out = Command::new("llvm-config")
    .args(&["--cxxflags", "--ldflags", "--system-libs", "--libs", "core"])
    .output()
    .expect("failed to execute llvm-config");

let llvm_clang_args = llvm_config_out
    .stdout
    .split(|byte| byte.is_ascii_whitespace())
    .map(|arg| str::from_utf8(arg).unwrap());

然后您可以将 llvm_clang_args 作为 .clang_args(llvm_clang_args) (不是 .clang_arg,注意额外的 s)。

关于c++ - 无法使用bindgen进行llvm绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60500516/

相关文章:

asp.net-mvc - 将 formValue 绑定(bind)到不同名称的属性,ASP.NET MVC

asp.net-mvc-3 - 我如何将整数列表添加到路由

asp.net - 通过 .aspx 页面上的 <% 进行绑定(bind)

c++ - OOP,使用类的链表

c++ - 将字符串转换为数字类型的通用方法?

c - __stdcall 和 __cdecl 调用约定的函数名称的前导下划线是可选的吗?

c - 将低字节从 int 复制到 char : Simpler to just do a byte load? 的指令

c++ - 为什么在平台A编译的静态库不能在没有警告的情况下在平台B上使用?

c++ - 如何为 Arduino IDE 安装 openCV 库?

C: const vs no const ..这是怎么编译的?