Creating a Fat Static Library


As you probably have noticed after compiling the demo project, compile time is rather long. While compile time will go down when you re-compile, once in a while, and every time you clean the project, it will again re-compile the entire project.

To avoid this, and to drastically reduce compile time, you should create a static library of cocos2d-x.

First of all, open Xcode, and from the top menu Xcode, select Preferences. Select the last tab “Locations”, and change “Derived Data” from “Default” to “Relative”. This will make sure it is humanly possible to find the library outputs, but remember to switch it back afterwards.

In any project you create with the command line tool, you will find a “cocos2d” folder. In this folder there is a sub-folder named “build”, containing an Xcode project named cocos2d_libs.xcodeproj.

Open this project with Xcode.

First select the project, and select “Build Settings”. Make sure that “Build Active Architecture Only” is set to No for debug. As we are building a debug library, this will make sure that both 32 and 64 bit architecture is added to the library. When done, you should switch the setting back to YES.

After that, select the target “libcocos2d iOS”, and build it for both device and simulator. This will most likely take a couple of minutes, unless you are working on NASA specced hardware.

After you are done, you will in the cocos2d/build folder, now also have a “DerivedData” folder. Here you will have two files:

  1. cocos2d_libs/Build/Products/Debug-iphonesimulator/libcocos2d iOS.a

  2. cocos2d_libs/Build/Products/Debug-iphoneos/libcocos2d iOS.a

These are the static libraries for simulator and device, both including 2 and 64 bit architecture. Final step is to merge these into a single library, called a “fat” library.

To do this, switch to terminal and run the command

lipo -create -output “output filename” “input filename simulator” “input filename os”

Personally I navigated to cocos2d_libs/Build/Products, and ran the command

lipo -create -output “libcocos2d-x 3.7 iOS.a” “Debug-iphoneos/libcocos2d iOS.a” “Debug-iphonesimulator/libcocos2d iOS.a”

Finally you can switch Xcode preferences back to default, remove the cocos2d sub project, and in stead add the fat library.