Rust-GDAL 是 Rust 语言的 GDAL(Geospatial Data Abstraction Library) 绑定库,用于处理地理数据。由于 GDAL 依赖较多,在 Windows 上的安装相对复杂,本文档将介绍如何安装 GDAL 并配置 Rust-GDAL 的开发环境。
1. 检查 Rust 版本
首先,确认当前 Windows 系统上安装的 Rust 版本及工具链类型。可以使用 rustup show
命令查询:
rustup show
如果输出如下:
stable-x86_64-pc-windows-gnu
stable-x86_64-pc-windows-msvc
表示你的 Rust 版本支持 GNU 工具链 和 MSVC 工具链。Rust 在 Windows 上有两个主要的工具链:
**stable-x86_64-pc-windows-gnu**
(GNU 工具链)- 使用 MinGW-w64(GNU 编译器集合 GCC) 进行编译。
- 需要 MinGW-w64 作为编译环境(可能需要手动安装)。
- 适用于开源生态的环境,不依赖 Microsoft 的工具链。
**stable-x86_64-pc-windows-msvc**
(MSVC 工具链)- 使用 Microsoft Visual C++(MSVC) 进行编译。
- 依赖 Visual Studio Build Tools(可通过
rustup
自动安装)。 - 适用于 Windows 原生环境,通常与 Windows API 兼容性更好,例如 DirectX、.NET。
2. stable-x86_64-pc-windows-gnu 用户安装步骤
如果你选择 GNU 工具链,需要安装 MSYS2 及相关工具。
2.1 安装 MSYS2
- 下载 MSYS2 安装程序:MSYS2 官网
或直接下载最新版(2024-12-08):
MSYS2 64-bit 安装程序 - 运行安装程序,需要 Windows 10 及以上的 64 位系统。
- 选择安装目录(推荐选择无空格、无特殊字符的路径,不要使用网络驱动器)。可以设置其他盘符。
- 完成安装后,点击 Finish。
- 启动 MSYS2 UCRT64 终端(MSYS2 提供多个终端,如
MINGW64
、CLANG64
,推荐使用UCRT64
)。
2.2 安装 MinGW-w64 GCC
安装 mingw-w64 GCC 以支持 Rust GNU 版本:使用如下命令:
pacman -S mingw-w64-ucrt-x86_64-gcc
系统会解析依赖,并提示如下:
resolving dependencies...
looking for conflicting packages...
Packages (15) mingw-w64-ucrt-x86_64-binutils-2.41-2
mingw-w64-ucrt-x86_64-crt-git-11.0.0.r216.gffe883434-1
mingw-w64-ucrt-x86_64-gcc-libs-13.2.0-2
mingw-w64-ucrt-x86_64-gmp-6.3.0-2
mingw-w64-ucrt-x86_64-headers-git-11.0.0.r216.gffe883434-1
mingw-w64-ucrt-x86_64-isl-0.26-1
mingw-w64-ucrt-x86_64-libiconv-1.17-3
mingw-w64-ucrt-x86_64-libwinpthread-git-11.0.0.r216.gffe883434-1
mingw-w64-ucrt-x86_64-mpc-1.3.1-2
mingw-w64-ucrt-x86_64-mpfr-4.2.1-2
mingw-w64-ucrt-x86_64-windows-default-manifest-6.4-4
mingw-w64-ucrt-x86_64-winpthreads-git-11.0.0.r216.gffe883434-1
mingw-w64-ucrt-x86_64-zlib-1.3-1
mingw-w64-ucrt-x86_64-zstd-1.5.5-1
mingw-w64-ucrt-x86_64-gcc-13.2.0-2
Total Download Size: 49.38 MiB
Total Installed Size: 418.82 MiB
:: Proceed with installation? [Y/n]
按 Y 确认安装,等待完成。
安装完成后,检查 GCC 版本:
gcc --version
输出示例:
gcc.exe (Rev2, Built by MSYS2 project) 13.2.0
2.3 更新 MSYS2
MSYS2 需要定期更新:
pacman -Syu
更新完成后,重新打开 UCRT64 终端。
2.2 安装 gdal
在 MSYS2 UCRT64
终端中运行:
pacman -S mingw-w64-ucrt-x86_64-gdal
安装完成后,检查 GDAL 版本:
gdalinfo --version
如果输出类似:
GDAL 3.9.3, released 2024/10/07
则表示 GDAL 安装成功。
2.3 安装pkg-config
在 Windows MSYS2 环境下,**pkg-config**
是 Rust-GDAL 依赖项之一,用于帮助 cargo
识别 GDAL 头文件和库文件的位置。
在 UCRT64 终端中运行:
pacman -S mingw-w64-ucrt-x86_64-pkg-config
安装完成后,检查 pkg-config
版本:
pkg-config --version
如果输出类似:
0.29.2
说明安装成功 。
2.4 配置环境变量
在系统环境变量中配置下面的安装文件对应的路径:
GDAL_HOME=D:\software\msys2\ucrt64 # 主目录
PKG_CONFIG_PATH=D:\software\msys2\ucrt64\lib\pkgconfig # pkg配置文件路径
PROJ_LIB=D:\software\msys2\ucrt64\share\proj # proj的路径
GDAL_LIB_DIR=D:\software\msys2\ucrt64\lib # lib库的路径
GDAL_VERSION=393 # 安装的具体的gdal的版本
2.5 验证Rust-GDAL是否正确配置
创建 Rust 项目
cargo new rust-gdal-test
cd rust-gdal-test
在 Cargo.toml
添加 gdal
依赖
[dependencies]
gdal = "0.17"
在 src/main.rs
添加代码
use gdal::Dataset;
fn main() {
let dataset = Dataset::open("/vsicurl/https://download.osgeo.org/gdal/data/gtiff/small_world.tif").expect("Failed to open dataset");
println!("Dataset size: {:?}", dataset.raster_size());
}
运行代码
cargo run
如果能正确输出影像数据的尺寸信息,说明 Rust-GDAL 配置安装成功!