C语言 iOS 库到 BitCode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31233395/
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
iOS library to BitCode
提问by stack_tom
I recently downloaded Xcode 7 beta, and Xcode complains about some of my C libraries not being compiled into BitCode. How would I go about telling Clang to produce BitCode that is compatible with iOS? I've seen similar answers on stackoverflow, but I don't know if they apply to producing BitCode libraries for iOS.
我最近下载了 Xcode 7 beta,Xcode 抱怨我的一些 C 库没有被编译成 BitCode。我将如何告诉 Clang 生成与 iOS 兼容的 BitCode?我在 stackoverflow 上看到过类似的答案,但我不知道它们是否适用于为 iOS 生成 BitCode 库。
Edit:
编辑:
I am using the correct setting, -fembed-bitcode, but when I try to archive, I get the error: ld: warning: ignoring file XXXX/XXXX, file was built for archive which is not the architecture being linked (arm64). When I use -fembed-bitcode-marker, I can archive, but I get the error: full bitcode bundle could not be generated because XX/XX was built only with bitcode marker. The library must be generated from Xcode archive build with bitcode enabled.
我使用了正确的设置,-fembed-bitcode,但是当我尝试存档时,出现错误:ld:警告:忽略文件 XXXX/XXXX,文件是为存档而构建的,这不是正在链接的体系结构 (arm64)。当我使用 -fembed-bitcode-marker 时,我可以存档,但出现错误:无法生成完整的 bitcode 包,因为 XX/XX 仅使用 bitcode 标记构建。该库必须从 Xcode 存档构建生成,并启用位码。
Any ideas on what is going wrong? The library is compiling successfully, but it doesn't let me archive. I created a simple add function and made it into a library, and I get the same symptoms, so it is not the library I'm compiling.
关于出了什么问题的任何想法?该库正在成功编译,但它不允许我存档。我创建了一个简单的 add 函数并将其放入一个库中,我得到了相同的症状,所以它不是我正在编译的库。
Edit 2: You must build both the arm64 and armv7 versions using bitcode and lipo them together. Using bitcode doesn't take away the need for a fat library when archiving. source : https://forums.developer.apple.com/message/25132#25132
编辑 2:您必须使用 bitcode 和 lipo 来构建 arm64 和 armv7 版本。归档时使用 bitcode 并不会消除对胖库的需求。来源:https: //forums.developer.apple.com/message/25132#25132
回答by Danoli3
When building static libraries you must add the following for bitcode generation:
在构建静态库时,您必须为位码生成添加以下内容:
-fembed-bitcode
for a dynamic library you need to additionally link with
对于动态库,您需要另外链接
-fembed-bitcode
Note: This command is only available with Xcode7+
注意:此命令仅适用于 Xcode7+
In regards to the accepted answer of using -fembed-bitcode-marker
关于使用的公认答案 -fembed-bitcode-marker
You should be aware that a normal build with the -fembed-bitcode-marker option will produce minimal size embedded bitcode sections without any real content. This is done as a way of testing the bitcode-related aspects of your build without slowing down the build process. The actual bitcode content is included when you do an Archive build.
您应该知道,使用 -fembed-bitcode-marker 选项的正常构建将生成最小尺寸的嵌入位代码部分,而没有任何实际内容。这样做是为了在不减慢构建过程的情况下测试构建中与位码相关的方面。当您进行存档构建时,会包含实际的位码内容。
bwilson Apple Staff. https://forums.developer.apple.com/thread/3971#12225
比尔森苹果员工。 https://forums.developer.apple.com/thread/3971#12225
To be more specific:
更具体:
-fembed-bitcode-markersimply marks where the bitcode would be in the binary after an archive build.-fembed-bitcodeactually does the full bitcode generation and embedding, so this is what you need to use for building static libraries.- Xcode itself builds with
-fembed-bitcode-markerfor regular builds (like deploy to simulator) - Xcode only builds with
-fembed-bitcodefor archive builds / production builds (as this is only needed for Apple).
-fembed-bitcode-marker简单地标记在存档构建后位码在二进制文件中的位置。-fembed-bitcode实际上完成了完整的位码生成和嵌入,所以这是您构建静态库所需的。- Xcode 本身构建
-fembed-bitcode-marker用于常规构建(如部署到模拟器) - Xcode 仅
-fembed-bitcode用于存档构建/生产构建(因为这仅适用于 Apple)。
回答by Gautam Jain
Go the Build Settings. Search for "custom compiler flags".
Add -fembed-bitcodeto Other C Flags.
This will ensure that the lib is built with bitcode compatibility at compile time. I made this for iOS 64 bit and 32 bit, and lipo'd them into one.
Works like a charm.
转到构建设置。搜索“自定义编译器标志”。
添加-fembed-bitcode到其他 C 标志。这将确保在编译时构建的库与位码兼容。我为 iOS 64 位和 32 位制作了这个,并将它们合成为一个。奇迹般有效。
Since you guys had queries, here's a screenshot of the settings: The settings are same for the project target and the SDK target.
由于你们有疑问,这里是设置的屏幕截图:项目目标和SDK目标的设置相同。
The bitcode lib will not work with Xcode 6.
位码库不适用于 Xcode 6。
回答by Ian Han
If you're still having trouble after adding the -fembed-bitcodeto the Other C flags, search for "Enable Bitcode" under "Build Options" and set it to No. This will allow you to archive properly.
如果在添加-fembed-bitcode到其他 C 标志后仍然遇到问题,请在“构建选项”下搜索“启用位码”并将其设置为否。这将允许您正确存档。
回答by Xeieshan
What you need is -fembed-bitcode. When ENABLE_BITCODE is enabled, Xcode builds with -fembed-bitcode-marker for regular builds and with -fembed-bitcode for archive builds. One option simply "marks" where the bitcode would be in the binary after an archive build and enforces the new bitcode rules, while the other actually does the full-on bitcode generation, which is likely slower and thus not enabled on every kind of build.
你需要的是-fembed-bitcode。当启用 ENABLE_BITCODE 时,Xcode 使用 -fembed-bitcode-marker 构建常规构建,使用 -fembed-bitcode 构建存档构建。一个选项只是简单地“标记”在存档构建后位码在二进制文件中的位置并强制执行新的位码规则,而另一个选项实际上执行完整的位码生成,这可能较慢,因此并非在每种构建中都启用.
Syo Ikeda's guide to handling BITCODE might also help you:
Syo Ikeda 的 BITCODE 处理指南也可能对您有所帮助:


回答by Naishta
If you are building a Static Library and would like to enable the bitcode, just the (1) ENABLE_BITCODE = YES may not be enough.
如果您正在构建静态库并希望启用位码,仅 (1) ENABLE_BITCODE = YES 可能还不够。
(2) Also with the setting -fembed-bitcodethe error below was still being thrown for multiple files when built in Teamcity
(2) 同样的设置-fembed-bitcode,当在 Teamcity 中构建时,仍然为多个文件抛出以下错误
bitcode bundle could not be generated because ‘/path/fileInYourStaticLib.a(fileInYourStaticLib.o)' was built without full bitcode. All object files and libraries for bitcode must be generated from Xcode Archive or Install build for architecture arm64
Few tips/things to consider that helped me to ultimately solve the issue, in addition to the above steps (1) and (2)
除了上述步骤 (1) 和 (2) 之外,还有一些提示/事情可以帮助我最终解决问题
Make Sure you set the variable 'Other C Flags' to "-fembed-bitcode" on the 'PROJECT', and all the 'TARGETS'.
If there are multiple projects that you are trying to create a static library for, make sure all the projects have
"-fembed-bitcode"enabled.On Build Settings, click on the + sign at the top to add a user-defined build setting with the nameBITCODE_GENERATION_MODE, and set Debug tomarker, Release tobitcodeIf the above steps doesn't work, you can also try this option.
On Build Settings -> Other C flags, set Debug to-fembed-bitcode-marker, and Release to-fembed-bitcode
确保在“PROJECT”和所有“TARGETS”上将变量“Other C Flags”设置为“-fembed-bitcode”。
如果您尝试为多个项目创建静态库,请确保所有项目都已
"-fembed-bitcode"启用。On Build Settings, click on the + sign at the top to add a user-defined build setting with the nameBITCODE_GENERATION_MODE, and set Debug to标记位, Release to码如果上述步骤不起作用,您也可以尝试此选项。
On Build Settings -> Other C flags, set Debug to-fembed-bitcode-marker, and Release to-fembed-bitcode
This blog was of great help https://medium.com/@heitorburger/static-libraries-frameworks-and-bitcode-6d8f784478a9
这个博客 很有帮助 https://medium.com/@heitorburger/static-libraries-frameworks-and-bitcode-6d8f784478a9
Also every time you make the above changes, try deleting the DerivedData, Clean the XCode Project, and possibly quit and restart XCode
此外,每次进行上述更改时,请尝试删除 DerivedData,清理 XCode 项目,并可能退出并重新启动 XCode

