How to ignore target when using bindgen inside of build.rs?

I am trying to make a no_std "-sys" crate using bindgen. I successfully followed the guide, the crate builds and passes tests, but when I try to use it in a project using a custom target (bare metal x86_64) I get the following error:

thread 'main' panicked at 'libclang error; possible causes include:
  - Invalid flag syntax
  - Unrecognized flags
  - Invalid flag arguments
  - File I/O errors
  - Host vs. target architecture mismatch

When I remove the build.rs and instead use the bindgen CLI to generate the Rust library file I can use the crate with the custom target without issue.

I've looked for a way to manually set the target for bindgen without luck, does anyone have any advice? Thanks:)

Edit: I've also tried the .clang_arg("--target x86_64-pc-linux-gnu") method on the bindgen Builder without any luck

If I'm reading this right, .clang_arg(...) will work to override the target triple bindgen uses with clang but I think it requires either --target=x86_64-pc-linux-gnu (note the extra =) or -target x86_64-pc-linux-gnu to actually trigger the override.

That said, bindgen should default to the the target cargo is building for when used from a build script; is this not x86_64-pc-linux-gnu in your case?

Thank you so much, adding the = fixed it!:))

That said, bindgen should default to the the target cargo is building for when used from a build script; is this not x86_64-pc-linux-gnu in your case?

The problem is that I don't want it to use the same target as cargo. I want to generate target-agnostic no_std bindings on the host machine, and then separately cross-compile those bindings with the rest of the project for the target machine. Since the cargo target is custom, I'm guessing there was no way for clang to find and read the JSON so was failing because the x86_64-none-none triple wasn't recognised.

1 Like

Bindgen will default to the target you are compiling for (determined by the --target arg given to cargo), not the host system that the build script will be running on.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.