如何构建通用的ios静态库

Jul*_*lie 8 iphone xcode static-libraries universal-binary ios

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

以下是重新创建的方法:

  1. 打开XCode 3.2.4并启动一个名为Library4的Cocoa Touch静态库的新项目
  2. 单击classes文件夹并创建一个名为"TestViewController"的新UIViewController
  3. 右键单击Frameworks文件夹,添加现有框架,选择UIKit
  4. 在左侧面板中,展开目标,右键单击我的库目标,然后单击获取信息.更改为所有配置,将基本sdk更改为iphone模拟器4.0,将ios部署目标更改为ios3.0
  5. 单击带有二进制文件的链接库文件夹.在右侧窗格中,将两者的角色更改为"Weak"
  6. 建立图书馆
  7. 打开Xcode 3.2.2并启动一个名为Library4Test的基于View的新应用程序
  8. 将TestViewController.h复制到classes文件夹
  9. 将libLibrary4.a文件复制到frameworks文件夹.它会自动添加到目标的链接器阶段
  10. 右键单击Library4Test Target并单击获取信息.在其他链接器标志中,添加"-ObjC"和"-all_load"
  11. 在app delegate header中添加Import"TestViewController.h"
  12. 在app delegate header的应用程序的didFinishLaunchingWithOptions方法中添加

    TestViewController*test = [TestViewController alloc] init;

  13. 使用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
Run Code Online (Sandbox Code Playgroud)

Eli*_*oij 2

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

  1. 开始一个新项目并选择 iOS Libarary >> Cocoa Touch Static Library
  2. 添加一些类。我的是 UIView 上的类别,它们为 NSLog 中的使用提供了更好的描述。
  3. 选择目标并确保每个类别的角色设置为适当的值。就我的类别而言,应该是“公共”。
  4. 双击目标并选择“构建”选项卡。
  5. 选择配置版本并设置以下构建设置:
  6. 部署位置 是(选中)
    部署后处理 是(选中)
    安装构建产品位置 /

构建您的静态库。默认情况下它将构建在 /usr/local/lib 中

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

cd ~    
ln -s /usr/local/lib
Run Code Online (Sandbox Code Playgroud)

现在打开您要在其中使用库的 Xcode 项目。创建一个名为“库”或类似名称的组,按住 Ctrl 键单击并使用“添加现有文件”来添加库。它将被称为 libYourLibrary.a 之类的名称。当您运行项目时,您将收到链接错误。因此,双击项目文件,转到“构建>>所有配置”,并将以下值添加到“库搜索路径”设置中:~/lib