• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Frequently Asked Questions
2
3<!-- START doctoc generated TOC please keep comment here to allow auto update -->
4<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
5
6- [Why isn't `bindgen` generating methods for this allowlisted class?](#why-isnt-bindgen-generating-methods-for-this-allowlisted-class)
7- [Why isn't `bindgen` generating bindings to inline functions?](#why-isnt-bindgen-generating-bindings-to-inline-functions)
8- [Does `bindgen` support the C++ Standard Template Library (STL)?](#does-bindgen-support-the-c-standard-template-library-stl)
9- [How to deal with bindgen generated padding fields?](#how-to-deal-with-bindgen-generated-padding-fields)
10- [How to generate bindings for a custom target?](#how-to-generate-bindings-for-a-custom-target)
11
12<!-- END doctoc generated TOC please keep comment here to allow auto update -->
13
14### Why isn't `bindgen` generating methods for this allowlisted class?
15
16Are the methods `inline` methods, or defined inline in the class? For example:
17
18```c++
19class Dooder {
20  public:
21    // Function defined inline in the class.
22    int example_one() { return 1; }
23
24    // `inline` function whose definition is supplied later in the header, or in
25    // another header.
26    inline bool example_two();
27};
28
29inline bool Dooder::example_two() {
30    return true;
31}
32```
33
34If so, see
35["Why isn't `bindgen` generating bindings to inline functions?"](#why-isnt-bindgen-generating-bindings-to-inline-functions)
36
37If not, consider filing an issue!
38
39### Why isn't `bindgen` generating bindings to inline functions?
40
41These functions don't typically end up in object files or shared libraries with
42symbols that we can reliably link to, since they are instead inlined into each
43of their call sites. Therefore, we don't generate bindings to them, since that
44creates linking errors.
45
46However, if you are compiling the C/C++ yourself (rather than using a system
47shared library, for example), then you can pass `-fkeep-inline-functions` or
48`-fno-inline-functions` to `gcc` or `clang`, and invoke `bindgen` with either
49the `bindgen::Builder::generate_inline_functions` method or the
50`--generate-inline-functions` flag.
51
52Note that these functions and methods are usually marked inline for a reason:
53they tend to be hot. The above workaround makes them an out-of-line call, which
54might not provide acceptable performance.
55
56As an alternative, you can invoke `bindgen` with either the
57`bindgen::Builder::wrap_static_fns` method or the `--wrap-static-fns` flag.
58Which generates a C source file that can be compiled against the input headers
59to produce Rust headers for `static` and `static inline` functions. See [How to
60handle `static inline` functions](https://github.com/rust-lang/rust-bindgen/discussions/2405)
61for further information.
62
63### Does `bindgen` support the C++ Standard Template Library (STL)?
64
65Sort of. A little. Depends what you mean by "support".
66
67Most functions, methods, constructors, and destructors are inline in the
68STL. That ties our hands when it comes to linking: ["Why isn't `bindgen` generating bindings to inline functions?"](#why-isnt-bindgen-generating-bindings-to-inline-functions)
69
70As far as generating opaque blobs of bytes with the correct size and alignment,
71`bindgen` can do pretty well. This is typically enough to let you use types that
72transitively contain STL things. We generally recommend marking `std::.*` as
73opaque, and then allowlisting only the specific things you need from the library
74you're binding to that is pulling in STL headers.
75
76### How to deal with bindgen generated padding fields?
77
78Depending the architecture, toolchain versions and source struct, it is
79possible that bindgen will generate padding fields named `__bindgen_padding_N`.
80As these fields might be present when compiling for one architecture but not
81for an other, you should not initialize these fields manually when initializing
82the struct. Instead, use the `Default` trait. You can either enable this when
83constructing the `Builder` using the `derive_default` method, or you can
84implement this per struct using:
85
86```rust,ignore
87impl Default for SRC_DATA {
88    fn default() -> Self {
89        unsafe { std::mem::zeroed() }
90    }
91}
92```
93
94This makes it possible to initialize `SRC_DATA` by:
95
96```rust,ignore
97SRC_DATA {
98    field_a: "foo",
99    field_b: "bar",
100    ..Default::default()
101}
102```
103
104In the case bindgen generates a padding field, then this field will
105be automatically initialized by `..Default::default()`.
106
107### How to generate bindings for a custom target?
108
109To generate bindings for a custom target you only need to pass the `--target`
110argument to `libclang`. For example, if you want to generate bindings for the
111`armv7a-none-eabi` target using the command line, you need to invoke `bindgen`
112like so:
113```bash
114$ bindgen <input_headers> -- --target=armv7a-none-eabi
115```
116If you are using `bindgen` as a library, you should call
117`builder.clang_arg("--target=armv7a-none-eabi")` on your `builder`.
118