From 45be9bac5ad2dd5a7567014513923cd262960de0 Mon Sep 17 00:00:00 2001 From: thibault Date: Fri, 6 Oct 2017 05:27:13 +0900 Subject: [PATCH 1/2] Add c++ linking example --- src/build_tools.md | 63 +++++++++++++++++++++++++++++++++++++++++++++- src/intro.md | 3 ++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/build_tools.md b/src/build_tools.md index 4c7852d2..a3322c29 100644 --- a/src/build_tools.md +++ b/src/build_tools.md @@ -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 + +## 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 almost the same as linking it with a C library. The main difference is the [`cpp`][cc-build-cpp] option that should be put to `true` and the addition of `extern "C"` in the foo.cpp 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}} @@ -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 \ No newline at end of file diff --git a/src/intro.md b/src/intro.md index 41265735..eec089ad 100644 --- a/src/intro.md +++ b/src/intro.md @@ -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}} @@ -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 From f89d8887d4a4e91bc831e28857f9677b41c7430c Mon Sep 17 00:00:00 2001 From: thibault Date: Sat, 7 Oct 2017 21:13:03 +0900 Subject: [PATCH 2/2] Rewrote the explanation with j-haj suggestion --- src/build_tools.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/build_tools.md b/src/build_tools.md index a3322c29..cb28ba13 100644 --- a/src/build_tools.md +++ b/src/build_tools.md @@ -118,7 +118,7 @@ fn run() -> Result<()> { [![cc-badge]][cc] [![cat-development-tools-badge]][cat-development-tools] -Linking a bundled C++ library is almost the same as linking it with a C library. The main difference is the [`cpp`][cc-build-cpp] option that should be put to `true` and the addition of `extern "C"` in the foo.cpp file. +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`