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:

  1. Drag&drop lib project file in app project (or add it with Project->Add to project…).
  2. Click on arrow at lib project icon - mylib.a file name shown, drag this mylib.a file and drop it into Target -> Link Binary With Library group.
  3. Open target info in fist page (General) and add my lib to dependencies list

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]);}@end

and 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,否則在終端合并庫文件時會報錯.

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/458804.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/458804.shtml
英文地址,請注明出處:http://en.pswp.cn/news/458804.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

UVALive 6511 Term Project

Term Project Time Limit: 3000msMemory Limit: 131072KBThis problem will be judged on UVALive. Original ID: 651164-bit integer IO format: %lld Java class name: Main解題&#xff1a;強連通分量 1 #include <bits/stdc.h>2 using namespace std;3 const in…

MATLAB混合編程視頻教程下載 SIMULINK系統仿真視頻

下載鏈接&#xff1a; http://item.taobao.com/item.htm?id43401674106 精通MATLAB混合編程視頻講解 MATLAB各類函數視頻講解 基于MATLAB的高等數學問題求解 MATLAB函數速查視頻講解 面向對象C視頻教程 五朵金花&#xff0c;帶你輕松搞定MATLAB 金花詳情&#xff1a; 精通MA…

css z-index

為什么80%的碼農都做不了架構師&#xff1f;>>> css z-index 最近在發現z-index在position"relative"的元素中會失效 去掉position屬性就正常&#xff1a;z-index1 出現在 z-index999之上 記錄一下供以后查看 轉載于:https://my.oschina.net/livend/blog…

用c++寫的一個詞典工具

使用的QT圖形界面&#xff0c;用libcurl獲取的網頁&#xff0c;在之中遇見了很多問題&#xff0c;一直想用c類封裝一下libcurl,發現c很不到家啊。索性用了友元函數。 先貼上代碼吧 main.cpp #include <stdio.h> #include <stdlib.h> #include <unistd.h>#in…

VC++中Format用法

Format是一個很常用&#xff0c;卻又似乎很煩的方法&#xff0c;本人試圖對這個方法的幫助進行一些翻譯&#xff0c;讓它有一個完整的概貌&#xff0c;以供大家查詢之用&#xff1a;首先看它的聲明&#xff1a;function Format(const Format: string; const Args: array of con…

為什么NX10幫助功能無法找到HTML,NX10.0 新功能介紹視頻教程專輯

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓NX10.0 新功能介紹視頻教程專輯PLM之家NX10.0界面基礎新功能--1 NX新界面增強.mp4PLM之家NX10.0界面基礎新功能--2 問題管理功能增強.mp4PLM之家NX10.0界面基礎新功能--3 資源工具條開發API接口.mp4PLM之家NX10.0界面基礎新功能--4…

SVN,桌面右擊-settings-Icon Overlays 選擇 None

SVN&#xff0c;桌面右擊->settings->Icon Overlays 選擇 None 轉載于:https://www.cnblogs.com/yu520zhong/p/4857324.html

模擬實現strstr

今天我們來模擬實現strstr這個函數&#xff0c;首先我們先來了解一下這個函數&#xff0c;這個函數原型為char *strstr(char *str1,char *str2)&#xff0c;功能是&#xff1a;從字符串str1中查找是否有符串str2&#xff0c;若str2是str1的子串&#xff0c;則先確定str2在str1的…

股票歷史數據的獲取

從 Yahoo 獲取 歷史數據 HTML Output: http://finance.yahoo.com/q/hp?s300072.sz&d7&e23&f2010&a5&b11&c2010CSV Output: http://ichart.finance.yahoo.com/table.csv?s300072.sz&d7&e23&f2010&a5&b11&c2010由于歷史原因…

html5證書,1+X證書Web前端開發HTML專項練習

1 . HTML5 之前的 HTML 版本是&#xff1f; A.HTML 4.01 B.HTML 4 C.HTML 4.1 D.HTML 4.9 2 . HTML5 的正確 doctype 是&#xff1f; A.B.C.3 . 在 HTML5 中&#xff0c;哪個元素用于組合標題元素&#xff1f;A.B.C.D.4 . HTML5 中不再支持下面哪個元素&#xff1f;A.B.C.D.5 .…

PHP中封裝mysql數據庫鏈接(簡單版)

為什么80%的碼農都做不了架構師&#xff1f;>>> 1&#xff0c;封裝mysql數據鏈接需要哪些屬性 主機地址&#xff0c;數據庫名&#xff0c;數據庫密碼&#xff0c;數據庫名&#xff0c;字符集 2&#xff0c;利用構造方法在實例化類時&#xff0c;php底層會自動執行的…

c++異常處理機制

c異常處理機制 C異常處理機制是一個用來有效地處理運行錯誤的非常強大且靈活的工具&#xff0c;它提供了更多的彈性、安全性和穩固性&#xff0c;克服了傳統方法所帶來的問題. 異常的拋出和處理主要使用了以下三個關鍵字&#xff1a; try、 throw 、 catch 。 拋出異常即檢測…

html標簽api,html5新增標簽+API介紹

新增標簽1、embedembed標簽是html5新增的標簽&#xff0c;用來嵌入內容&#xff0c;比如插件等&#xff0c;常用于視頻文件的播放(為外部應用程序定義容器)。這篇文章介紹了embed的常用屬性&#xff0c;基本都是定義播放器的一些設置的&#xff0c;自動播放loop、開始時間start…

UVA10404

1 //題意&#xff1a;S 和 O 二人玩游戲 。N顆石頭&#xff0c;M個數&#xff0c;每次拿 M[i]個石頭&#xff08;i>0 && i<m) ,誰拿到最后一個誰贏。 2 3 // 定義f[i]為還剩i顆石頭時的勝負情況 &#xff0c;若S勝利&#xff0c;則記 1 &#xff0c;4 #include&…

java中定義一個CloneUtil 工具類

其實所有的java對象都可以具備克隆能力&#xff0c;只是因為在基礎類Object中被設定成了一個保留方法&#xff08;protected),要想真正擁有克隆的能力&#xff0c; 就需要實現Cloneable接口&#xff0c;重寫clone方法。通過克隆方法得到的對象是一個本地的副本。 1、實現Clonea…

C++異常以及錯誤處理

計算機應用程序中離不開錯誤處理&#xff0c;尤其是生產型大型軟件系統。應用軟件系統運行屬于循環處理事務&#xff0c;出錯后需要保證不能讓軟件程序直接退出。這就需要使用一定的程序容錯處理來應對。一般情況下&#xff0c;大型軟件開發中的軟件系統容錯處理會結合異常處理…

家用臺式計算機的額定功率,臺式機的功率(臺式電腦電源功率多大才合適?)...

臺式機的功率(臺式電腦電源功率多大才合適&#xff1f;)組裝一臺電腦&#xff0c;我們需要先挑選好硬件&#xff0c;搭配硬件最關鍵點就是CPU和主板兼容性&#xff0c;硬件之間的均衡性、電源功率等&#xff0c;均需要考慮周到。那么臺式電腦電源功率多大才合適&#xff1f;下面…

關于angularjs input上傳圖片前獲取圖片的Size 淺析

首先我們需要一個指令來追蹤input的change。ngChage不適用input[file]。 app.directive("fileread", [function () { return { scope: { selectedFile: "", changed: & }, link: function(scope, element, attributes) { element.bind("change&qu…

CountDownLacth詳解

一個同步輔助類&#xff0c;在完成一組正在其他線程中執行的操作之前&#xff0c;它允許一個或多個線程一直等待。 用給定的計數 初始化 CounDownLatch。由于調用了countDown() 方法&#xff0c;所以在當前計數到達零之前,await()方法會一直受阻塞。之后&#xff0c;會釋放所有…

怎么用計算機畫正弦函數圖像,幾何畫板如何繪制正弦函數圖象

考慮到三角函數問題描點的實際困難&#xff0c;教材表述時借助正弦線利用幾何法利用三角函數線作正弦函數圖象&#xff0c;但由此帶來的困難是如何實現這種效果。如果能讓三角函數線動起來&#xff0c;那將會更加直觀易懂。幾何畫板作為使用專業的幾何繪圖軟件&#xff0c;自帶…