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.mzph.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…

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底层会自动执行的…

家用台式计算机的额定功率,台式机的功率(台式电脑电源功率多大才合适?)...

台式机的功率(台式电脑电源功率多大才合适&#xff1f;)组装一台电脑&#xff0c;我们需要先挑选好硬件&#xff0c;搭配硬件最关键点就是CPU和主板兼容性&#xff0c;硬件之间的均衡性、电源功率等&#xff0c;均需要考虑周到。那么台式电脑电源功率多大才合适&#xff1f;下面…

怎么用计算机画正弦函数图像,几何画板如何绘制正弦函数图象

考虑到三角函数问题描点的实际困难&#xff0c;教材表述时借助正弦线利用几何法利用三角函数线作正弦函数图象&#xff0c;但由此带来的困难是如何实现这种效果。如果能让三角函数线动起来&#xff0c;那将会更加直观易懂。几何画板作为使用专业的几何绘图软件&#xff0c;自带…

C#将dll打包到程序中

直接进入主题 先来看一个栗子,假设现在有一个第三方dll namespace TestLibrary1 {public class Test{public void Point(){Console.WriteLine("aaabbbccc");}} } TestLibrary1.dll在项目中引用,然后调用其中的方法Test,将输出aaabbbccc using System;namespace Conso…

Exchange 2016集成ADRMS系列-12:域内outlook 2010客户端测试

接下来&#xff0c;我们来到域内安装了office 2010的机器上进行测试。 首先我们在客户端上强制刷新组策略&#xff0c;把我们刚才设置的策略刷新下来。 然后我们可以运行gpresult /h result.html来看看策略是不是已经下来了。 策略下来之后&#xff0c;我们打开客户端上面的out…

如何用css和HTML结合画熊,结合伪元素实现的纯CSS3高级图形绘制

自小编上次整理了一些基础图形的绘制方法之后&#xff0c;大家都纷纷表示对css3的绘图技巧学习很有帮助。虽说万变不离其宗&#xff0c;再复杂的图形也可以用最简单的三角形或者圆弧组合出来&#xff0c;但仍有不少朋友反映&#xff0c;学会基本图形也不懂得怎样组合&#xff0…

新手学Struts(一)----JSP+Servlet讲解MVC实现原理

MVC基本原理一个简单的例子改良的例子Struts基本流程的实现最近在学SSH&#xff08;Struts HibernateSpring)&#xff0c;这也算是一个比较经典的框架了&#xff0c;之前都是看人家大牛说这个框架&#xff0c;那个框架&#xff0c;说的真溜&#xff0c;自己也是佩服的五体投地啊…

windows下apache+php+mysql 环境配置方法

转自&#xff1a;http://www.jb51.net/article/30128.htm 一 准备 1 下载apache http://httpd.apache.org/download.cgi#apache24 httpd-2.2.22-win32-x86-openssl-0.9.8t.msiopenssl表示带有openssl模块&#xff0c;利用openssl可给Apache配置SSL安全链接 2 下载php http://wi…

USB枚举过程分析

1. 枚举是什么? 枚举就是从设备读取一些信息&#xff0c;知道设备是什么样的设备&#xff0c;如何进行通信&#xff0c;这样主机就可以根据这些信息来加载合适的驱动程序。调试USB设备&#xff0c;很重要的一点就是USB的枚举过程&#xff0c;只要枚举成功了&#xff0c;那么就…

CATia对计算机配置要求,【2人回答】求CATIA对电脑的详细配置要求-3D溜溜网

回答&#xff1a;1、内存要求在32G和64G之间。2、硬盘要求选择7200转机械硬盘&#xff0c;4k以上分辨&#xff0c;就选择SSD固态硬盘。3、CPU要求睿频在4.1GHZ以上&#xff0c;核心在6和8核之间。4、显卡要求显存在6GB和11GB之间&#xff0c;位宽要求在192bit和384bit之间。5、…

如何制作自动更新程序?

原文出自&#xff1a;http://blog.csdn.net/metaphysis/article/details/18866631 如何制作自动更新程序&#xff1f; [版权所有 邱秋 2014 metaphysisyeah.net, 转载请注明出处] 最近为单位写了一个C/S结构的软件&#xff0c;这个软件是工作在单位的局域网内的。为了减轻为程序…

包含JS交互的混淆出错

2019独角兽企业重金招聘Python工程师标准>>> 小上司离职&#xff0c;我接手他负责的项目&#xff0c;进行版本更新的时候&#xff0c;在生成jar包测试的时候&#xff0c;点击按钮没有反应&#xff0c;页面是webview&#xff0c;按钮则是与js交互&#xff0c;logcat打…

计算机常用代码大全,常用电脑命令大全【图文】

导语 &#xff1a;电脑&#xff0c;这个在前几年貌似还离我们非常遥远的词汇&#xff0c;现在随着信息化社会的不断发展&#xff0c;已经走进了我们的千家万户。现在每一家新 装修 &#xff0c;它都作为一种必备的家用电器来使用。由此可见电脑对我们 现代 人的重要性。但电脑毕…

细数技术指标-[转载]

技术指标类别庞杂&#xff0c;要一一学全&#xff0c;基本不可能&#xff0c;也没有这个必要。我们只要掌握几个常用的指标&#xff0c;了解它们的原理&#xff0c;从而举一反三&#xff0c;就足够了。其实任何一种技术指标都是从形态、价格、量、时间这四项出发的&#xff0c;…

图论测试题(一)第一题:longest

第一题&#xff1a;longest 乌托邦有n个城市&#xff0c;某些城市之间有公路连接。任意两个城市都可以通过公路直接或者间接到达&#xff0c;并且任意两个城市之间有且仅有一条路径&#xff08;What does this imply? A tree!&#xff09;。 每条公路都有自己的长度&#xff0…