在 Windows 上编译 x86 (32位) 版本的 libimagequant_sys.a
,你需要确保 Rust 工具链和 C 编译器都配置为 32 位目标。以下是具体步骤:
1. 确保 Rust 支持 32 位编译
运行以下命令安装 i686-pc-windows-msvc
或 i686-pc-windows-gnu
目标(取决于你的工具链):
rustup update
rustup target add i686-pc-windows-msvc # 如果你使用 MSVC 工具链
# 或者
rustup target add i686-pc-windows-gnu # 如果你使用 MinGW/GCC 工具链
2. 安装 32 位 C 编译器
MSVC (Visual Studio) 用户
- 确保已安装 Visual Studio 2022 或 2019 ,并勾选:
- "Desktop development with C++"
- "MSVC v143 - VS 2022 C++ x86/x64 build tools"
- 或者使用 Build Tools for Visual Studio(仅命令行工具)。
MinGW (GCC) 用户
-
安装 32 位 MinGW (如 MSYS2):
pacman -S mingw-w64-i686-gcc
-
确保
i686-w64-mingw32-gcc
在PATH
中。
3. 编译 libimagequant-sys(32 位)
方法 1:使用 --target
参数
git clone https://github.com/ImageOptim/libimagequant
cd imagequant-sys
cargo build --release --target i686-pc-windows-msvc # MSVC 工具链
# 或
cargo build --release --target i686-pc-windows-gnu # MinGW 工具链
生成的静态库路径:
target\i686-pc-windows-msvc\release\libimagequant_sys.a
方法 2:设置环境变量(强制 32 位编译)
set CC=clang-cl -m32 # 如果使用 Clang
set CFLAGS=-m32
cargo build --release --target i686-pc-windows-msvc
4. 验证生成的二进制是否为 32 位
使用 dumpbin
(MSVC) 或 file
(MinGW) 检查:
dumpbin /HEADERS target\i686-pc-windows-msvc\release\libimagequant_sys.a | find "machine"
# 应该输出 "x86" 或 "386"
或(MinGW):
file target/i686-pc-windows-gnu/release/libimagequant_sys.a
# 应该显示 "Intel 80386" 或 "PE32" (32-bit)
常见问题
-
link.exe
找不到?- 确保从 "x86 Native Tools Command Prompt for VS" 运行命令(MSVC)。
- 或运行
vcvarsall.bat x86
设置环境变量。
-
MinGW 编译失败?
- 确保安装的是
mingw-w64-i686-gcc
而不是 64 位版本。
- 确保安装的是
-
Rust 报错 "can't find crate for std"?
- 运行
rustup target add i686-pc-windows-msvc
安装标准库。
- 运行
总结
-
MSVC 用户:
rustup target add i686-pc-windows-msvc cargo build --release --target i686-pc-windows-msvc
-
MinGW 用户:
rustup target add i686-pc-windows-gnu cargo build --release --target i686-pc-windows-gnu
这样就能在 Windows 上编译出 32 位 (x86) 的 libimagequant_sys.a
。
在 Windows 上编译 x64 (64位) 版本的 libimagequant_sys.a,方法与编译 x86 类似,但需要调整目标架构和工具链配置。以下是详细步骤:
- 确保 Rust 支持 64 位编译
默认情况下,Rust 在 Windows 上会使用 x86_64 目标。如果尚未安装,运行:
rustup update
rustup target add x86_64-pc-windows-msvc # MSVC 工具链
或
rustup target add x86_64-pc-windows-gnu # MinGW/GCC 工具链
- 安装 64 位 C 编译器
MSVC (Visual Studio) 用户
• 确保已安装 Visual Studio 2022/2019,并勾选:
• "Desktop development with C++"
• "MSVC v143 - VS 2022 C++ x64 build tools"
• 或使用 Build Tools for Visual Studio(仅命令行工具)。
MinGW (GCC) 用户
• 安装 64 位 MinGW(如 https://www.msys2.org/):
pacman -S mingw-w64-x86_64-gcc
• 确保 x86_64-w64-mingw32-gcc 在 PATH 中。
- 编译 libimagequant-sys(64 位)
方法 1:使用 --target 参数
git clone https://github.com/ImageOptim/libimagequant
cd imagequant-sys
cargo build --release --target x86_64-pc-windows-msvc # MSVC 工具链
或
cargo build --release --target x86_64-pc-windows-gnu # MinGW 工具链
生成的静态库路径:
target\x86_64-pc-windows-msvc\release\libimagequant_sys.a
方法 2:默认编译(无需指定 target)
如果当前 Rust 工具链已经是 64 位,可直接运行:
cargo build --release
生成的库默认位于:
target\release\libimagequant_sys.a
- 验证生成的二进制是否为 64 位
MSVC 用户
dumpbin /HEADERS target\x86_64-pc-windows-msvc\release\libimagequant_sys.a | find "machine"
输出应为 x64 或 8664(表示 64 位)。
MinGW 用户
file target/x86_64-pc-windows-gnu/release/libimagequant_sys.a
输出应包含 x86_64 或 PE32+(表示 64 位 PE 文件)。
常见问题
-
link.exe 找不到?
• 确保从 "x64 Native Tools Command Prompt for VS" 运行命令(MSVC)。
• 或运行 vcvarsall.bat x64 设置环境变量。
-
MinGW 编译失败?
• 确认安装的是 mingw-w64-x86_64-gcc(64 位),而非 32 位版本。
-
Rust 报错 "can't find crate for std"?
• 运行 rustup target add x86_64-pc-windows-msvc 安装标准库。
总结
• MSVC 用户:
cargo build --release --target x86_64-pc-windows-msvc
• MinGW 用户:
cargo build --release --target x86_64-pc-windows-gnu
• 默认 64 位工具链:
cargo build --release
编译完成后,检查输出文件的架构即可确认是否成功生成 64 位库。