在构建依赖项时,您可以指定rust使用gcc
编译器,只要您已经为mingw
正确安装了rust.为了确保你的Rust 正确配置为mingw
-使用this thread.记住,默认情况下,Rust for windows将配置为MSVC,而不是mingw.
The following steps were originally mentioned in the official 100
完成后,需要一个构建脚本将库链接到依赖项.但首先,你需要图书馆.从官方网站libsdl website下载mingw
个特定的库
现在,您需要将这些文件按正确的顺序放入与cargo.toml
相同的文件夹中-
SDL2-devel-2.0.x-mingw.tar.gz\SDL2-2.0.x\i686-w64-mingw32\bin -> gnu-mingw\dll\32
SDL2-devel-2.0.x-mingw.tar.gz\SDL2-2.0.x\x86_64-w64-mingw32\bin -> gnu-mingw\dll\64
SDL2-devel-2.0.x-mingw.tar.gz\SDL2-2.0.x\i686-w64-mingw32\lib -> gnu-mingw\lib\32
SDL2-devel-2.0.x-mingw.tar.gz\SDL2-2.0.x\x86_64-w64-mingw32\lib -> gnu-mingw\lib\64
gnu-mingw
应该是与cargo.toml
位于同一目录中的文件夹
现在,您需要构建脚本本身,创建一个名为build.rs
的文件,并将其放入cargo.toml
中的[package]
build="build.rs"
有关构建脚本的更多信息,请参见here
这是 playbook -
use std::env;
use std::path::PathBuf;
fn main() {
let target = env::var("TARGET").unwrap();
if target.contains("pc-windows") {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let mut lib_dir = manifest_dir.clone();
let mut dll_dir = manifest_dir.clone();
lib_dir.push("gnu-mingw");
dll_dir.push("gnu-mingw");
lib_dir.push("lib");
dll_dir.push("dll");
if target.contains("x86_64") {
lib_dir.push("64");
dll_dir.push("64");
}
else {
lib_dir.push("32");
dll_dir.push("32");
}
println!("cargo:rustc-link-search=all={}", lib_dir.display());
for entry in std::fs::read_dir(dll_dir).expect("Can't read DLL dir") {
let entry_path = entry.expect("Invalid fs entry").path();
let file_name_result = entry_path.file_name();
let mut new_file_path = manifest_dir.clone();
if let Some(file_name) = file_name_result {
let file_name = file_name.to_str().unwrap();
if file_name.ends_with(".dll") {
new_file_path.push(file_name);
std::fs::copy(&entry_path, new_file_path.as_path()).expect("Can't copy from DLL dir");
}
}
}
}
}
Note:这故意省略了MSVC特定的内容.
现在在你的构建配置aka [build]
inside cargo.toml
中,你需要-
target="x86_64-pc-windows-gnu"
可用目标的列表可以在cargo build docs中找到
更多关于构建配置的信息可以在config docs页中找到
作为奖励,如果你想使用其他编译器(gcc
除外).你所要做的就是确保必要的库在同一个目录中,并将它们放在你的[target.TARGET_NAME]
中
linker = "path\\to\\c\\linker"
ar = "path\\to\\c\\ar"
用你 Select 的目标值替换TARGET_NAME
.
编辑:根据OP的要求,提供如何将CMake与rust结合的信息.
不过,将CMake与rust结合使用是可能的,编译和构建第三方依赖项几乎肯定需要一个定制的构建脚本,该脚本将能够替换依赖项自己的构建脚本.
为了举例说明,让我们使用CMake with rust创建一个定制的简单C静态库.
The following steps were originally mentioned in 100
首先,你需要一个C项目,除了.c
个文件,它不需要太多.现在,你应该把.c
个文件放在一个名为libfoo
的目录中(或者你的库可以被称为什么).现在你可以把这个libfoo
目录和你的rust
项目放在同一个目录中,或者放在任何你想放的地方,但是一定要记住路径.
继续,在.c
文件中放入一个简单的"hello world"程序-
#include <stdio.h>
void testcall(float value)
{
printf("Hello, world from C! Value passed: %f\n",value);
}
(Note:函数应该是not,因为我们正在构建一个静态库)
现在我们需要一个CMakelists.txt
在同一个目录中-
cmake_minimum_required(VERSION 3.0)
project(LibFoo C)
add_library(foo STATIC foo.c)
install(TARGETS foo DESTINATION .)
这是一个非常简单的脚本,尽管最后一行很重要——它确保库的目的地是.
——我们必须稍后从rust找到这个库.
所以现在,文件 struct 可能看起来像-
.
├── Cargo.lock
├── Cargo.toml
├── libfoo
│ ├── CMakeLists.txt
│ └── foo.c
└── src
└── main.rs
现在,对于rust部分,您需要一个构建脚本和项目的构建依赖项cmake
.
将构建脚本添加到cargo.toml
-
[package]
build="build.rs"
还有依赖性-
[build-dependencies]
cmake = "0.1.31"
现在在你的build.rs
中,你必须调用cmake
-
extern crate cmake;
use cmake::Config;
fn main()
{
let dst = Config::new("libfoo").build();
println!("cargo:rustc-link-search=native={}", dst.display());
println!("cargo:rustc-link-lib=static=foo");
}
.build()
部分很简单,但为什么会有println!
呢?
它们将必要的命令写入stdout
,这样cargo
就可以搜索库并链接它.This is where the name and destination of your c library comes into play
现在,您只需执行cargo run
,它将构建C库以及您的rust项目!
您还可以在详细模式(-vv
)下运行它,以查看C库构建的详细输出.
现在你所要做的就是从你的main.rs
个电话里给图书馆打电话-
#[link(name="foo", kind="static")]
extern {
// this is rustified prototype of the function from our C library
fn testcall(v: f32);
}
fn main() {
println!("Hello, world from Rust!");
// calling the function from foo library
unsafe {
testcall(3.14159);
};
}
不过,这篇博客的作者给extern函数留下了一个注释,这很简单-
注意,这个原型需要从C原型到Rust原型进行一些手动转换.对于在基元值类型上操作的简单函数来说,这很简单,但当涉及更复杂的数据类型时,可能更难编写.
这让我们回到SDL2 crate ,编译它所需的C库,将它们链接起来,然后构建 crate 本身,这当然需要很多修改——但我希望这为您指明了正确的方向.