给rust链接windows下的资源或者c/c++的.o文件

本文是用编译脚本(build.rs)的方式链接windows下的资源

1、原理

编译脚本的用法可以看下面这个官方的链接 
http://doc.crates.io/build-script.html

下面这个是官方的例子,是用gcc将.c文件生成.o,然后ar打包,最后cargo build链接

// build.rs

use std::process::Command;
use std::env;
use std::path::Path;

fn main() {
    let out_dir = env::var("OUT_DIR").unwrap();

    // note that there are a number of downsides to this approach, the comments
    // below detail how to improve the portability of these commands.
    Command::new("gcc").args(&["src/hello.c", "-c", "-fPIC", "-o"])
                       .arg(&format!("{}/hello.o", out_dir))
                       .status().unwrap();
    Command::new("ar").args(&["crus", "libhello.a", "hello.o"])
                      .current_dir(&Path::new(&out_dir))
                      .status().unwrap();

    println!("cargo:rustc-link-search=native={}", out_dir);
    println!("cargo:rustc-link-lib=static=hello");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

2、编译链接windows下的资源

这里需要用到mingw里的windres.exe和ar.exe 
下面是toml文件,需要把build=”build.rs”加进去:

[package]

name = "examples"
version = "0.0.1"
authors = [...]
build="build.rs"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

下面是build.rs里的代码,与toml文件放在同一个目录即可:

// build.rs

use std::process::Command;
use std::env;
use std::path::Path;

fn main() {
    let out_dir = env::var("OUT_DIR").ok().expect("can't find out_dir");

    // note that there are a number of downsides to this approach, the comments
    // below detail how to improve the portability of these commands.
    Command::new("windres").args(&["src/hello.rc",  "-o"])
                       .arg(&format!("{}/hello2.o", out_dir))
                       .status().unwrap();
    Command::new("ar").args(&["crus", "libhello.a", "hello2.o"])
                      .current_dir(&Path::new(&out_dir))
                      .status().unwrap();

    println!("cargo:rustc-link-search=native={}", out_dir);
    println!("cargo:rustc-link-lib=static=hello");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

build.rs流程:

  • 用windres.exe把src/hello.rc文件编译成hello2.o
  • 用ar工具把hello2.o打包成libhello.a
  • 最后两个println!是让cargo去out_dir用-l static=hello的方式链接libhello.a

设置好了toml,写好build.rs运行:cargo build即可