如何为armv6、armv7和i386编译静态库(fat)

发布于 2024-08-31 09:53:53 字数 346 浏览 12 评论 0原文

我知道这个问题已经被提出过几次,但我的目标与我在网上搜索到的内容略有不同。具体来说,我已经能够为 iPhone 构建静态库,但我能够构建的最终 fat 文件仅包含 arm 和 i386 架构(并且我不确定 arm 指的是:是 v6 还是 v7?)。我无法专门针对armv6和armv7进行编译,它们使用lipo合并这两种架构。 lipo 工具抱怨armv6 和armv7 库中存在相同的架构(arm,而不是armv6 或armv7)。

有人可以准确解释如何构建armv6和armv7,并使用lipo将这些库合并到一个fat文件中吗?

编辑:我需要不使用 Xcode 进行构建,而是直接编译传统的 unix 库。

I know this question has been posed several times, but my goal is slightly different with regard to what I have found searching the web. Specifically, I am already able to build a static library for iPhone, but the final fat file I am able to build only contains arm and i386 architectures (and I am not sure to what arm refers: is v6 or v7?). I am not able to compile specifically for armv6 and armv7 and them merge both architectures using lipo. The lipo tool complains that the same architecture (arm, not armv6 or armv7) is present in both the armv6 and armv7 libraries.

Can someone explain exactly how to build for armv6 and armv7, and them merge these libraries into a fat file using lipo?

EDIT: I need to build not using Xcode but compiling directly a traditional unix library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

錯遇了你 2024-09-07 09:53:53

这是我找到的一个很好的解决方案: 支持 iOS 5 和 Arm64 的静态库

编辑:

解决方案是使用命令行(或 Rakefile)构建不同的架构,然后使用 lipo 绑定它们。

首先使用 xcodebuild 使用arm 构建二进制文件:

xcodebuild -project 'StaticLibDemo.xcodeproj' -configuration 'Release' -sdk 'iphoneos7.0' clean build ARCHS='armv7 armv7s' IPHONEOS_DEPLOYMENT_TARGET='5.0' TARGET_BUILD_DIR='./build-arm' BUILT_PRODUCTS_DIR='./build-arm'

请注意,您必须设置 IPHONEOS_DEPLOYMENT_TARGET='5.0' 和 ARCHS='armv7 armv7s',建议设置构建和产品目录以使事情更清晰,请查看构建设置参考更多详细信息该标志的含义。

arm64 的下一个版本:

xcodebuild -project 'StaticLibDemo.xcodeproj' -configuration 'Release' -sdk 'iphoneos7.0' clean build ARCHS='arm64' IPHONEOS_DEPLOYMENT_TARGET='7.0' TARGET_BUILD_DIR='./build-arm64' BUILT_PRODUCTS_DIR='./build-arm64'

请注意 ARCHS 和 IPHONEOS_DEPLOYMENT_TARGET 上的差异。我们还需要构建模拟器,在这种情况下,我们必须将 sdk 更改为 iphonesimulator7.0,并首先分两步构建 i386:

xcodebuild -project 'StaticLibDemo.xcodeproj' -configuration 'Release' -sdk 'iphonesimulator7.0' clean build ARCHS='i386' IPHONEOS_DEPLOYMENT_TARGET='5.0' TARGET_BUILD_DIR='./build-i386' BUILT_PRODUCTS_DIR='./build-i386'

现在是棘手的部分!如果您只是根据 Xcode 设置将 ARCHS 更改为 x86_86,您将收到类似以下错误:“x86_64 不是有效的架构”。为了避免这种情况,只需添加 VALID_ARCHS='x86_64':

xcodebuild -project 'StaticLibDemo.xcodeproj' -configuration 'Release' -sdk 'iphonesimulator7.0' clean build ARCHS='x86_64' VALID_ARCHS='x86_64' IPHONEOS_DEPLOYMENT_TARGET='7.0' TARGET_BUILD_DIR='./build-x86_64' BUILT_PRODUCTS_DIR='./build-x86_64'

最后我们只需要创建一个包含所有 5 种架构的胖二进制文件:

lipo -create './build-arm/libStaticLibDemo.a' './build-arm64/libStaticLibDemo.a' './build-i386/libStaticLibDemo.a' './build-x86_64/libStaticLibDemo.a' -output 'libStaticLibDemo.a'

作者为此创建了一个工作示例,您可以获取它:https://github.com/diogot/StaticLibDemo


这是帖子的链接:支持 iOS 5 和 Arm64 的静态库

所有积分都去迪奥戈·特里达帕利。

Here is a good solution I found: Static Libs With Support to iOS 5 and Arm64

Edited:

The solution is to build different architectures separated then bind them using lipo, by using command line (or Rakefile).

First build the binary with arm using xcodebuild:

xcodebuild -project 'StaticLibDemo.xcodeproj' -configuration 'Release' -sdk 'iphoneos7.0' clean build ARCHS='armv7 armv7s' IPHONEOS_DEPLOYMENT_TARGET='5.0' TARGET_BUILD_DIR='./build-arm' BUILT_PRODUCTS_DIR='./build-arm'

Note that you must set IPHONEOS_DEPLOYMENT_TARGET='5.0' and ARCHS='armv7 armv7s', it’s recommended to set build and product dirs to make the things more clear, take a look at Build Setting Reference for more details what this flags means.

Next build for arm64:

xcodebuild -project 'StaticLibDemo.xcodeproj' -configuration 'Release' -sdk 'iphoneos7.0' clean build ARCHS='arm64' IPHONEOS_DEPLOYMENT_TARGET='7.0' TARGET_BUILD_DIR='./build-arm64' BUILT_PRODUCTS_DIR='./build-arm64'

Note the difference on ARCHS and IPHONEOS_DEPLOYMENT_TARGET. We also need to build for simulator, in this case we have to change the sdk to iphonesimulator7.0 and build in two steps first for i386:

xcodebuild -project 'StaticLibDemo.xcodeproj' -configuration 'Release' -sdk 'iphonesimulator7.0' clean build ARCHS='i386' IPHONEOS_DEPLOYMENT_TARGET='5.0' TARGET_BUILD_DIR='./build-i386' BUILT_PRODUCTS_DIR='./build-i386'

Now the tricky part! If you just change the ARCHS to x86_86 depending on your Xcode setting you will got an error like: “x86_64 is not a valid arch”. To avoid this just add VALID_ARCHS='x86_64':

xcodebuild -project 'StaticLibDemo.xcodeproj' -configuration 'Release' -sdk 'iphonesimulator7.0' clean build ARCHS='x86_64' VALID_ARCHS='x86_64' IPHONEOS_DEPLOYMENT_TARGET='7.0' TARGET_BUILD_DIR='./build-x86_64' BUILT_PRODUCTS_DIR='./build-x86_64'

Finally we just have to create a fat binary with all the 5 architectures:

lipo -create './build-arm/libStaticLibDemo.a' './build-arm64/libStaticLibDemo.a' './build-i386/libStaticLibDemo.a' './build-x86_64/libStaticLibDemo.a' -output 'libStaticLibDemo.a'

The author created a working example of this, you can get it: https://github.com/diogot/StaticLibDemo


Here is the Link to the post: Static Libs With Support to iOS 5 and Arm64

All credits go to Diogo Tridapalli.

断桥再见 2024-09-07 09:53:53

只需使用 libtool 将两个 arm6 和 arm7 版本链接在一起 - 这就是 XCode 的作用。但是,如果您尝试将这些静态库合并到一个新的超级库中,则会遇到问题。如果您需要这样做,请阅读本文

如果您已经这样做了,这就是 lipo 抱怨您的“armv6”库同时包含armv6和armv7的原因。我的帖子有一个修复程序,可能对您来说更容易,因为您不使用 XCode,但基本上您使用 lipo -extract 来确保您有一个精简的armv6库和一个精简的armv7库,然后再继续。

Just use libtool to link the two arm6 and arm7 versions together - its what XCode does. However you will have problems if you try to combine these static libraries into a new super-library. If you need to do that then read this.

If you are doing this already, that would be why lipo is complaining that your "armv6" library contains both armv6 and armv7. My post has a fix that will probably be easier for you since you don't use XCode, but basically you use lipo -extract to make sure you have a thin armv6 library and a thin armv7 library before you go any further.

诗酒趁年少 2024-09-07 09:53:53

在重新加入之前似乎不需要从脂肪库中提取(如杰米的回答中所述)。我使用的是苹果公司的最终版本 4.0 SDK,它似乎默认创建了 fat armv6/armv7 库。

我之前为输入库指定了体系结构,如下所示:

$DEVROOT/usr/bin/lipo -arch arm $PROJECT_DIR/buildlib/Release-iphoneos/lib.a -arch i386 $PROJECT_DIR/buildlib/Release-iphonesimulator/lib.a -create -output $PROJECT_DIR/buildlib/lib.a

这在后来的 SDK 上会失败,但从(现在是胖的)arm 库中删除体系结构可以正常工作:

$DEVROOT/usr/bin/lipo $PROJECT_DIR/buildlib/Release-iphoneos/lib.a -arch i386 $PROJECT_DIR/buildlib/Release-iphonesimulator/lib.a -create -output $PROJECT_DIR/buildlib/lib.a

Lipo 现在必须能够检测胖库中的体系结构。

There doesn't seem to be a need to extract from the fat library before rejoining any more (as described in jamie's answer). I'm using the final 4.0 SDK from apple, which appear to create the fat armv6/armv7 libraries by default.

I was previously specifying the architecture for the input lib like so:

$DEVROOT/usr/bin/lipo -arch arm $PROJECT_DIR/buildlib/Release-iphoneos/lib.a -arch i386 $PROJECT_DIR/buildlib/Release-iphonesimulator/lib.a -create -output $PROJECT_DIR/buildlib/lib.a

This fails on the later SDKs, but removing the architecture from the (now fat) arm lib works fine:

$DEVROOT/usr/bin/lipo $PROJECT_DIR/buildlib/Release-iphoneos/lib.a -arch i386 $PROJECT_DIR/buildlib/Release-iphonesimulator/lib.a -create -output $PROJECT_DIR/buildlib/lib.a

Lipo must now be able to detect the architectures in the fat libraries.

晌融 2024-09-07 09:53:53

确保将构建设置设置为有效架构:armv6 armv7架构:优化(armv6 armv7)。这应该会产生针对 v6 和 v6 进行优化的二进制文件。 v7。如果您不确定是否有效,只需设置Architectures: Standard (armv6) 并比较文件大小。优化应该产生两倍的大小(当我没记错时)。

您还可以在二进制文件上使用 lipo -info 来查看所有包含的架构。

在我的应用程序的发行版上运行它可以得到:

ullrich ~/Code/.../build/Distribution-iphoneos/My.app (streaming)$ lipo -info My
Architectures in the fat file: My are: armv6 armv7 

Make sure to have your build settings set to Valid Architectures: armv6 armv7 and Architectures: Optimized (armv6 armv7). This should result in a binary optimized for both v6 & v7. If you're not sure it worked out, just set the Architectures: Standard (armv6) and compare the file sizes. Optimized should produce double the size (when I remember rightly).

You also always can use lipo -info on your binary to see all the included architecures.

Running it on a distribution build of my app gives me:

ullrich ~/Code/.../build/Distribution-iphoneos/My.app (streaming)$ lipo -info My
Architectures in the fat file: My are: armv6 armv7 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文