本文旨在說明靜態庫製做中的一些常見問題和特殊處理 1. 打包靜態庫須要的相關問題和設置c++
Other Linker Flags
爲-ObjC
或者-all_load
NSClassFromString
或者runtime
的objc_getClass
,可是轉換出來的Class
一直爲nil
。解決方法:在主工程的Other Linker Flags
須要添加參數-ObjC
便可public
了Base SDK
指的是當前編譯所用的SDK 版本,通常默認爲當前xocde的最新版Build Active Architecture Only
設置成No
Deployment Target
它控制着運行應用須要的最低操做系統版本Skip Install
設置爲Yes
Mach-O Type
靜態庫設置爲Static Library
,動態庫設置爲Dynamic Library
,製做bundle
文件設置爲Bundle
xib
,要用的話就將xib
放到bundle
文件中編譯,而後xib
就會變成.nib
的文件C
或者C++
,在使用的時候須要添加libc++.tbd
或者libstdc++.tbd
Implicit declaration of function ‘XXXX’ is invalid in C99
警告:**C語言是過程化的編程語言,程序執行順序是從上到下。若是在調用某函數的時候,函數在調用以前沒有定義也沒有聲明,而是在調用以後定義,那麼編譯時Implicit declaration of function ‘XXXX’ is invalid in C99
警告就產生了。這是有別於面向對象編程語言的地方2. framework中Optional和Required的區別git
Social.framework
和AdSupport.framework
,是在iOS 6以後才被引入的,更新了一些新的特性,若是運行在5.0甚至更低的設備上,這些庫不支持,會編譯通不過,這時候就要使用弱引用了dyld:Library not found ……
說明你可能使用了不應有的強引用,根據日誌將這個庫的引用形式修改一下;或者是使用了動態庫,就須要在Embeded Binaries
選項中添加這個動態庫3. 如何看一個framework中的二進制文件是靜態庫仍是動態庫github
$ file /Users/yostar/Desktop/ProjectTest/YostarSDK/ThirdPath/TwitterKit.framework/TwitterKit
;見下面的截圖,一個是靜態庫,一個是動態庫
4. 查看靜態庫是否支持bitcode $ otool -l /Users/yostar/Desktop/UnityLib/libYostarSDK.a | grep __LLVM
若是上述命令的輸出結果有__LLVM
,那麼就說明,所用的framework
或.a
支持設置Enable bitcode
爲YES
,不然不支持編程
5. 靜態庫相關操做xcode
$ lipo -info ./XXXX.a
$ lipo -info ./XXXX.framework/XXXX
複製代碼
$ lipo -create XXXX_iphoneos.a XXXX_iphonesimulator.a -output XXXX_all.a
$ lipo -create XXXX_iphoneos.framework/XXXX_iphoneos XXXX_iphonesimulator.framework/XXXX_iphonesimulator -output XXXX_all
複製代碼
$ lipo -thin libname.a armv7(CPU架構名稱) -output libname-armv7.a
$ lipo -thin XXXX.framework/XXXX arm64 -output XXXX.framework/XXXX-arm64
複製代碼
6. 打包framework之嵌套另外一靜態庫產生類文件重複問題 將打包好的framework和第三方靜態庫引入項目,運行,產生兩個靜態庫文件類名重複的問題。以下:bash
這就說明在封裝framework時將第三方靜態庫中的文件給引入了,從而形成兩個庫中有多個相同類名文件。 這樣編譯生成的framework就不會和引入的靜態庫有相同的類文件了7. 打包 C,C++文件及和OC混編,接口代碼架構
C
代碼 xcode新建文件YostarUtilits.h
和YostarUtilits.m
,例子以下:#import <Foundation/Foundation.h>
const char * getIDFA();
@interface YostarUtilits : NSObject
@end
複製代碼
#import "YostarUtilits.h"
const char * getIDFA(){
NSString *str = @"123";
const char *strC = [IDFAStr UTF8String];
char *result = (char *)calloc(10, sizeof(char *));
if (result) {
strcpy(result, strC);
}
return result;
}
@implementation YostarUtilits
@end
複製代碼
C++
代碼 xcode新建文件YostarUtilits.h
和YostarUtilits.mm
,例子以下:#import <Foundation/Foundation.h>
@interface YostarUtilits : NSObject
@end
複製代碼
#import "YostarUtilits.h"
#if defined(__cplusplus)
extern "C"
{
#endif
const char * getIDFA(){
NSString *str = @"123";
const char *strC = [IDFAStr UTF8String];
char *result = (char *)calloc(10, sizeof(char *));
if (result) {
strcpy(result, strC);
}
return result;
}
#if defined(__cplusplus)
}
#endif
@implementation YostarUtilits
@end
複製代碼
附:個人博客地址框架