`

Objective-C categories in static library

    博客分类:
  • ios
 
阅读更多

from:http://stackoverflow.com/questions/2567498/objective-c-categories-in-static-library

 

Question:Can you guide me how to properly link static library to iphone project. I use staic library project added to app project as direct dependency (target -> general -> direct dependecies) and all works OK, but categories. A category defined in static library is not working in app.

So my question is how to add static library with some categories into other project?

 

And in general, what is best practice to use in app project code from other projects?

================================================================================

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 fromhttp://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

分享到:
评论

相关推荐

    Objective-C语言入门教程&深入浅出,理论实践相结合.pdf

    Objective-C语言入门教程&深入浅出,理论实践相结合 Objective-C语言入门教程&深入浅出,理论实践相结合 Objective-C语言入门教程&深入浅出,理论实践相结合 Objective-C语言入门教程&深入浅出,理论实践相结合 ...

    Objective-C基础教程(入门教程).pdf

    Objective-C是一种广泛用于苹果平台应用程序开发的编程语言,它是C语言的一个超集,并加入了Smalltalk风格的消息传递机制。本篇教程主要面向初学者,介绍了Objective-C的基础知识点和一些核心概念。 1. Objective-C...

    windows 下搭建 Objective-C 开发环境

    ### 在Windows下搭建Objective-C开发环境 随着移动应用开发的普及,越来越多的开发者开始尝试不同的编程语言和技术栈。尽管Objective-C主要与Apple的平台(如iOS和macOS)相关联,但有时候,出于某些原因(比如团队...

    Objective-c语言学习-快速入门完整版完整版

    Objective-C是在C语言基础上扩展的,引入了Smalltalk的面向对象特性,如消息传递机制,使得代码更加灵活和可扩展。 "第二讲 数据类型、运算符和表达式"深入讲解了基本的数据类型,包括整型、浮点型、字符型等,以及...

    Objective-C Runtime测试代码

    Objective-C语言的许多决策可以在编译和运行时执行。只要有可能,它是动态的。这意味着Objective-C语言不仅需要一个编译器,还需要一个运行时系统来执行编译的代码。Runtime系统是一种用于Objective-C语言的操作系统...

    Objective-C 2.0 运行时系统编程指南

    ### Objective-C 2.0 运行时系统编程指南 #### 概述 Objective-C是一种面向对象的编程语言,它以其独特的动态性而闻名。这种动态性体现在它能够尽可能地将决策过程推迟到运行时执行,而不是在编译或链接阶段。因此...

    Objective-C高级编程 iOS与OS X多线程和内存管理_Objective-C_ios_

    《Objective-C高级编程:iOS与OS X多线程和内存管理》是一本深入探讨Apple平台开发中的关键技术的书籍。本书主要围绕Objective-C语言在iOS和OS X操作系统上的应用,特别是针对多线程和内存管理这两个核心主题进行...

    Objective-C编程全解最新版.pdf.zip

    1. **Objective-C基础**:Objective-C是在C语言基础上扩展的,因此,理解C语言的基本语法是必要的。Objective-C添加了消息传递机制、类和协议等面向对象特性。 2. **消息传递**:Objective-C中的对象通过发送消息来...

    C语言主要知识点巩固(学习Objective-C 的前提)

    C语言是计算机编程的基础,它是Objective-C的重要基石。在学习Objective-C之前,深入理解C语言的知识点至关重要。C语言以其高效、简洁和灵活性被广泛应用于系统编程、嵌入式开发、游戏引擎等领域。本资料主要涵盖了...

    Objective-C语言教程

    希望这个简单的Objective-C语言教程能够为你提供一个入门的起点。Objective-C是一种面向对象的编程语言,广泛应用于Mac OS和iOS开发。通过学习Objective-C的基本语法、类和对象、控制流程和方法等内容,你将能够编写...

    Objective-C与Objective-C++的混用代码示例

    Objective-C是基于C语言的,主要用于构建iOS和macOS应用程序,而Objective-C++则是Objective-C的一个扩展,它引入了C++的特性,使得开发者可以同时利用Objective-C的动态特性和C++的面向对象编程能力。 ### ...

    用Objective-C语言实现了各种设计模式,收集各种例子,方便大家学习和普及设计模式 .zip

    用Objective-C语言实现了各种设计模式,收集各种例子,方便大家学习和普及设计模式。.zip用Objective-C语言实现了各种设计模式,收集各种例子,方便大家学习和普及设计模式。.zip用Objective-C语言实现了各种设计...

    Objective-C程序设计 第6版 pdf

    1. **Objective-C基础**:Objective-C是在C语言基础上扩展的,因此它包含了C的所有特性。书中会讲解基本的数据类型、控制结构、函数和指针等基础知识,同时介绍Objective-C特有的动态类型和消息传递机制。 2. **...

    《Objective-C基础教程》中文版(含源代码)

    《Objective-C基础教程》是一本面向初学者的编程书籍,主要涵盖了Objective-C语言的基本概念、语法和编程实践,尤其适合那些想要踏入iOS应用开发领域的学习者。Objective-C是Apple公司开发的面向对象的编程语言,它...

    Objective-C2.0程序设计第二版中文版及代码

    1. **Objective-C基础**:Objective-C是在C语言基础上扩展的,添加了面向对象特性。它的基础包括类、对象、消息传递等概念。类定义了对象的属性和行为,对象则是类的实例。消息传递是Objective-C的核心,通过`...

    Objective-C的语法与Cocoa框架.pdf

    Objective-C语法的基石是C语言的语法结构,这意味着所有的C语言代码都可以在Objective-C中直接使用。但是,Objective-C在C的基础上新增了消息传递机制,这是它与C语言的一个重要区别。在Objective-C中,方法调用是...

    Objective-C程序设计(第4版)(全球最畅销Objective-C编程书籍)

    《Objective-C 程序设计(第4版)》作者假设读者没有面向对象程序语言或者C语言(Objective-C基础)编程经验,因此,初学者和有经验的程序员都可以使用这本《Objective-C 程序设计(第4版)》学习Objective-C。...

    Objective-C.2.0 中文版.pdf & Objective-C.chm

    这个压缩包包含两个关于Objective-C的学习资源:"Programming In Objective-C.chm" 和 "Programming in Objective-C.2.0 中文版.pdf"。这两个文件分别以CHM(Microsoft帮助文档格式)和PDF(便携式文档格式)呈现,...

    这是一个基于Objective-C语言的基础案例集 旨在用于给初学者快速了解Objective-C语言的语法 .zip

    6. categories:Objective-C的类别机制允许向已存在的类添加新的方法,而无需修改原有代码,这是Objective-C的一个强大特性。 7. blocks:Blocks是Objective-C的匿名函数,可以捕获和存储当前作用域内的变量。它们...

Global site tag (gtag.js) - Google Analytics