给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");
}

2、编译链接windows下的资源

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

[package]

name = "examples"
version = "0.0.1"
authors = [...]
build="build.rs"

下面是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");
}

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即可

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值