使用 go build 为 Iphone 模拟器构建静态库
php小编子墨介绍:在开发iOS应用程序时,我们常常需要使用静态库来扩展功能或者提供一些通用的工具函数。使用go语言进行开发的开发者可能会想知道如何使用go build命令来构建适用于iPhone模拟器的静态库。在本文中,我们将详细介绍如何使用go build命令来构建静态库,并且提供一些实用的技巧和注意事项,帮助开发者顺利完成静态库的构建过程。无论你是初学者还是有一定经验的开发者,都可以通过阅读本文来获得关于构建静态库的实用知识和技巧。
问题内容
我使用以下方法在我的 ios 项目中构建一个 c 存档:
goos=ios goarch=arm64 cgo_enabled=1 sdk=iphonesimulator cgo_cflags="-fembed-bitcode" cc=
pwd/clangwrap.sh go build -buildmode=c-archive -o libuplink.a
clangwrap.sh 看起来像这样
#!/bin/sh # go/clangwrap.sh sdk_path=`xcrun --sdk $sdk --show-sdk-path` clang=`xcrun --sdk $sdk --find clang` if [ "$goarch" == "amd64" ]; then carch="x86_64" elif [ "$goarch" == "arm64" ]; then carch="arm64" fi exec $clang -arch $carch -isysroot $sdk_path -mios-version-min=10.0 "$@"
当我在 xcode 中链接它并尝试使用模拟器运行时,我只能在设备本身上运行它:
building for iOS Simulator, but linking in object file built for iOS ... for architecture arm64
如何将 go build
的模拟器定位为 swift 项目中使用的静态库?
解决方法
要求
- 为 iphone 模拟器创建静态库
- 使用 apple silicon 而不是 intel 模拟器
- 能够实现特定的最低版本
tl;dr
如果您选择模拟器作为运行目的地,您可以执行类似于 xcode 的操作。
所以基本上使用 -target arm64-apple-ios16.2-simulator
之类的东西而不是 -arch arm64
。还要省略 -mios-version-min=10.0
,因为实际的最小版本是在 -target 中编码的(例如 16.2),它优先(模拟器的正确选项无论如何都是 -miphonesimulator-version-min
) .
然后,作为 cgo_ldflags
,还指定 -target
选项以及 -syslibroot
以及 sdk 的路径。
你的构建脚本稍微调整了一下,它可能看起来像这样:
这指定模拟器作为目标,最低版本为 15。
build.sh
#!/bin/sh export goos=ios export goarch=arm64 export cgo_enabled=1 export sdk=iphonesimulator export cgo_cflags="-fembed-bitcode" export min_version=15 . ./target.sh export cgo_ldflags="-target ${target} -syslibroot \"${sdk_path}\"" cc="$(pwd)/clangwrap.sh" export cc go build -buildmode=c-archive -o libuplink.a
target.sh
#!/bin/sh sdk_path=$(xcrun --sdk "$sdk" --show-sdk-path) export sdk_path if [ "$goarch" = "amd64" ]; then carch="x86_64" elif [ "$goarch" = "arm64" ]; then carch="arm64" fi if [ "$sdk" = "iphoneos" ]; then export target="$carch-apple-ios$min_version" elif [ "$sdk" = "iphonesimulator" ]; then export target="$carch-apple-ios$min_version-simulator" fi
clangwrap.sh
然后 clangwrap.sh
简化为:
#!/bin/zsh clang=$(xcrun --sdk "$sdk" --find clang) exec "$clang" -target "$target" -isysroot "$sdk_path" "$@"
详细信息
不同的 sdk
必须为 ios 设备和 iphone 模拟器指定不同的 sdk。您可以在 xcode 支持的其他平台旁边找到它们
在 /applications/xcode.app/contents/developer/platforms
下。例如,在 xcode 14.2 等中,有一个带有 iphoneos16.2.sdk
的 iphoneos
平台和带有 iphonesimulator16.2.sdk
的 iphonesimulator
平台。
apple 开发者论坛中的一位 apple 员工发布了这篇有趣的帖子:https: //developer.apple.com/forums/thread/673387#662260022
要检查生成的静态库以显示load
命令,可以调用:
otool -l libuplink.a
生成的用于 apple silicon 模拟器的静态库应显示如下内容:
... load command 1 cmd lc_build_version cmdsize 24 platform 7 minos 15.0 sdk 16.2 ...
注意:platform 7
表示模拟器,minos
表示最低部署目标,sdk
表示实际使用的 sdk 版本。
请参阅包含文件 loader.h
中的部分,内容如下:
/* known values for the above platform field. */ #define platform_unknown 0 #define platform_any 0xffffff #define platform_macos 1 #define platform_ios 2 #define platform_tvos 3 #define platform_watchos 4 #define platform_bridgeos 5 #define platform_maccatalyst 6 #define platform_iossimulator 7 #define platform_tvossimulator 8 #define platform_watchossimulator 9 #define platform_driverkit 10
您可以在自己的系统上自行查看它们,如下所示:
cat `xcrun --sdk iphonesimulator --show-sdk-path`/usr/include/mach-o/loader.h
专为 iphone 设备构建
要为 iphone sdk 构建静态库,您需要更改以下内容:
export sdk=iphoneos
在上面的 build.sh
脚本中。
otool -l
的输出将显示为:
... Load command 1 cmd LC_BUILD_VERSION cmdsize 24 platform 2 minos 15.0 sdk 16.2 ntools 0 ...
注意:platform 2
代表 platform_ios
而不是模拟器。
这当然可以在设备上完美运行。
以上是使用 go build 为 Iphone 模拟器构建静态库的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Go语言中使用RedisStream实现消息队列时类型转换问题在使用Go语言与Redis...

苹果iPhone17或将迎来重大升级,以应对国内华为、小米等强劲竞争对手的冲击。据数码博主@数码闲聊站爆料,iPhone17标准版有望首次搭载高刷新率屏幕,显着提升用户体验。此举标志着苹果历经五年,终于将高刷新率技术下放至标准版机型。目前,iPhone16作为6000元价位段唯一一款配备60Hz屏幕的旗舰手机,显得有些落后。虽然iPhone17标准版将拥有高刷新率屏幕,但与Pro版相比仍存在差异,例如边框设计仍未达到Pro版的超窄边框效果。更值得关注的是,iPhone17Pro系列将采用全新、更

有两种查看 XML 文件的方法:安卓手机:使用文件管理器或第三方应用(XML Viewer、DroidEdit)。iPhone:通过 iCloud Drive 传输文件,然后使用“文件”应用程序或第三方应用(XML Buddy、Textastic)。

Go爬虫Colly中的Queue线程问题探讨在使用Go语言的Colly爬虫库时,开发者常常会遇到关于线程和请求队列的问题。�...

GoLand中自定义结构体标签不显示怎么办?在使用GoLand进行Go语言开发时,很多开发者会遇到自定义结构体标签在�...

Go语言中字符串打印的区别:使用Println与string()函数的效果差异在Go...

Go语言中用于浮点数运算的库介绍在Go语言(也称为Golang)中,进行浮点数的加减乘除运算时,如何确保精度是�...

iPhone 上没有内置 XML 查看器,可以使用第三方应用来打开 XML 文件,如 XML Viewer、JSON Viewer。方法:1. 在 App Store 下载并安装 XML 查看器;2. 在 iPhone 上找到 XML 文件;3. 长按 XML 文件,选择“共享”;4. 选择已安装的 XML 查看器应用;5. XML 文件将在该应用中打开。注意:1. 确保 XML 查看器兼容 iPhone iOS 版本;2. 输入文件路径时注意大小写敏感;3. 谨慎处理包含外部实体的 XML 文
