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 QA1490 http://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.html blog. 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,否则在终端合并库文件时会报错.

Xcode 5.1 编译模拟器以及真机都能使用的静态库的更多相关文章

  1. Android 如何添加一个apk使模拟器和真机都编译进去 m

    添加一个apk都需要将LOCAL_PACKAGE_NAME的值添加到PRODUCT_PACKAGES才行.而PRODUCT_PACKAGES一般在build/target/product/目录下的文件 ...

  2. iOS 静态库中使用宏定义区分iPhone模拟器与真机---备用

    问题描述 一般项目中,可以使用宏定义来判断模拟器还是真机,这无疑是有效的. #if TARGET_IPHONE_SIMULATOR #define SIMULATOR 1 #elif TARGET_O ...

  3. Appium移动端自动化测试--搭建模拟器和真机测试环境

    详细介绍安装Android Studio及Android SDK.安装Appium Server. 文章目录如下 目录 文章目录如下 模拟器--安装Android Studio及Android SDK ...

  4. android应用程序monkey压力测试(模拟器或真机)

    首先需要安装一个模拟器: 前置条件: 1.jdk环境配置 2.eclipse下载安装(直接解压即可) 3.网站上下载ADT: 由于国内禁止google的浏览,所以需要自己上网找资源,下面这个网站有比较 ...

  5. 利用lipo编译合并iPhone模拟器和真机通用的静态类

    利用lipo编译合并iPhone模拟器和真机通用的静态类 如何编译静态类库,而且现在网上也有很多的教程,现在问题时我们编译好了的静态类库会时两个版本的.a文件,分别用于模拟器和iPhone真迹,因此M ...

  6. 判断IOS静态库(.a文件)是否支持模拟器和真机运行

    判断IOS静态库(.a文件)是否支持模拟器和真机运行 在mac终端下,进入到.a文件目录下,然后输入: lipo -info libMyAlertView.a Architectures in the ...

  7. 合并模拟器和真机的静态库动态库aggregate

    创建Aggregate的target 在Build Phases 添加Run Script,内容为 scriptFile=${SRCROOT}/universalA.shsh ${scriptFile ...

  8. ios framework 分离与合并多种CPU架构,分离与合并模拟器与真机

    ios  framework 分离与合并多种CPU架构,分离与合并模拟器与真机 如果你所用的framework支持真机和模拟器多种CPU架构,而你需要的是其中的一种或几种,那么可以可以从framewo ...

  9. [转] iOS开发之使用lipo命令制作模拟器与真机通用静态库

    转自 http://blog.csdn.net/jinglijun/article/details/8276089 通常在项目中使用静态库的时候都会有两个版本,一个用于模拟器,一个用于真机,因为Mac ...

随机推荐

  1. lintcode-->翻转字符串

    给定一个字符串,逐个翻转字符串中的每个单词. 您在真实的面试中是否遇到过这个题? Yes 说明 单词的构成:无空格字母构成一个单词 输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包 ...

  2. antlr4 接触

    1. 配置IDE(可选) 2. 加减法示例 编写expr.g4 grammar AddMinus; expr: Minus Number #minusNum | expr op=('+'|'-') e ...

  3. 虚拟化明星——深挖轻量级容器docker

    docker是一个轻量级容器,属于操作系统层面的虚拟化技术,封装了文件系统(AUFS)以及网络互联,进程隔离等特性. 传统虚拟化架构: docker虚拟化架构: 可以看出,docker是没有Guest ...

  4. mysql中难以理解的sql

    工作中遇到这样的例子, CASE type WHEN 1 THEN '普通红包' WHEN 2 THEN '普通礼包加油卡' WHEN 3 THEN '优 惠码兑换加油卡' WHEN 4 THEN ' ...

  5. Redis运行流程源码解析--转载

    http://blog.nosqlfan.com/html/4007.html http://www.searchdatabase.com.cn/showcontent_62166.htm 导读:本文 ...

  6. Sass进阶之路,之一(基础篇)

    Sass 学习Sass之前,应该要知道css预处理器这个东西,css预处理器是什么呢? Css预处理器定义了一种新的语言将Css作为目标生成文件,然后开发者就只要使用这种语言进行编码工作了.预处理器通 ...

  7. Ionic APP 热更新

    开门见山,本文主题:cordova-hot-code-push 作用:cordova热更新插件,提供了在应用程序中对基于Web的内容进行自动更新的功能. GitHub地址:https://github ...

  8. DedeCMS修改管理员用户名

    织梦内容管理系统(DedeCMS) 以简单.实用.开源而闻名,是国内最知名的PHP开源网站管理系统,也是使用用户最多的PHP类CMS系统,在经历多年的发展,目前的版本无论在功能,还是在易用性方面,都有 ...

  9. automapper 自动映射 集成asp.net Core2.1

    学习博文:https://www.cnblogs.com/yan7/p/8085410.html 1.使用nuget 安装 <PackageReference Include="Aut ...

  10. MyEclipse在不同编辑面间快速切换

    想在下面的编辑页面间切换有两种方式,一种是按“栈”方式切换,一种是按“链表”方式切换. 按栈方式切换: Alt + 左箭头: 上一次编辑页面 Alter + 右箭头 : 下一个编辑也页面 按链表(绝对 ...