Skip to content

Add c++ linking example #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion src/build_tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 -->
Expand All @@ -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
3 changes: 2 additions & 1 deletion src/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,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}}

Expand All @@ -140,6 +140,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
Expand Down