010 Rust 网络编程,cargo 构建脚本介绍
说明
此章节并不是只有Rust网络编程的内容,但是由于后续的例子有些依赖于此知识点,所以放在Rust网络编程系列中做简单介绍。参考资料主要是官方文档《Cargo之书》。
介绍
在实际的项目中,有些包需要编译第三方非Rust代码,例如 C库;有些包需要链接到 C库,当然这些库既可以位于系统上,
也可以从源代码构建。其它的需求则有可能是需要构建代码生成 。
在Cargo中,提供了构建脚本,来满足这些需求。
指定的build命令应执行的Rust文件,将在包编译其它内容之前,被编译和调用,从而具备Rust代码所依赖的构建或生成的组件。Build通常被用来:
- 构建一个捆绑的C库;
- 在主机系统上找到C库;
- 生成Rust模块;
- 为crate执行所需的某平台特定配置。
示例一
功能:生成代码(Rust模块)。
修改Cargo.toml:
[package] ... build = "build.rs
在src同级目录下添加build.rs:
. ├── Cargo.toml ├── build.rs └── src └── main.rs
编写build.rs文件,如下:
use std::env; use std::fs::File; use std::io::Write; use std::path::Path; fn main() { let out_dir = env::var("OUT_DIR").unwrap(); let dest_path = Path::new(&out_dir).join("hello.rs"); let mut f = File::create(&dest_path).unwrap(); f.write_all(b" pub fn message() -> &'static str { \"Hello, World!\" } ").unwrap(); }
编写src/main.rs:
include!(concat!(env!("OUT_DIR"), "/hello.rs")); fn main() { println!("{}", message()); }
编译运行结果:
Hello, world!
示例二
功能:编译c代码,然后由Rust调用。
修改Cargo.toml,添加如下:
[package] # ---snip--- build = "build.rs # ---snip--- [build-dependencies] cc = "1.0
添加build.rs,编写如下:
extern crate cc; fn main() { cc::Build::new() .file("src/hello.c") .compile("hello"); }
在src目录下添加hello.c,编写如下:
#include <stdio.h> void hello() { printf("Hello, World!\n"); }
编写src/main.rs如下:
extern { fn hello(); } fn main() { unsafe { hello(); } }
编译执行:
打印:Hello, World!
本作品采用《CC 协议》,转载必须注明作者和本文链接