I try using bindgen to generate FFI with a C++ library and get stuck on the following error:
# RUST_BACKTRACE=1 cargo build
Compiling try-opensim v0.1.0 (/home/nbrr/dev/bodysim/try-opensim)
error: failed to run custom build command for `try-opensim v0.1.0 (/home/nbrr/dev/bodysim/try-opensim)`
process didn't exit successfully: `/home/nbrr/dev/bodysim/try-opensim/target/debug/build/try-opensim-37f0c009d9c78e17/build-script-build` (exit code: 101)
--- stdout
cargo:rustc-link-lib=opensim
--- stderr
thread 'main' panicked at '"std___shared_count___shared_count<_Lp>" is not a valid Ident', /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/proc-macro2-0.4.24/src/stable.rs:563:9
stack backtrace:
0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
1: std::sys_common::backtrace::print
at libstd/sys_common/backtrace.rs:71
at libstd/sys_common/backtrace.rs:59
2: std::panicking::default_hook::{{closure}}
at libstd/panicking.rs:211
3: std::panicking::default_hook
at libstd/panicking.rs:227
4: std::panicking::rust_panic_with_hook
at libstd/panicking.rs:476
5: std::panicking::continue_panic_fmt
at libstd/panicking.rs:390
6: std::panicking::begin_panic_fmt
at libstd/panicking.rs:345
7: proc_macro2::stable::validate_term
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/proc-macro2-0.4.24/src/stable.rs:563
8: proc_macro2::stable::Ident::_new
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/proc-macro2-0.4.24/src/stable.rs:495
9: proc_macro2::stable::Ident::new
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/proc-macro2-0.4.24/src/stable.rs:505
10: proc_macro2::Ident::new
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/proc-macro2-0.4.24/src/lib.rs:821
11: bindgen::ir::context::BindgenContext::rust_ident_raw
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/ir/context.rs:937
12: bindgen::ir::context::BindgenContext::rust_ident
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/ir/context.rs:929
13: <bindgen::ir::function::Function as bindgen::codegen::CodeGenerator>::codegen
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/codegen/mod.rs:3424
14: <bindgen::ir::item::Item as bindgen::codegen::CodeGenerator>::codegen
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/codegen/mod.rs:373
15: <bindgen::ir::module::Module as bindgen::codegen::CodeGenerator>::codegen::{{closure}}
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/codegen/mod.rs:401
16: <bindgen::ir::module::Module as bindgen::codegen::CodeGenerator>::codegen
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/codegen/mod.rs:431
17: <bindgen::ir::item::Item as bindgen::codegen::CodeGenerator>::codegen
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/codegen/mod.rs:370
18: <bindgen::ir::module::Module as bindgen::codegen::CodeGenerator>::codegen::{{closure}}
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/codegen/mod.rs:401
19: <bindgen::ir::module::Module as bindgen::codegen::CodeGenerator>::codegen
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/codegen/mod.rs:431
20: <bindgen::ir::item::Item as bindgen::codegen::CodeGenerator>::codegen
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/codegen/mod.rs:370
21: bindgen::codegen::codegen::{{closure}}
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/codegen/mod.rs:3576
22: bindgen::ir::context::BindgenContext::gen
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/ir/context.rs:1239
23: bindgen::codegen::codegen
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/codegen/mod.rs:3554
24: bindgen::Bindings::generate
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/lib.rs:1790
25: bindgen::Builder::generate
at /home/nbrr/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.44.0/src/lib.rs:1216
26: build_script_build::main
at ./build.rs:9
27: std::rt::lang_start::{{closure}}
at libstd/rt.rs:74
28: std::panicking::try::do_call
at libstd/rt.rs:59
at libstd/panicking.rs:310
29: __rust_maybe_catch_panic
at libpanic_unwind/lib.rs:102
30: std::rt::lang_start_internal
at libstd/panicking.rs:289
at libstd/panic.rs:392
at libstd/rt.rs:58
31: std::rt::lang_start
at libstd/rt.rs:74
32: main
33: __libc_start_main
34: _start
My build.rs
file is as follows
extern crate bindgen;
use std::env;
use std::path::PathBuf;
fn main() {
println!("cargo:rustc-link-lib=opensim");
let bindings = bindgen::Builder::default()
.header("wrapper.hpp")
.clang_arg("-std=c++14")
.clang_arg("-Iopensim-core/Bindings")
.clang_arg("-Iopensim-core")
.clang_arg("-Iopensim-core/dependencies/simbody/SimTKcommon/include")
// many similar includes
.clang_arg("-Iopensim-core/dependencies/simbody/Simbody/Visualizer/include")
.clang_arg("-Iopensim-core/Vendors/lepton/include")
.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!");
}