iphone - 如何构建通用ios静态库

标签 iphone xcode ios universal-binary static-libraries

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

重新创建的方法如下:

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

最佳答案

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

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

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

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

cd ~    
ln -s /usr/local/lib

现在打开您要在其中使用您的库的 Xcode 项目。创建一个名为 Libraries 或类似名称的组,按住 Ctrl 并单击并使用“添加现有文件”来添加库。它将被称为 libYourLibrary.a 当你运行你的项目时,你会得到一个链接错误。所以双击 project 文件,转到 Build >> All configurations 并将以下值添加到“Library Search Paths”设置:~/lib

关于iphone - 如何构建通用ios静态库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4006574/

相关文章:

ios - 在 iOS 9 中提取企业应用程序的 IPA

iphone - 如何使用最新的 Xcode 下载来针对旧版 iOS 进行构建?

swift - 如何将参数传递给对象函数而不发生崩溃

ios - 将图像保存到 Main Bundle 然后将它们加载到 HTML 中可以在模拟器上运行,但不能在真实设备上运行?

ios - 如何即时快速本地化 Storyboard?

ios - 如何在iOS上的React Native中设置热重载?

iphone - 如何像iOS 7 iPad App Store一样同时翻转和放大一个UIView?

iphone - 继承类中 SQLite3 和 NSDictionary 的内存泄漏?

ios - 无法进行自动布局

iphone - 当用户暂停我的应用程序时,我应该如何保存音频文件的状态?