Building c++ static library for iPhone

xiaoxiao2021-03-01  15

It is a good idea to use c++ static library in a iPhone project, for xcode always need rebuild all files once a while ( Even if I modify only cpp file, I don't know why :( ).The way is simple but there are some points to notice. 1) You should build both simulator and os version. Commonly, you will have four libraries:Debug-iphoneos,Debug-iphonesimulator,Release-iphoneos,and Release-iphonesimulator. 2) How to build static library? Just add a new target with the type static library, and add all relative code files into the target, set the build options and build. 3) A word about device version. You should use code sign when build device version. 4) A word about SDK version. Initially, I use a beta version of iphone sdk, and it can not build device version of libaray but can build app. After I update to the final sdk, everything is OK. 5) How to link with library? Add the library file - with extension ".a"- to frameworks, set the library search path to the library, and use #pragma comment( lib, "xxx.a" ) in your code. You should make separate build config for device and simulator version, for they use different library. so I have four configuration: debug, release, debug_iphone, release_iphone. And there is still a problem for me: the library added to framework is the same to all configurations, I add the debug library to framework, and I cann't change it to release version in release config. I use different library search path to solve this problem, it seems OK. For release config, it will use release version library although the library added to framework is debug version.