上QQ阅读APP看书,第一时间看更新
Windows with Build Script
A few steps will be required in order to make all of it work. Follow the guide!
- Download the mingw and msvc development libraries from http://www.libsdl.org/ (SDL2-devel-2.0.x-mingw.tar.gz and SDL2-devel-2.0.x-VC.zip).
- Unpack to folders of your choice. (You can delete it afterward.)
- Create the following folder structure in the same folder as your Cargo.toml:
gnu-mingw\dll\32 gnu-mingw\dll\64 gnu-mingw\lib\32 gnu-mingw\lib\64 msvc\dll\32 msvc\dll\64 msvc\lib\32 msvc\lib\64
- Copy the lib and dll files from the source archive to the directories we created in step 3 as follows:
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 SDL2-devel-2.0.5-VC.zip\SDL2-2.0.x\lib\x86\*.dll -> msvc\dll\32 SDL2-devel-2.0.5-VC.zip\SDL2-2.0.x\lib\x64\*.dll -> msvc\dll\64 SDL2-devel-2.0.5-VC.zip\SDL2-2.0.x\lib\x86\*.lib -> msvc\lib\32 SDL2-devel-2.0.5-VC.zip\SDL2-2.0.x\lib\x64\*.lib -> msvc\lib\64
- Create a Build Script. If you don't already have one, put this in your Cargo.toml file under [package]:
build = "build.rs"
- Create a file in the same directory as Cargo.toml called build.rs and write this into it:
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(); if target.contains("msvc") { lib_dir.push("msvc"); dll_dir.push("msvc"); } else { 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"); } } } } }
- On build, the Build Script will copy the needed DLLs into the same directory as your Cargo.toml file. You probably don't want to commit these to any Git repositories though, so add the following line to your .gitignore file:
/*.dll
- When you're shipping your game, make sure that you copy the corresponding SDL2.dll to the same directory that your compiled exe is in; otherwise, the game won't launch.
And now your project should build and run on any Windows computer!