Xcode 5.1 编译模拟器以及真机都能使用的静态库
Xcode 5.1.dmg 下載地址
http://pan.baidu.com/s/1jGJpKm6
?
1.新建 Framework & Library 工程
我起名叫ShowInfo,下面為其源碼
showInfo.h
=========================
#import <Foundation/Foundation.h>
@interface ShowInfo : NSObject
+ (void)showInfo;
@end
=========================
showInfo.m
=========================
#import "ShowInfo.h"
@implementation ShowInfo
+ (void)showInfo
{
??? NSLog(@"hello Y.X.");
}
@end
=========================
?
2.分別制作真機以及模擬器使用的靜態庫
如上圖所示,Debug-iphoneos以及Debug-iphonesimulator都有一個靜態庫文件
?
3.合并靜態庫
合并靜態庫的格式如下所示
lipo -create?/絕對路徑/libShowInfo.a??/絕對路徑/libShowInfo.a? -output?/絕對路徑/libShowInfo.a
大功告成!
?
以下為 Xcode 5.1 測試結果
無意間發現 Xcode 5.1 中的靜態庫 Search Paths 中的路徑為?$(PROJECT_DIR)?,以后再也不會出現換臺電腦后重新設置庫搜索路徑的問題了.
以下為 Xcode 5.0 測試結果
?
?
問:如果有很多文件,如何編譯成一個靜態庫文件?
如上例中,我將一個操作CoreData的很多文件打包成一個靜態庫,編譯時把需要導出的頭文件導出來即可(圖片右下部分).
?
問:為什么在使用靜態庫時報錯呢?
極有可能你的靜態庫文件中含有類目文件,就以上圖中為例,有很多的類目文件,解決方法在 Other Linker Flags 是添加 -ObjC 標簽,如下圖所示
原理解析如下?http://stackoverflow.com/questions/2567498/objective-c-categories-in-static-library/2615407#2615407
Solution:?As of Xcode 4.2, you only need to go to the application that is linking against the library (not the library itself) and click the project in the Project Navigator, click your app's target, then build settings, then search for "Other Linker Flags", click the + button, and add '-ObjC'. '-all_load' and '-force_load' are no longer needed.
Details:?I found some answers on various forums, blogs and apple docs. Now I try make short summary of my searches and experiments.
Problem was caused by (citation from apple Technical Q&A QA1490http://developer.apple.com/mac/library/qa/qa2006/qa1490.html):
Objective-C does not define linker symbols for each function (or method, in Objective-C) - instead, linker symbols are only generated for each class. If you extend a pre-existing class with categories, the linker does not know to associate the object code of the core class implementation and the category implementation. This prevents objects created in the resulting application from responding to a selector that is defined in the category.
And their solution:
To resolve this issue, the static library should pass the -ObjC option to the linker. This flag causes the linker to load every object file in the library that defines an Objective-C class or category. While this option will typically result in a larger executable (due to additional object code loaded into the application), it will allow the successful creation of effective Objective-C static libraries that contain categories on existing classes.
and there is also recommendation in iPhone Development FAQ:
How do I link all the Objective-C classes in a static library? Set the Other Linker Flags build setting to -ObjC.
and flags descriptions:
-all_load?Loads all members of static archive libraries.
-ObjC?Loads all members of static archive libraries that implement an Objective-C class or category.
-force_load (path_to_archive)?Loads all members of the specified static archive library. Note: -all_load forces all members of all archives to be loaded. This option allows you to target a specific archive.
*we can use force_load to reduce app binary size and to avoid conflicts wich all_load can cause in some cases.
Yes, it works with *.a files added to the project. Yet I had troubles with lib project added as direct dependency. But later I found that it was my fault - direct dependency projecct possibly was not added properly. When I remove it and add again with steps:
after that all works OK. "-ObjC" flag was enough in my case.
I also was interested with idea from?http://iphonedevelopmentexperiences.blogspot.com/2010/03/categories-in-static-library.htmlblog. Author say he can use category from lib without setting -all_load or -ObjC flag. He just add to category h/m files empty dummy class interface/implementation to force linker use this file. And yes, this trick do the job.
But author also said he even not instantiated dummy object. Mm… As I've found we should explicitly call some "real" code from category file. So at least class function should be called. And we even need not dummy class. Single c function do the same.
So if we write lib files as:
// mylib.hvoid useMyLib();@interfaceNSObject(Logger)-(void)logSelf;@end// mylib.mvoid useMyLib(){NSLog(@"do nothing, just for make mylib linked");}@implementationNSObject(Logger)-(void)logSelf{NSLog(@"self is:%@",[self description]);}@endand if we call useMyLib(); anywhere in App project then in any class we can use logSelf category method;
[self logSelf];And more blogs on theme:
http://t-machine.org/index.php/2009/10/13/how-to-make-an-iphone-static-library-part-1/
http://blog.costan.us/2009/12/fat-iphone-static-libraries-device-and.html
?
問:合并靜態庫時為什么會報錯?
請將你正在使用的 Xcode 的 APP 文件名字修改成Xcode.app,否則在終端合并庫文件時會報錯.
總結
以上是生活随笔為你收集整理的Xcode 5.1 编译模拟器以及真机都能使用的静态库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BZOJ 2301 Problem b(
- 下一篇: java cxf实现webserv