Skip to content

Commit b844035

Browse files
thibaultbarbiebudziq
authored andcommitted
Add c++ linking example (#318)
Add c++ linking example
1 parent 525c3b6 commit b844035

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/build_tools.md

+62-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ See crates.io's [documentation on the matter][build-script-docs] for more inform
99
| Recipe | Crates | Categories |
1010
|--------|--------|------------|
1111
| [Compile and link statically to a bundled C library][ex-cc-static-bundled] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
12+
| [Compile and link statically to a bundled C++ library][ex-cc-static-bundled-cpp] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
1213

1314

1415
[ex-cc-static-bundled]: #ex-cc-static-bundled
@@ -110,6 +111,66 @@ fn run() -> Result<()> {
110111
```
111112

112113

114+
115+
[ex-cc-static-bundled-cpp]: #ex-cc-static-bundled-cpp
116+
<a name="ex-cc-static-bundled-cpp"></a>
117+
## Compile and link statically to a bundled C++ library
118+
119+
[![cc-badge]][cc] [![cat-development-tools-badge]][cat-development-tools]
120+
121+
Linking a bundled C++ library is very similar to linking a bundled C library. The two core differences when compiling and statically linking a bundled C++ library are specifying a C++ compiler via the builder method [`cpp(true)`][cc-build-cpp] and preventing name mangling by the C++ compiler by adding the `extern "C"` section at the top of our C++ source file.
122+
123+
124+
### `Cargo.toml`
125+
126+
```toml
127+
[package]
128+
...
129+
build = "build.rs"
130+
131+
[build-dependencies]
132+
cc = "1"
133+
```
134+
135+
### `build.rs`
136+
137+
```rust,no_run
138+
extern crate cc;
139+
140+
fn main() {
141+
cc::Build::new()
142+
.cpp(true)
143+
.file("src/foo.cpp")
144+
.compile("foo");
145+
}
146+
```
147+
148+
### `src/foo.cpp`
149+
150+
```cpp
151+
extern "C" {
152+
int multiply(int x, int y);
153+
}
154+
155+
int multiply() {
156+
return x*y;
157+
}
158+
```
159+
160+
### `src/main.rs`
161+
162+
```rust,ignore
163+
extern {
164+
fn multiply(x : i32, y : i32);
165+
}
166+
167+
fn main(){
168+
unsafe {
169+
println!("{}", multiply(5,7));
170+
}
171+
}
172+
```
173+
113174
{{#include links.md}}
114175

115176
<!-- Other Reference -->
@@ -120,4 +181,4 @@ fn run() -> Result<()> {
120181
[cc-build-include]: https://docs.rs/cc/*/cc/struct.Build.html#method.include
121182
[cc-build-flag]: https://docs.rs/cc/*/cc/struct.Build.html#method.flag
122183
[cc-build-compile]: https://docs.rs/cc/*/cc/struct.Build.html#method.compile
123-
184+
[cc-build-cpp]: https://docs.rs/cc/*/cc/struct.Build.html#method.cpp

src/intro.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ community. It needs and welcomes help. For details see
131131
| Recipe | Crates | Categories |
132132
|--------|--------|------------|
133133
| [Compile and link statically to a bundled C library][ex-cc-static-bundled] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
134-
134+
| [Compile and link statically to a bundled C++ library][ex-cc-static-bundled-cpp] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
135135

136136
{{#include links.md}}
137137

@@ -142,6 +142,7 @@ community. It needs and welcomes help. For details see
142142
[ex-bitflags]: basics.html#ex-bitflags
143143
[ex-byteorder-le]: basics.html#ex-byteorder-le
144144
[ex-cc-static-bundled]: build_tools.html#ex-cc-static-bundled
145+
[ex-cc-static-bundled-cpp]: build_tools.html#ex-cc-static-bundled-cpp
145146
[ex-check-broken-links]: net.html#ex-check-broken-links
146147
[ex-check-cpu-cores]: basics.html#ex-check-cpu-cores
147148
[ex-clap-basic]: app.html#ex-clap-basic

0 commit comments

Comments
 (0)