|
|
|
|
<!doctype html><html lang=zh-CN class="sidebar-visible no-js light"><meta charset=UTF-8><title>构建时工具 - Rusty Book(锈书)</title><meta content="text/html; charset=utf-8" http-equiv=Content-Type><meta name=description content=""><meta name=viewport content="width=device-width,initial-scale=1"><meta name=theme-color content=#ffffff><link rel=icon href=../favicon.svg><link rel="shortcut icon" href=../favicon.png><link rel=stylesheet href=../css/variables.css><link rel=stylesheet href=../css/general.css><link rel=stylesheet href=../css/chrome.css><link rel=stylesheet href=../css/print.css media=print><link rel=stylesheet href=../FontAwesome/css/font-awesome.css><link rel=stylesheet href=../fonts/fonts.css><link rel=stylesheet href=../highlight.css><link rel=stylesheet href=../tomorrow-night.css><link rel=stylesheet href=../ayu-highlight.css><link rel=stylesheet href=../theme/style1.css><body><script>var path_to_root="../",default_theme=window.matchMedia("(prefers-color-scheme: dark)").matches?"navy":"light"</script><script>try{var theme=localStorage.getItem("mdbook-theme"),sidebar=localStorage.getItem("mdbook-sidebar");theme.startsWith('"')&&theme.endsWith('"')&&localStorage.setItem("mdbook-theme",theme.slice(1,theme.length-1)),sidebar.startsWith('"')&&sidebar.endsWith('"')&&localStorage.setItem("mdbook-sidebar",sidebar.slice(1,sidebar.length-1))}catch(e){}</script><script>var theme;try{theme=localStorage.getItem("mdbook-theme")}catch(e){}null==theme&&(theme=default_theme);var html=document.querySelector("html");html.classList.remove("no-js"),html.classList.remove("light"),html.classList.add(theme),html.classList.add("js")</script><script>var html=document.querySelector("html"),sidebar="hidden";if(document.body.clientWidth>=1080){try{sidebar=localStorage.getItem("mdbook-sidebar")}catch(e){}sidebar=sidebar||"visible"}html.classList.remove("sidebar-visible"),html.classList.add("sidebar-"+sidebar)</script><nav id=sidebar class=sidebar aria-label="Table of contents"><div class=sidebar-scrollbox><ol class=chapter><li class="chapter-item expanded affix"><a href=../about.html>Rusty Book</a><li class="chapter-item expanded affix"><li class=part-title>Awesome<li class=spacer><li class="chapter-item expanded"><a href=../daily-dev.html>日常开发常用库</a><li class="chapter-item expanded"><a href=../superstar.html>Rust 明星项目</a><li class="chapter-item expanded"><a href=../empowering-js.html>使用 Rust 增强 JS</a><li class="chapter-item expanded"><a href=../games.html>Rust开发的游戏</a><li class="chapter-item expanded"><a href=../gamedev.html>游戏引擎</a><li class="chapter-item expanded affix"><li class=part-title>Awesome + Cookbook<li class=spacer><li class="chapter-item expanded"><a href=../algos/awesome.html>实用算法</a><a class=toggle><div>❱</div></a><li><ol class=section><li class="chapter-item expanded"><a href=../algos/randomness.html>生成随机值</a><li class="chapter-item expanded"><a href=../algos/sorting.html>Vec 排序</a><li class="chapter-item expanded"><div>压缩算法</div><a class=toggle><div>❱</div></a><li><ol class=section><li class=chapter-item><a href=../algos/compression/tar.html>使用.tar包</a></ol><li class="chapter-item expanded"><div>密码学</div><a class=toggle><div>❱</div></a><li><ol class=section><li class=chapter-item><a href=../algos/cryptography/hashing.html>哈希</a><li class=chapter-item><a href=../algos/cryptography/encryption.html>加密</a></ol><li class="chapter-item expanded"><div>数学计算</div><a class=toggle><div>❱</div></a><li><ol class=section><li class=chapter-item><a href=../algos/math/linear-algebra.html>线性代数</a><li class=chapter-item><a href=../algos/math/trigonometry.html>三角函数</a><li class=chapter-item><a href=../algos/math/complex.html>复数</a><li class=chapter-item><a href=../algos/math/statistics.html>统计学</a><li class=chapter-item><a href=../algos/math/misc.html>杂项</a></ol></ol><li class="chapter-item expanded"><a href=../datastructures/awesome.html>数据结构</a><a class=toggle><div>❱</div></a><
|
|
|
|
|
...
|
|
|
|
|
build = "build.rs"
|
|
|
|
|
|
|
|
|
|
[build-dependencies]
|
|
|
|
|
cc = "1"
|
|
|
|
|
|
|
|
|
|
[dependencies]
|
|
|
|
|
error-chain = "0.11"
|
|
|
|
|
</code></pre><p><em>build.rs</em><pre><pre class=playground><code class="language-rust edition2021">fn main() {
|
|
|
|
|
cc::Build::new()
|
|
|
|
|
.file("src/hello.c")
|
|
|
|
|
.compile("hello"); // outputs `libhello.a`
|
|
|
|
|
}
|
|
|
|
|
</code></pre></pre><p><em>src/hello.c</em><pre><code class=language-C>#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void hello() {
|
|
|
|
|
printf("Hello from C!\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void greet(const char* name) {
|
|
|
|
|
printf("Hello, %s!\n", name);
|
|
|
|
|
}
|
|
|
|
|
</code></pre><p><em>src/main.rs</em><pre><pre class=playground><code class="language-rust edition2021">use error_chain::error_chain;
|
|
|
|
|
use std::ffi::CString;
|
|
|
|
|
use std::os::raw::c_char;
|
|
|
|
|
|
|
|
|
|
error_chain! {
|
|
|
|
|
foreign_links {
|
|
|
|
|
NulError(::std::ffi::NulError);
|
|
|
|
|
Io(::std::io::Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn prompt(s: &str) -> Result<String> {
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
print!("{}", s);
|
|
|
|
|
std::io::stdout().flush()?;
|
|
|
|
|
let mut input = String::new();
|
|
|
|
|
std::io::stdin().read_line(&mut input)?;
|
|
|
|
|
Ok(input.trim().to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern {
|
|
|
|
|
fn hello();
|
|
|
|
|
fn greet(name: *const c_char);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() -> Result<()> {
|
|
|
|
|
unsafe { hello() }
|
|
|
|
|
let name = prompt("What's your name? ")?;
|
|
|
|
|
let c_name = CString::new(name)?;
|
|
|
|
|
unsafe { greet(c_name.as_ptr()) }
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
</code></pre></pre><h3 id=编译并静态链接一个-c-库-1><a class=header href=#编译并静态链接一个-c-库-1>编译并静态链接一个 C++ 库</a></h3><p>链接到 C++ 库跟之前的方式非常相似。主要的区别在于链接到 C++ 库时,你需要通过构建方法 <a href=https://docs.rs/cc/*/cc/struct.Build.html#method.cpp>cpp(true)</a> 来指定一个 C++ 编译器,然后在 C++ 的代码顶部添加 <code>extern "C"</code> 来阻止 C++ 编译器对库名进行名称重整( name mangling )。<p><em>Cargo.toml</em><pre><code class=language-toml>[package]
|
|
|
|
|
...
|
|
|
|
|
build = "build.rs"
|
|
|
|
|
|
|
|
|
|
[build-dependencies]
|
|
|
|
|
cc = "1"
|
|
|
|
|
</code></pre><p><em>build.rs</em><pre><pre class=playground><code class="language-rust edition2021">fn main() {
|
|
|
|
|
cc::Build::new()
|
|
|
|
|
.cpp(true)
|
|
|
|
|
.file("src/foo.cpp")
|
|
|
|
|
.compile("foo");
|
|
|
|
|
}
|
|
|
|
|
</code></pre></pre><p><em>src/foo.cpp</em><pre><code class=language-c++>extern "C" {
|
|
|
|
|
int multiply(int x, int y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int multiply(int x, int y) {
|
|
|
|
|
return x*y;
|
|
|
|
|
}
|
|
|
|
|
</code></pre><p><em>src/main.rs</em><pre><pre class=playground><code class="language-rust edition2021">extern {
|
|
|
|
|
fn multiply(x : i32, y : i32) -> i32;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main(){
|
|
|
|
|
unsafe {
|
|
|
|
|
println!("{}", multiply(5,7));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</code></pre></pre><h3 id=为-c-库创建自定义的-define><a class=header href=#为-c-库创建自定义的-define>为 C 库创建自定义的 define</a></h3><p><a href=https://docs.rs/cc/*/cc/struct.Build.html#method.define>cc::Build::define</a> 可以让我们使用自定义的 define 来构建 C 库。<p>以下示例在构建脚本 <code>build.rs</code> 中动态定义了一个 define,然后在运行时打印出 <strong>Welcome to foo - version 1.0.2</strong>。Cargo 会设置一些<a href=https://doc.rust-lang.org/cargo/reference/environment-variables.html>环境变量</a>,它们对于自定义的 define 会有所帮助。<p><em>Cargo.toml</em><pre><code class=language-toml>[package]
|
|
|
|
|
...
|
|
|
|
|
version = "1.0.2"
|
|
|
|
|
build = "build.rs"
|
|
|
|
|
|
|
|
|
|
[build-dependencies]
|
|
|
|
|
cc = "1"
|
|
|
|
|
</code></pre><p><em>build.rs</em><pre><pre class=playground><code class="language-rust edition2021">fn main() {
|
|
|
|
|
cc::Build::new()
|
|
|
|
|
.define("APP_NAME", "\"foo\"")
|
|
|
|
|
.define("VERSION", format!("\"{}\"", env!("CARGO_PKG_VERSION")).as_str())
|
|
|
|
|
.define("WELCOME", None)
|
|
|
|
|
.file("src/foo.c")
|
|
|
|
|
.compile("foo");
|
|
|
|
|
}
|
|
|
|
|
</code></pre></pre><p><em>src/foo.c</em><pre><code class=language-C>#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
void print_app_info() {
|
|
|
|
|
#ifdef WELCOME
|
|
|
|
|
printf("Welcome to ");
|
|
|
|
|
#endif
|
|
|
|
|
printf("%s - version %s\n", APP_NAME, VERSION);
|
|
|
|
|
}
|
|
|
|
|
</code></pre><p><em>src/main.rs</em><pre><pre class=playground><code class="language-rust edition2021">extern {
|
|
|
|
|
fn print_app_info();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main(){
|
|
|
|
|
unsafe {
|
|
|
|
|
print_app_info();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</code></pre></pre><div id=giscus-container></div></main><nav class=nav-wrapper aria-label="Page navigation"><a rel=prev href=../devtools/version.html class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts=Left><i class="fa fa-angle-left"></i> </a><a rel=next href=../encoding/strings.html class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts=Right><i class="fa fa-angle-right"></i></a><div style=clear:both></div></nav></div></div><nav class=nav-wide-wrapper aria-label="Page navigation"><a rel=prev href=../devtools/version.html class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts=Left><i class="fa fa-angle-left"></i> </a><a rel=next href=../encoding/strings.html class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts=Right><i class="fa fa-angle-right"></i></a></nav></div><script>window.playground_copyable=!0</script><script src=../ace.js charset=utf-8></script><script src=../editor.js charset=utf-8></script><script src=../mode-rust.js charset=utf-8></script><script src=../theme-dawn.js charset=utf-8></script><script src=../theme-tomorrow_night.js charset=utf-8></script><script src=../elasticlunr.min.js charset=utf-8></script><script src=../mark.min.js charset=utf-8></script><script src=../searcher.js charset=utf-8></script><script src=../clipboard.min.js charset=utf-8></script><script src=../highlight.js charset=utf-8></script><script src=../book.js charset=utf-8></script><script>var pagePath="devtools/build-tools.md"</script><script src=../assets/custom1.js></script><script src=../assets/bigPicture.js></script>
|