rust-lang / rust-lang/rust-bindgen

`.clang_arg("-x c++")` causes failure in `generate`

Open
#1,855 12 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

As reported by @virtualritz in #1852, using clang_arg("-x c++") causes bindgen's generate to return an Err result.

As a workaround, .clang_arg("-xc++") can be used.

From this build.rs,

use std::path::PathBuf;

fn main() {
    let bindings = bindgen::Builder::default()
        .header("wrapper.hpp")
        .clang_arg("-x c++")            // does not work
        .generate()
        .expect("Couldn't make bindings!");

    let out_path = PathBuf::from(std::env::var("OUT_DIR").unwrap());
    bindings.write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}

RUST_BACKTRACE=1 cargo build -vv shows :

error: language not recognized: ' c++', err: true
warning: '-x c++': 'linker' input unused [-Wunused-command-line-argument], err: false
thread 'main' panicked at 'Couldn't make bindings!: ()', build.rs:4:20
stack backtrace:
   0: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
   1: core::fmt::write
   2: std::io::Write::write_fmt
   3: std::panicking::default_hook::{{closure}}
   4: std::panicking::default_hook
   5: std::panicking::rust_panic_with_hook
   6: rust_begin_unwind
   7: core::panicking::panic_fmt
   8: core::result::unwrap_failed
   9: core::result::Result<T,E>::expect
  10: build_script_build::main
  11: std::rt::lang_start::{{closure}}
  12: std::rt::lang_start_internal
  13: std::rt::lang_start
  14: main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided build.rs reproduction and inspect the Builder::clang_arg("-x c++") path leading into generate. Run RUST_BACKTRACE=1 cargo build -vv and compare it with the documented -xc++ workaround. Done means the spaced language argument no longer returns an Err while generating bindings.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.