mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-22 03:23:05 +00:00
parent
525c3b6c78
commit
b8440351c4
2 changed files with 64 additions and 2 deletions
|
@ -9,6 +9,7 @@ See crates.io's [documentation on the matter][build-script-docs] for more inform
|
|||
| Recipe | Crates | Categories |
|
||||
|--------|--------|------------|
|
||||
| [Compile and link statically to a bundled C library][ex-cc-static-bundled] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
|
||||
| [Compile and link statically to a bundled C++ library][ex-cc-static-bundled-cpp] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
|
||||
|
||||
|
||||
[ex-cc-static-bundled]: #ex-cc-static-bundled
|
||||
|
@ -110,6 +111,66 @@ fn run() -> Result<()> {
|
|||
```
|
||||
|
||||
|
||||
|
||||
[ex-cc-static-bundled-cpp]: #ex-cc-static-bundled-cpp
|
||||
<a name="ex-cc-static-bundled-cpp"></a>
|
||||
## Compile and link statically to a bundled C++ library
|
||||
|
||||
[![cc-badge]][cc] [![cat-development-tools-badge]][cat-development-tools]
|
||||
|
||||
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.
|
||||
|
||||
|
||||
### `Cargo.toml`
|
||||
|
||||
```toml
|
||||
[package]
|
||||
...
|
||||
build = "build.rs"
|
||||
|
||||
[build-dependencies]
|
||||
cc = "1"
|
||||
```
|
||||
|
||||
### `build.rs`
|
||||
|
||||
```rust,no_run
|
||||
extern crate cc;
|
||||
|
||||
fn main() {
|
||||
cc::Build::new()
|
||||
.cpp(true)
|
||||
.file("src/foo.cpp")
|
||||
.compile("foo");
|
||||
}
|
||||
```
|
||||
|
||||
### `src/foo.cpp`
|
||||
|
||||
```cpp
|
||||
extern "C" {
|
||||
int multiply(int x, int y);
|
||||
}
|
||||
|
||||
int multiply() {
|
||||
return x*y;
|
||||
}
|
||||
```
|
||||
|
||||
### `src/main.rs`
|
||||
|
||||
```rust,ignore
|
||||
extern {
|
||||
fn multiply(x : i32, y : i32);
|
||||
}
|
||||
|
||||
fn main(){
|
||||
unsafe {
|
||||
println!("{}", multiply(5,7));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
{{#include links.md}}
|
||||
|
||||
<!-- Other Reference -->
|
||||
|
@ -120,4 +181,4 @@ fn run() -> Result<()> {
|
|||
[cc-build-include]: https://docs.rs/cc/*/cc/struct.Build.html#method.include
|
||||
[cc-build-flag]: https://docs.rs/cc/*/cc/struct.Build.html#method.flag
|
||||
[cc-build-compile]: https://docs.rs/cc/*/cc/struct.Build.html#method.compile
|
||||
|
||||
[cc-build-cpp]: https://docs.rs/cc/*/cc/struct.Build.html#method.cpp
|
|
@ -131,7 +131,7 @@ community. It needs and welcomes help. For details see
|
|||
| Recipe | Crates | Categories |
|
||||
|--------|--------|------------|
|
||||
| [Compile and link statically to a bundled C library][ex-cc-static-bundled] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
|
||||
|
||||
| [Compile and link statically to a bundled C++ library][ex-cc-static-bundled-cpp] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
|
||||
|
||||
{{#include links.md}}
|
||||
|
||||
|
@ -142,6 +142,7 @@ community. It needs and welcomes help. For details see
|
|||
[ex-bitflags]: basics.html#ex-bitflags
|
||||
[ex-byteorder-le]: basics.html#ex-byteorder-le
|
||||
[ex-cc-static-bundled]: build_tools.html#ex-cc-static-bundled
|
||||
[ex-cc-static-bundled-cpp]: build_tools.html#ex-cc-static-bundled-cpp
|
||||
[ex-check-broken-links]: net.html#ex-check-broken-links
|
||||
[ex-check-cpu-cores]: basics.html#ex-check-cpu-cores
|
||||
[ex-clap-basic]: app.html#ex-clap-basic
|
||||
|
|
Loading…
Reference in a new issue