官术网_书友最值得收藏!

  • Rust Programming By Example
  • Guillaume Gomez Antoni Boucher
  • 330字
  • 2021-07-02 19:13:03

Windows with Build Script

A few steps will be required in order to make all of it work. Follow the guide!

  1. 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).
  2. Unpack to folders of your choice. (You can delete it afterward.)
  3. 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
  1. 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
  1. Create a Build Script. If you don't already have one, put this in your Cargo.toml file under [package]:
        build = "build.rs"
  1. 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"
); } } } } }
  1. 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
  1. 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!

主站蜘蛛池模板: 凭祥市| 呈贡县| 海原县| 陵水| 双城市| 稻城县| 尉犁县| 曲靖市| 谢通门县| 娱乐| 缙云县| 夹江县| 山西省| 原阳县| 永新县| 乌拉特后旗| 盐边县| 大新县| 广昌县| 和顺县| 东兰县| 临海市| 鹤峰县| 大安市| 丹寨县| 永修县| 襄垣县| 遂平县| 普陀区| 旬阳县| 溧水县| 十堰市| 云南省| 平顺县| 平度市| 尖扎县| 海淀区| 墨江| 遂川县| 余干县| 新化县|