Iphone 如何构建通用ios静态库

Iphone 如何构建通用ios静态库,iphone,xcode,ios,universal-binary,static-libraries,Iphone,Xcode,Ios,Universal Binary,Static Libraries,我正在尝试构建一个静态库,可以与ios3.x和ios4.x一起使用。我可以用ios3.0构建一个静态库,该库与ios3.0中的另一个项目一起工作,但不会在ios4中编译。从ios4到ios3也是如此 以下是如何重新创建: 打开XCode 3.2.4并启动一个新项目,即名为Library4的Cocoa Touch静态库 单击classes文件夹并创建一个名为“TestViewController”的新UIViewController 右键单击框架文件夹,添加现有框架,选择UIKit 在左侧面板中,

我正在尝试构建一个静态库,可以与ios3.x和ios4.x一起使用。我可以用ios3.0构建一个静态库,该库与ios3.0中的另一个项目一起工作,但不会在ios4中编译。从ios4到ios3也是如此

以下是如何重新创建:

  • 打开XCode 3.2.4并启动一个新项目,即名为Library4的Cocoa Touch静态库
  • 单击classes文件夹并创建一个名为“TestViewController”的新UIViewController
  • 右键单击框架文件夹,添加现有框架,选择UIKit
  • 在左侧面板中,展开目标,右键单击我的库目标,然后单击获取信息。更改为所有配置,将基本sdk更改为iphone simulator 4.0,将ios部署目标更改为ios3.0
  • 单击带有二进制文件的链接库文件夹。在右侧窗格中,将两者的角色更改为“弱”
  • 建图书馆
  • 打开Xcode 3.2.2并启动一个名为Library4Test的新的基于视图的应用程序
  • 将TestViewController.h复制到classes文件夹
  • 将libLibrary4.a文件复制到frameworks文件夹。它会自动添加到目标的链接器阶段
  • 右键单击Library4测试目标,然后单击获取信息。在其他链接器标志中,添加“-ObjC”和“-all_load”
  • 在应用程序代理标题中添加导入“TestViewController.h”
  • 在应用程序委托标头的应用程序的didFinishLaunchingWithOptions方法add中

    TestViewController*test=[TestViewController alloc]init

  • 使用ios3.0模拟器编译

  • 当我编译时,我得到:

    Ld build/Debug-iphonesimulator/library4Test.app/library4Test normal i386
    cd /Users/test/Documents/Testing/library4Test
    setenv MACOSX_DEPLOYMENT_TARGET 10.5
    setenv PATH "/Developer/GrandpaIPhone/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/GrandpaIPhone/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Developer/GrandpaIPhone/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -arch i386 -isysroot /Developer/GrandpaIPhone/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.3.sdk -L/Users/test/Documents/Testing/library4Test/build/Debug-iphonesimulator -L/Users/test/Documents/Testing/library4Test -F/Users/test/Documents/Testing/library4Test/build/Debug-iphonesimulator -filelist /Users/test/Documents/Testing/library4Test/build/library4Test.build/Debug-iphonesimulator/library4Test.build/Objects-normal/i386/library4Test.LinkFileList -mmacosx-version-min=10.5 -framework Foundation -framework UIKit -framework CoreGraphics -llibrary4_1 -o /Users/test/Documents/Testing/library4Test/build/Debug-iphonesimulator/library4Test.app/library4Test
    
    Undefined symbols:
    "_objc_msgSendSuper2", referenced from:
    -TestViewController didReceiveMemoryWarning in liblibrary4_1.a(TestViewController.o)
    -TestViewController viewDidUnload in liblibrary4_1.a(TestViewController.o)
    -TestViewController dealloc in liblibrary4_1.a(TestViewController.o)
    "__objc_empty_vtable", referenced from:
    _OBJC_METACLASS_$_TestViewController in liblibrary4_1.a(TestViewController.o)
    _OBJC_CLASS_$_TestViewController in liblibrary4_1.a(TestViewController.o)
    "_OBJC_CLASS_$_UIViewController", referenced from:
    _OBJC_CLASS_$_TestViewController in liblibrary4_1.a(TestViewController.o)
    "_OBJC_METACLASS_$_UIViewController", referenced from:
    _OBJC_METACLASS_$_TestViewController in liblibrary4_1.a(TestViewController.o)
    ".objc_class_name_TestViewController", referenced from:
    literal-pointer@__OBJC@__cls_refs@TestViewController in library4_1os3TestAppDelegate.o
    "_OBJC_METACLASS_$_NSObject", referenced from:
    _OBJC_METACLASS_$_TestViewController in liblibrary4_1.a(TestViewController.o)
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    

    不太确定,但它看起来像是一个链接问题(所有那些.o的东西)。下面是我如何设置iOS静态库的。这是一件非常简单的事情,但它是有效的

  • 启动一个新项目并选择iOS库>>Cocoa Touch静态库
  • 添加一些类。我的是UIView上的类别,它们提供了更好的描述,以便在NSLog中使用
  • 选择目标并确保将每个类的角色设置为适当的值。对于我的类别,应该是“公共”的
  • 双击目标并选择Build选项卡
  • 选择配置版本并设置以下生成设置:
  • 部署位置是(选中)
    部署后处理是(检查)
    安装构建 产品定位/
  • 构建静态库。默认情况下,它将构建在/usr/local/lib中

    现在创建一个符号链接,以便轻松访问新库。一种简单的方法是打开终端并运行以下命令:

    cd ~    
    ln -s /usr/local/lib
    
    现在打开要在其中使用库的Xcode项目。创建一个名为Libraries或类似的组,按住ctrl键并单击并使用“添加现有文件”添加库。它将被称为类似libYourLibrary的东西
    运行项目时,将出现链接错误。因此,双击项目文件,转到Build>>All configurations并将以下值添加到“Library Search path”设置中:~/lib

    通过sdk创建一个目标的方式?我为iOS OSx创建了一个静态库,过程简单得多

  • 创建项目(从iOS模板“Cocoa touch静态库”)
  • 添加源文件
  • 为sdk添加一个目标
  • 当然,我必须补充一些

    #if TARGET_OS_IPHONE
    ...
    #else
    ...
    #endif
    

    遵守守则。您可以添加自己的条件。

    编译器错误的发生是因为您没有链接到正确的系统框架。暗红色中的所有符号都是从基础框架、UIKIT.Frand和LIBOBJC.DyLIB


    静态库不会自动引入它们需要链接的所需框架,因此当您使用它们时,您必须自己将它们添加到项目中。

    对于只想构建静态IOS库的人:我刚刚创建了一个问题,您可能会在这里回答,但我不确定。这是: