1
1
# Generating Bindings to C++
2
2
3
- ` bindgen ` can handle a surprising number of C++ features, but not all of
4
- them. When ` bindgen ` can't translate some C++ construct into Rust, it usually
5
- comes down to one of two things:
6
-
7
- 1 . Rust has no equivalent language feature
8
- 2 . C++ is * hard!*
9
-
10
- Notable C++ features that are unsupported or only partially supported, and for
11
- which ` bindgen ` * should* generate opaque blobs whenever it finds an occurrence
12
- of them in a type it is generating bindings for:
13
-
14
- * Template specialization
15
- * Partial template specialization
16
- * Traits templates
17
- * SFINAE
3
+ ` bindgen ` can handle a some C++ features, but not all of them. To set
4
+ expectations: ` bindgen ` will give you the type definitions and FFI declarations
5
+ you need to build an API to the C++ library, but using those types in Rust will
6
+ be nowhere near as nice as using them in C++. You will have to manually call
7
+ constructors, destructors, overloaded operators, etc yourself.
18
8
19
9
When passing in header files, the file will automatically be treated as C++ if
20
10
it ends in ` .hpp ` . If it doesn't, adding ` -x c++ ` clang args can be used to
@@ -23,10 +13,55 @@ as well.
23
13
24
14
You pretty much ** must** use [ whitelisting] ( ./whitelisting.html ) when working
25
15
with C++ to avoid pulling in all of the ` std::* ` types, many of which ` bindgen `
26
- cannot handle. Additionally, you may want to mark other types
27
- as [ opaque] ( ./opaque.html ) that ` bindgen ` stumbles on.
16
+ cannot handle. Additionally, you may want to mark other types as
17
+ [ opaque] ( ./opaque.html ) that ` bindgen ` stumbles on. It is recommended to mark
18
+ all of ` std::* ` opaque, and to whitelist only precisely the functions and types
19
+ you intend to use.
20
+
21
+ You should read up on the [ FAQs] ( ./faq.html ) as well.
22
+
23
+ ## Supported Features
24
+
25
+ * Inheritance (for the most part; there are
26
+ [ some outstanding bugs] ( https://github.com/rust-lang-nursery/rust-bindgen/issues/380 ) )
27
+
28
+ * Methods
29
+
30
+ * Bindings to constructors and destructors (but they aren't implicitly or
31
+ automatically invoked)
32
+
33
+ * Function and method overloading
34
+
35
+ * Templates * without* specialization. You should be able to access individual
36
+ fields of the class or struct.
37
+
38
+ ## Unsupported Features
39
+
40
+ When ` bindgen ` finds a type that is too difficult or impossible to translate
41
+ into Rust, it will automatically treat it as an opaque blob of bytes. The
42
+ philosophy is that
43
+
44
+ 1 . we should always get layout, size, and alignment correct, and
45
+
46
+ 2 . just because one type uses specialization, that shouldn't cause ` bindgen ` to
47
+ give up on everything else.
48
+
49
+ Without further ado, here are C++ features that ` bindgen ` does not support or
50
+ cannot translate into Rust:
51
+
52
+ * Inline functions and methods: see
53
+ [ "Why isn't ` bindgen ` generating bindings to inline functions?"] ( ./faq.html#why-isnt-bindgen-generating-bindings-to-inline-functions )
54
+
55
+ * Template functions, methods of template classes and structs. We don't know
56
+ which monomorphizations exist, and can't create new ones because we aren't a
57
+ C++ compiler.
58
+
59
+ * Anything related to template specialization:
60
+ * Partial template specialization
61
+ * Traits templates
62
+ * Specialization Failure Is Not An Error (SFINAE)
28
63
29
- Note that using ` bindgen ` with C++ isn't nearly as plug-and-play as using it
30
- with C is. Improvement is ongoing.
64
+ * Cross language inheritance, for example inheriting from a Rust struct in C++.
31
65
32
- You might want to read up on the [ FAQs] ( ./faq.html ) as well.
66
+ * Automatically calling copy and/or move constructors or destructors. Supporting
67
+ this isn't possible with Rust's move semantics.
0 commit comments