如何构建通用的ios静态库
我正在尝试构建一个可以与 ios3.x 和 ios4.x 一起使用的静态库。我可以使用 ios3.0 构建一个静态库,该库可以与 ios3.0 中的另一个项目一起使用,但无法在 ios4 中编译。从ios4到ios3也是如此。
重新创建的方法如下:
- 打开 XCode 3.2.4 并启动一个名为 Library4 的 Cocoa Touch 静态库的新项目
- 单击类文件夹并创建一个名为“TestViewController”的新 UIViewController
- 右键单击 Frameworks 文件夹,添加现有框架,选择 UIKit
- In在左侧面板中,展开目标,右键单击我的库目标,然后单击获取信息。更改为所有配置,将基本 sdk 更改为 iphone 模拟器 4.0,将 ios 部署目标更改为 ios3.0
- 单击 Link Library with Binaries 文件夹。在右侧窗格中,将两者的角色更改为“弱”
- 构建库
- 打开 Xcode 3.2.2 并启动一个名为 Library4Test 的新的基于视图的应用程序
- 将 TestViewController.h 复制到类文件夹
- 将 libLibrary4.a 文件复制到框架文件夹。它会自动添加到目标的链接器阶段
- 右键单击 Library4Test 目标,然后单击获取信息。在其他链接器标志中,添加“-ObjC”和“-all_load”
- 在应用程序委托标头中添加导入“TestViewController.h”
在应用程序委托标头的应用程序的 didFinishLaunchingWithOptions 方法中添加
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
I'm trying to build a static library that I can use with both ios3.x and ios4.x. I can build a static library with ios3.0 that works with another project in ios3.0 but won't compile in ios4. The same is true going from ios4 to ios3.
Here's how to recreate:
- Open XCode 3.2.4 and start a new project that's a Cocoa Touch Static Library called Library4
- Click on the classes folder and create a new UIViewController named "TestViewController"
- Right click on Frameworks folder, add existing framework, pick UIKit
- In the left panel, expand targets, right-click on my library target and click get info. Change to all configurations, Change the base sdk to iphone simulator 4.0, change ios deployment target to ios3.0
- Click on the Link Library with Binaries folder. In the right pane, change the roles of both to "Weak"
- Build the Library
- Open Xcode 3.2.2 and start a new View based application called Library4Test
- Copy TestViewController.h to the classes folder
- Copy libLibrary4.a file to the frameworks folder. It automatically gets added to the linker phase of the target
- Right-click on the Library4Test Target and click get info. In other linker flags, add "-ObjC" and "-all_load"
- In the app delegate header add Import "TestViewController.h"
IN the app delegate header's application's didFinishLaunchingWithOptions method add
TestViewController *test = [TestViewController alloc] init;
Compile with ios3.0 simulator
When I compile I get:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不太确定,但它看起来像是一个链接问题(所有这些 .o 的东西)。以下是我设置 iOS 静态库的方法。这是一个非常简单的事情,但是很有效。
部署后处理 是(检查)
安装构建
产品位置/
构建您的静态库。默认情况下,它将构建在 /usr/local/lib 中
现在创建一个符号链接以便轻松访问新库。一个简单的方法是打开终端并运行以下命令:
现在打开要在其中使用库的 Xcode 项目。创建一个名为“库”或类似名称的组,按住 Ctrl 键单击并使用“添加现有文件”来添加库。它将被称为类似 libYourLibrary.a 的名称
当您运行项目时,您会收到链接错误。因此,双击项目文件,转到“构建>>”所有配置并将以下值添加到“库搜索路径”设置:~/lib
Not sure exactly, but it looks like a linking problem (all those .o things). Here's how I set up my iOS static library. It's a very simple thing, but it works.
Deployment Postprocessing YES (check)
Installation Build
Products Location /
Build your static library. By default it will build in /usr/local/lib
Now create a symbolic link for easy access to your new library. An easy way is to open your terminal and run the following commands:
Now open the Xcode project in which you want to use your library. Create a group called Libraries or something like that, ctrl-click and use 'Add existing files' to add the library. It will be called something like libYourLibrary.a
When you run your project, you'll get a linking error. So double-click the project file, go to Build >> All configurations and add the following value to the "Library Search Paths" setting: ~/lib
你如何通过sdk创建一个目标?我为 iOS-OSx 创建了一个静态库,过程要简单得多。
当然,我必须
在代码中添加一些。您可以添加自己的条件。
Way you just create one target by sdk? I create a static library for iOS-OSx and the procedure was much more simple.
Of course i must add some
to the code. You may add you own conditionals.
发生编译器错误是因为您没有链接到正确的系统框架。所有深红色符号均来自 Foundation.framework、UIKit.framework 和 libobjc.dylib。
静态库不会自动引入它们需要链接的所需框架,因此当您使用它们时,您必须自己将它们添加到项目中。
The compiler errors are happening because you didn't link against the right system frameworks. All of the symbols in dark red are from Foundation.framework, UIKit.framework and libobjc.dylib.
Static libraries don't automatically pull in the required frameworks that they need to link against, so when you use them you must add them to the project yourself.