[Solved] Bindgen and libc


I’m trying to use bindgen to generate bindings to a C - library. When I run bindgen it includes a bunch of stuff from libc, like fopen, fclose, malloc, FILE, etc. Then these definitions conflict with the ones in libc, which I’d also like to use since it has much nicer types.

Is there a way to get bindgen to use libc, or at least filter the stuff in libc out of the bindgen output?


You can use white- & blacklisting and whitelist_recursively(false) to control which items bindgen emits. Note however that there are some bugs related to this functionality: https://github.com/rust-lang/rust-bindgen/issues/1454 https://github.com/rust-lang/rust-bindgen/issues/1455


Thanks, I tried the whitelisting and it was a big improvement. I’m running it from the command line, not a library. The second issue there is definitely part of the problem. I think I may just use a build.rs file and use bindgen as a library in the future.

Looking through the library documentation it looks like using a build.rs with whitelist_recursively and either include_str!(...) or raw_line(...) might be a better way to go (as mentioned in the whitelist_recursively documentation).

In general, the library documentation page on docs.rs is more thorough than running bindgen --help.