当前位置: 首页 > news >正文

libimagequant 在 mac 平台编译双架构

在 macOS 上编译 libimagequant 的双架构(aarch64 + x86_64)通用二进制库,以下是完整步骤:


​1. 准备 Rust 工具链​

# 安装两个目标平台
rustup target add aarch64-apple-darwin x86_64-apple-darwin# 确认安装成功
rustup target list --installed
# 应输出:aarch64-apple-darwin, x86_64-apple-darwin

​2. 分别编译两个架构​

​编译 ARM64 架构 (M1/M2)​
cd /Users/admin/open_lib/libimagequant/imagequant-sys
cargo build --release --target aarch64-apple-darwin
​编译 x86_64 架构 (Intel)​
cargo build --release --target x86_64-apple-darwin

​3. 合并为通用二进制 (Universal Binary)​

# 创建输出目录
mkdir -p target/universal# 使用 lipo 合并静态库
lipo -create \-output target/universal/libimagequant.a \target/aarch64-apple-darwin/release/libimagequant_sys.a \target/x86_64-apple-darwin/release/libimagequant_sys.a# 验证合并结果
lipo -info target/universal/libimagequant.a
# 应输出:Architectures in the fat file: target/universal/libimagequant.a are: x86_64 arm64 

​4. 配置 Cargo 自动构建 (可选)​

imagequant-sys/Cargo.toml 中添加构建脚本配置:

[package]
build = "build.rs"[target.'cfg(target_os = "macos")'.dependencies]
libc = "0.2"

build.rs 中添加:

fn main() {println!("cargo:rerun-if-changed=build.rs");#[cfg(target_os = "macos")]{println!("cargo:rustc-link-search=native=target/universal");println!("cargo:rustc-link-lib=static=imagequant");}
}

​5. 验证最终库​

# 检查文件类型
file target/universal/libimagequant.a
# 应显示:Mach-O universal binary with 2 architectures# 检查符号表
nm -arch arm64 target/universal/libimagequant.a | head
nm -arch x86_64 target/universal/libimagequant.a | head

​6. 清理与重建​

# 完全清理后重建
cargo clean
cargo build --release --target aarch64-apple-darwin
cargo build --release --target x86_64-apple-darwin
lipo -create ... # 重复步骤3

​常见问题解决​

  1. lipo 报错 "has same architecture"​
    删除旧库再合并:
rm target/universal/libimagequant.a
  1. ​缺少 core/std 错误​
    确保工具链完整:
rustup component add rust-src
  1. ​Xcode 兼容性问题​
    更新命令行工具:
xcode-select --install

​最终目录结构​

libimagequant/
├── target/
│   ├── aarch64-apple-darwin/
│   ├── x86_64-apple-darwin/
│   └── universal/
│       └── libimagequant.a  # 通用二进制
└── imagequant-sys/└── target/...           # 子项目构建目录

通过以上步骤,你将获得一个同时支持 Intel 和 Apple Silicon 的静态库,可直接用于跨平台分发。

全流程记录

# 1. 进入项目目录
cd /Users/admin/open_lib/libimagequant/imagequant-sys# 2. 清理并编译ARM64架构 (M1/M2)
cargo clean && cargo build --target aarch64-apple-darwin
# 输出示例:
# Finished `dev` profile [unoptimized + debuginfo] target(s) in 1m 41s# 3. 验证ARM64二进制
cd ../target/aarch64-apple-darwin/debug
lipo -info libimagequant_sys.a
# 应输出:Non-fat file: libimagequant_sys.a is architecture: arm64# 4. 返回项目目录编译x86_64架构 (Intel)
cd ../../../../imagequant-sys
cargo build --release --target x86_64-apple-darwin
# 输出示例:
# Finished `release` profile [optimized] target(s) in 6.58s# 5. 验证x86_64二进制
cd ../target/x86_64-apple-darwin/release
lipo -info libimagequant_sys.a
# 应输出:Non-fat file: libimagequant_sys.a is architecture: x86_64# 6. 创建通用二进制
cd ../../..
mkdir -p universal
lipo -create \target/aarch64-apple-darwin/debug/libimagequant_sys.a \target/x86_64-apple-darwin/release/libimagequant_sys.a \-output universal/libimagequant_universal.a# 7. 最终验证
lipo -info universal/libimagequant_universal.a
# 应输出:Architectures in the fat file: universal/libimagequant_universal.a are: arm64 x86_64
http://www.dtcms.com/a/271916.html

相关文章:

  • 在 Mac 上安装 Java 和 IntelliJ IDEA(完整笔记)
  • CMD,PowerShell、Linux/MAC设置环境变量
  • MacOS 终端(Terminal)配置显示日期时间
  • 政安晨【开源人工智能硬件】【ESP乐鑫篇】 —— 在macOS上部署工具开发环境(小资的非开发者用苹果系统也可以玩乐鑫)
  • Canny边缘检测(cv2.Canny())
  • Mac自定义右键功能
  • 如何在Linux上搭建本地Docker Registry并实现远程连接
  • 【TCP/IP】6. 差错与控制报文协议
  • Rust Web 全栈开发(四):构建 REST API
  • llvm, polly, 向量化测试,GEMM非常明显
  • ValueConverter转换器WPF
  • Leetcode力扣解题记录--第42题 接雨水(动规和分治法)
  • 开源 Arkts 鸿蒙应用 开发(六)数据持久--文件和首选项存储
  • [特殊字符] LLM(大型语言模型):智能时代的语言引擎与通用推理基座
  • 藏文识别技术的关键挑战与解决方案
  • 使用Visual Studio Code附加到Jetty进程调试Servlet
  • 小皮面板搭建pikachu靶场
  • 710 Mybatis实战
  • Go语言中map的零值是什么?对零值map进行读写操作会发生什么?如何正确初始化和使用map?
  • 力扣-73.矩阵置零
  • 【会员专享数据】2013-2024年我国省市县三级逐年SO₂数值数据(Shp/Excel格式)
  • 2025年材料应用与计算机科学国际会议(MACS 2025)
  • C++中的左值、右值与std::move()
  • 数据 + 模型 驱动 AI Native 应用发展
  • 利用DBeaver实现异构数据库数据定时任务同步
  • 计算机网络实验——以太网安全实验
  • Flutter 知识点总结
  • React虚拟DOM的进化之路
  • Vue.js 过渡 动画
  • 如何在Flutter开发中系统性减少知识盲区