`

Building Objective-C static libraries with categories

    博客分类:
  • ios
 
阅读更多

 

Q:  Why do I get a runtime exception of "selector not recognized" when linking against an Objective-C static library that contains categories?

A: Why do I get a runtime exception of "selector not recognized" when linking against an Objective-C static library that contains categories?

The "selector not recognized" runtime exception occurs due to an issue between the implementation of standard UNIX static libraries, the linker and the dynamic nature of Objective-C. 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.

To resolve this issue, the target linking against the static library must 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. Follow these steps to pass -ObjC to the linker:

  1. In Xcode, double-click the target's name under "Targets" in the Project window.

  2. Choose the Build pane from the ensuing Info window.

  3. Scroll down to the Other Linker Flags build setting under the Linking collection and set its value to -ObjC.

Figure 1  Target Build pane: Other Linker Flags

Important: For 64-bit and iPhone OS applications, there is a linker bug that prevents -ObjC from loading objects files from static libraries that contain only categories and no classes. The workaround is to use the -all_load or -force_load flags.

 

-all_load forces the linker to load all object files from every archive it sees, even those without Objective-C code. -force_load is available in Xcode 3.2 and later. It allows finer grain control of archive loading. Each -force_load option must be followed by a path to an archive, and every object file in that archive will be loaded.

转载:http://developer.apple.com/library/mac/#qa/qa1490/_index.html

分享到:
评论

相关推荐

    windows 下搭建 Objective-C 开发环境

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

    iOS开发Objective-C项目工程混淆脚本.zip

    "iOS开发Objective-C项目工程混淆脚本.zip"这个压缩包很可能包含了一套用于混淆Objective-C代码的工具或脚本。Objective-C是苹果平台上的主要编程语言,其语法特性使得混淆相对Java或其他静态类型语言更为复杂。 ...

    windows平台搭建Objective-C环境

    在Windows平台上搭建Objective-C开发环境是一项相对复杂但可行的任务,主要依赖于GNUstep项目,它是一个开源实现的Objective-C环境,旨在提供与Apple的Mac OS X上的Cocoa类似的功能。以下是一个详细的步骤指南: 1....

    SWT64位( Cannot load 32-bit SWT libraries on 64-bit JVM)完美解决

    把原来的swt.jar替换成这个就能解决 Cannot load 32-bit SWT libraries on 64-bit JVM问题

    JDK 12 Documentation - Java Core Libraries Developer Guide

    JDK 12 Documentation - Java Core Libraries Developer Guide JDK 12 Documentation - Java Core Libraries Developer Guide JDK 12 Documentation - Java Core Libraries Developer Guide JDK 12 Documentation - ...

    Android-android-ui-animation-components-and-libraries.zip

    Android-android-ui-animation-components-and-libraries.zip,android ui库、组件和动画作者@ramotion-...操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有安全性优势。

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

    Objective-C是在C语言的基础上添加了面向对象的特性,使得它可以作为一种类C语言来使用。Objective-C与C++类似,但采用了不同的语法实现面向对象编程。 #### 二、Objective-C的基本语法 ##### 1. 类定义 ...

    Objective-C头文件导出工具class-dump

    Objective-C头文件导出工具class-dump 。 Current version: 3.4 (64 bit Intel) Requires Mac OS X 10.7 or later. class-dump 3.4 (64 bit) Usage: class-dump [options] <mach-o-file> where options are: -a ...

    Objective-C的语法与Cocoa框架

    ### Objective-C的语法与Cocoa框架 #### 一、Objective-C简介 Objective-C是一种通用、面向对象的编程语言,它是苹果公司 macOS 和 iOS 操作系统的主要开发语言。Objective-C 是在 C 语言的基础上添加了面向对象的...

    swt.jar 64-bit 解决Cannot load 32-bit SWT libraries on 64-bit JVM

    完美解决Cannot load 32-bit SWT libraries on 64-bit JVM问题

    Win10利用CodeBlocks搭建Objective-C开发环境(一).pdf

    ### Win10利用CodeBlocks搭建Objective-C开发环境详解 #### 一、前言 随着iOS应用的日益流行,越来越多的开发者希望能够快速上手Objective-C语言。然而,并非所有人都能拥有苹果设备来直接进行开发。因此,在...

    Eagle开源库文件 SparkFun-Eagle-Libraries

    Eagle开源库文件“SparkFun-Eagle-Libraries”是一个由知名电子元件供应商SparkFun公司提供的资源,用于支持Eagle设计软件。Eagle(Electronic Assistant for Layout and Schematic)是一款广泛使用的电路板设计软件...

    搭建Linux的Objective-C开发环境

    ### 搭建Linux下的Objective-C开发环境 #### 背景介绍 Objective-C是一种通用、面向对象的编程语言,最初由Next公司开发,并被广泛应用于Mac OS X和iOS平台的应用开发。对于那些没有苹果设备但又想学习或实践...

    Windows下进行Objective-c开发.docx

    在Windows环境下进行Objective-C开发可能对许多初学者来说是一个挑战,因为Objective-C通常是与Apple的macOS和iOS操作系统紧密关联的。然而,通过使用GNUstep,一个开源的Objective-C框架,开发者可以在Windows上...

    Windows XP 系统下创建 Objective-C 集成开发环境

    ### Windows XP 系统下创建 Objective-C 集成开发环境 #### 概述 本文旨在详细介绍如何在 Windows XP 操作系统中搭建一套基于 GNUstep 和 CodeBlocks 的 Objective-C 集成开发环境(IDE)。该环境对于希望在 ...

    CGAL-5.2-win64-auxiliary-libraries-gmp-mpfr--CGAL计算几何算法库.zip

    CGAL或计算几何算法库是一个C ++库,可让您轻松访问各种高效而可靠的几何算法。这些算法可用于广泛的应用程序,包括计算机辅助设计,机器人技术,分子生物学,医学成像,地理信息系统等。 CGAL具有广泛的数据结构和...

Global site tag (gtag.js) - Google Analytics