Skip to content

Commit f23b118

Browse files
author
bors-servo
authored
Auto merge of #1020 - fitzgen:faq, r=emilio
Create an FAQ section in the book I feel like we've had to anwer these questions multiple times... Good to be able to just point people to these canonical answers instead of repeating ourselves. r? @emilio
2 parents c1aef63 + 21f4837 commit f23b118

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
- [Preventing the Derivation of `Copy` and `Clone`](./nocopy.md)
2020
- [Generating Bindings to C++](./cpp.md)
2121
- [Using Unions](./using-unions.md)
22+
- [FAQ](./faq.md)

book/src/cpp.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ You pretty much **must** use [whitelisting](./whitelisting.html) when working
2525
with C++ to avoid pulling in all of the `std::*` types, many of which `bindgen`
2626
cannot handle. Additionally, you may want to mark other types
2727
as [opaque](./opaque.html) that `bindgen` stumbles on.
28+
29+
Note that using `bindgen` with C++ isn't nearly as plug-and-play as using it
30+
with C is. Improvement is ongoing.
31+
32+
You might want to read up on the [FAQs](./faq.html) as well.

book/src/faq.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
7+
- [Why isn't `bindgen` generating methods for this whitelisted class?](#why-isnt-bindgen-generating-methods-for-this-whitelisted-class)
8+
- [Why isn't `bindgen` generating bindings to inline functions?](#why-isnt-bindgen-generating-bindings-to-inline-functions)
9+
- [Does `bindgen` support the C++ Standard Template Library (STL)?](#does-bindgen-support-the-c-standard-template-library-stl)
10+
11+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
12+
13+
### Why isn't `bindgen` generating methods for this whitelisted class?
14+
15+
Are the methods `inline` methods, or defined inline in the class? For example:
16+
17+
```c++
18+
class Dooder {
19+
public:
20+
// Function defined inline in the class.
21+
int example_one() { return 1; }
22+
23+
// `inline` function whose definition is supplied later in the header, or in
24+
// another header.
25+
inline bool example_two();
26+
};
27+
28+
inline bool Dooder::example_two() {
29+
return true;
30+
}
31+
```
32+
33+
If so, see
34+
["Why isn't `bindgen` generating bindings to inline functions?"](#why-isnt-bindgen-generating-bindings-to-inline-functions)
35+
36+
If not, consider filing an issue!
37+
38+
### Why isn't `bindgen` generating bindings to inline functions?
39+
40+
These functions don't typically end up in object files or shared libraries with
41+
symbols that we can reliably link to, since they are instead inlined into each
42+
of their call sites. Therefore, we don't generate bindings to them, since that
43+
creates linking errors.
44+
45+
However, if you are compiling the C/C++ yourself (rather than using a system
46+
shared library, for example), then you can pass `-fkeep-inline-functions` or
47+
`-fno-inline-functions` to `gcc` or `clang`, and invoke `bindgen` with either
48+
the `bindgen::Builder::generate_inline_functions` method or the
49+
`--generate-inline-functions` flag.
50+
51+
Note that these functions and methods are usually marked inline for a reason:
52+
they tend to be hot. The above workaround makes them an out-of-line call, which
53+
might not provide acceptable performance.
54+
55+
### Does `bindgen` support the C++ Standard Template Library (STL)?
56+
57+
Sort of. A little. Depends what you mean by "support".
58+
59+
Most functions, methods, constructors, and destructors are inline in the
60+
STL. 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)
61+
62+
As far as generating opaque blobs of bytes with the correct size and alignment,
63+
`bindgen` can do pretty well. This is typically enough to let you use types that
64+
transitively contain STL things. We generally recommend marking `std::.*` as
65+
opaque, and then whitelisting only the specific things you need from the library
66+
you're binding to that is pulling in STL headers.

0 commit comments

Comments
 (0)