我正在尝试完成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/