You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+38-8Lines changed: 38 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -33,19 +33,31 @@ fn main() {
33
33
34
34
And that's it! Running `cargo build` should take care of the rest and your Rust
35
35
application will now have the C files `foo.c` and `bar.c` compiled into a file
36
-
named libfoo.a. You can call the functions in Rust by declaring functions in
36
+
named `libfoo.a`. If the C files contain
37
+
38
+
```c
39
+
voidfoo_function(void) { ... }
40
+
```
41
+
42
+
and
43
+
44
+
```c
45
+
int32_t bar_function(int32_t x) { ... }
46
+
```
47
+
48
+
you can call them from Rust by declaring them in
37
49
your Rust code like so:
38
50
39
51
```rust,no_run
40
52
extern {
41
53
fn foo_function();
42
-
fn bar_function();
54
+
fn bar_function(x: i32) -> i32;
43
55
}
44
56
45
57
pub fn call() {
46
58
unsafe {
47
59
foo_function();
48
-
bar_function();
60
+
bar_function(42);
49
61
}
50
62
}
51
63
@@ -54,14 +66,16 @@ fn main() {
54
66
}
55
67
```
56
68
69
+
See [the Rustonomicon](https://doc.rust-lang.org/nomicon/ffi.html) for more details.
70
+
57
71
## External configuration via environment variables
58
72
59
73
To control the programs and flags used for building, the builder can set a
60
74
number of different environment variables.
61
75
62
76
*`CFLAGS` - a series of space separated flags passed to compilers. Note that
63
77
individual flags cannot currently contain spaces, so doing
64
-
something like: "-L=foo\ bar" is not possible.
78
+
something like: `-L=foo\ bar` is not possible.
65
79
*`CC` - the actual C compiler used. Note that this is used as an exact
66
80
executable name, so (for example) no extra flags can be passed inside
67
81
this variable, and the builder must ensure that there aren't any
@@ -70,6 +84,7 @@ number of different environment variables.
70
84
common is `-fPIC`).
71
85
*`AR` - the `ar` (archiver) executable to use to build the static library.
72
86
*`CRATE_CC_NO_DEFAULTS` - the default compiler flags may cause conflicts in some cross compiling scenarios. Setting this variable will disable the generation of default compiler flags.
87
+
*`CXX...` - see [C++ Support](#c-support).
73
88
74
89
Each of these variables can also be supplied with certain prefixes and suffixes,
75
90
in the following prioritized order:
@@ -144,10 +159,25 @@ fn main() {
144
159
}
145
160
```
146
161
147
-
When using C++ library compilation switch, the `CXX` and `CXXFLAGS` env
148
-
variables are used instead of `CC` and `CFLAGS` and the C++ standard library is
149
-
linked to the crate target.
150
-
Remember that C++ does name mangling so `extern "C"` might be required to enable rust linker to find your functions.
162
+
For C++ libraries, the `CXX` and `CXXFLAGS` environment variables are used instead of `CC` and `CFLAGS`.
163
+
164
+
The C++ standard library may be linked to the crate target. By default it's `libc++` for OS X, FreeBSD, and OpenBSD, `libc++_shared` for Android, nothing for MSVC, and `libstdc++` for anything else. It can be changed in one of two ways:
165
+
166
+
1. by using the `cpp_link_stdlib` method on `Build`:
167
+
```rust,no-run
168
+
fn main() {
169
+
cc::Build::new()
170
+
.cpp(true)
171
+
.file("foo.cpp")
172
+
.cpp_link_stdlib("stdc++") // use libstdc++
173
+
.compile("libfoo.a");
174
+
}
175
+
```
176
+
2. by setting the `CXXSTDLIB` environment variable.
177
+
178
+
In particular, for Android you may want to [use `c++_static` if you have at most one shared library](https://developer.android.com/ndk/guides/cpp-support).
179
+
180
+
Remember that C++ does name mangling so `extern "C"` might be required to enable Rust linker to find your functions.
0 commit comments