如何构建通用的ios静态库

发布于 2024-09-29 03:29:23 字数 3126 浏览 0 评论 0原文

我正在尝试构建一个可以与 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. In在左侧面板中,展开目标,右键单击我的库目标,然后单击获取信息。更改为所有配置,将基本 sdk 更改为 iphone 模拟器 4.0,将 ios 部署目标更改为 ios3.0
  5. 单击 Link Library with Binaries 文件夹。在右侧窗格中,将两者的角色更改为“弱”
  6. 构建库
  7. 打开 Xcode 3.2.2 并启动一个名为 Library4Test 的新的基于视图的应用程序
  8. 将 TestViewController.h 复制到类文件夹
  9. 将 libLibrary4.a 文件复制到框架文件夹。它会自动添加到目标的链接器阶段
  10. 右键单击 Library4Test 目标,然后单击获取信息。在其他链接器标志中,添加“-ObjC”和“-all_load”
  11. 在应用程序委托标头中添加导入“TestViewController.h”
  12. 在应用程序委托标头的应用程序的 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

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:

  1. Open XCode 3.2.4 and start a new project that's a Cocoa Touch Static Library called Library4
  2. Click on the classes folder and create a new UIViewController named "TestViewController"
  3. Right click on Frameworks folder, add existing framework, pick UIKit
  4. 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
  5. Click on the Link Library with Binaries folder. In the right pane, change the roles of both to "Weak"
  6. Build the Library
  7. Open Xcode 3.2.2 and start a new View based application called Library4Test
  8. Copy TestViewController.h to the classes folder
  9. Copy libLibrary4.a file to the frameworks folder. It automatically gets added to the linker phase of the target
  10. Right-click on the Library4Test Target and click get info. In other linker flags, add "-ObjC" and "-all_load"
  11. In the app delegate header add Import "TestViewController.h"
  12. IN the app delegate header's application's didFinishLaunchingWithOptions method add

    TestViewController *test = [TestViewController alloc] init;

  13. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花坊 2024-10-06 03:29:23

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

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

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

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

cd ~    
ln -s /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.

  1. Start a new project and choose iOS Libarary >> Cocoa Touch Static Library
  2. Add some classes. Mine are categories on UIView that give better descriptions for use in NSLog.
  3. Select the target and make sure the Role of each class is set to an appropriate value. In the case of my categories that should be 'public'.
  4. Double-click on the target and choose the Build tab.
  5. Select the Configuration Release and set the following build settings:
  6. Deployment Location YES (check)
    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:

cd ~    
ln -s /usr/local/lib

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

兰花执着 2024-10-06 03:29:23

你如何通过sdk创建一个目标?我为 iOS-OSx 创建了一个静态库,过程要简单得多。

  1. 创建项目(来自 iOS 模板“Cocoa touch Static Library”)
  2. 添加源文件
  3. 添加 sdk 的一个目标

当然,我必须

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

在代码中添加一些。您可以添加自己的条件。

Way you just create one target by sdk? I create a static library for iOS-OSx and the procedure was much more simple.

  1. create the project (from iOS template "Cocoa touch Static Library")
  2. add the source files
  3. add one target for sdk

Of course i must add some

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

to the code. You may add you own conditionals.

ㄟ。诗瑗 2024-10-06 03:29:23

发生编译器错误是因为您没有链接到正确的系统框架。所有深红色符号均来自 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文