55
votes

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.

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.

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.

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 : Link

5
so you compile these C libraries yourself?user102008
Yeah, specifically asking about gmp. But I'm asking in general, how do we get clang to produce BitCode that works with iOS. I know there's an option to get clang to stop at the intermediate representation (IR), but I don't know if this will work with the whole BitCode Apple is mentioning now.stack_tom

5 Answers

73
votes

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+

In regards to the accepted answer of using -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.

bwilson Apple Staff. https://forums.developer.apple.com/thread/3971#12225


To be more specific:

  • -fembed-bitcode-marker simply marks where the bitcode would be in the binary after an archive build.
  • -fembed-bitcode actually 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-marker for regular builds (like deploy to simulator)
  • Xcode only builds with -fembed-bitcode for archive builds / production builds (as this is only needed for Apple).
55
votes

Go the Build Settings. Search for "custom compiler flags".
Add -fembed-bitcode to 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.

Since you guys had queries, here's a screenshot of the settings: The settings are same for the project target and the SDK target.

enter image description here


The bitcode lib will not work with Xcode 6.

31
votes

If you're still having trouble after adding the -fembed-bitcode to the Other C flags, search for "Enable Bitcode" under "Build Options" and set it to No. This will allow you to archive properly.

19
votes

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.

Syo Ikeda's guide to handling BITCODE might also help you:

You can find the full slide deck here.

12
votes

If you are building a Static Library and would like to enable the bitcode, just the (1) ENABLE_BITCODE = YES may not be enough.

(2) Also with the setting -fembed-bitcode the error below was still being thrown for multiple files when built in 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. Make Sure you set the variable 'Other C Flags' to "-fembed-bitcode" on the 'PROJECT', and all the 'TARGETS'.

  2. If there are multiple projects that you are trying to create a static library for, make sure all the projects have "-fembed-bitcode" enabled.

  3. 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 tobitcode

  4. If 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

This blog was of great help 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