xcode 我如何为 armv6、armv7 和 i386 编译静态库(胖)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2793392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i compile a static library (fat) for armv6, armv7 and i386
提问by Massimo Cafaro
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.
我知道这个问题已被多次提出,但我的目标与我在网上搜索的内容略有不同。具体来说,我已经能够为 iPhone 构建一个静态库,但我能够构建的最终胖文件只包含 arm 和 i386 架构(我不确定 arm 指的是什么:v6 还是 v7?)。我无法专门为 armv6 和 armv7 编译,它们使用 lipo 合并了两种架构。lipo 工具抱怨 armv6 和 armv7 库中都存在相同的架构(arm,而不是 armv6 或 armv7)。
Can someone explain exactly how to build for armv6 and armv7, and them merge these libraries into a fat file using lipo?
有人可以确切地解释如何为 armv6 和 armv7 构建,然后他们使用 lipo 将这些库合并到一个胖文件中吗?
EDIT: I need to build not using Xcode but compiling directly a traditional unix library.
编辑:我需要不使用 Xcode 构建,而是直接编译传统的 unix 库。
采纳答案by jamie
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.
只需使用 libtool 将两个 arm6 和 arm7 版本链接在一起 - 这就是 XCode 所做的。但是,如果您尝试将这些静态库组合到一个新的超级库中,则会遇到问题。如果您需要这样做,请阅读此内容。
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.
如果您已经这样做了,这就是为什么 lipo 抱怨您的“armv6”库包含 armv6 和 armv7 的原因。我的帖子有一个修复程序,由于您不使用 XCode,这对您来说可能会更容易,但基本上您使用 lipo -extract 来确保在您继续之前有一个瘦 armv6 库和一个瘦 armv7 库。
回答by Yang
Here is a good solution I found: Static Libs With Support to iOS 5 and Arm64
这是我找到的一个很好的解决方案: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).
解决方案是构建分离的不同架构,然后使用 lipo 绑定它们,通过使用命令行(或 Rakefile)。
First build the binary with arm using xcodebuild:
首先使用 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'
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.
请注意,您必须设置 IPHONEOS_DEPLOYMENT_TARGET='5.0' 和 ARCHS='armv7 armv7s',建议设置 build 和 product dirs 以使事情更清楚,查看 Build Setting Reference 以了解更多详细信息此标志的含义。
Next build for arm64:
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:
注意 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'
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':
现在是棘手的部分!如果您只是根据您的 Xcode 设置将 ARCHS 更改为 x86_86,您将收到如下错误:“x86_64 is not a valid arch”。为避免这种情况,只需添加 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:
最后,我们只需要创建一个包含所有 5 种架构的胖二进制文件:
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
作者为此创建了一个工作示例,您可以获取它:https: //github.com/diogot/StaticLibDemo
Here is the Link to the post: Static Libs With Support to iOS 5 and Arm64
这是帖子的链接:支持 iOS 5 和 Arm64 的静态库
All credits go to Diogo Tridapalli.
所有学分都归于 Diogo Tridapalli。
回答by gazreese
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.
在重新加入之前似乎不需要从胖库中提取(如杰米的回答中所述)。我使用的是 Apple 的最终 4.0 SDK,它似乎默认创建了 fat armv6/armv7 库。
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:
这在后来的 SDK 上失败了,但是从(现在是胖的)arm lib 中删除架构可以正常工作:
$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.
Lipo 现在必须能够检测到胖库中的架构。
回答by stigi
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).
确保将构建设置设置为Valid Architectures: armv6 armv7
和Architectures: Optimized (armv6 armv7)
。这应该会产生针对 v6 和 v7 优化的二进制文件。如果您不确定它是否成功,只需设置Architectures: Standard (armv6)
并比较文件大小。优化应该产生两倍的大小(当我没记错的时候)。
You also always can use lipo -info
on your binary to see all the included architecures.
您也始终可以lipo -info
在二进制文件上使用以查看所有包含的架构。
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