xcode 如何构建通用的ios静态库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4006574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:52:13  来源:igfitidea点击:

How to build universal ios static library

iphonexcodeiosuniversal-binarystatic-libraries

提问by Julie

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.

我正在尝试构建一个可以与 ios3.x 和 ios4.x 一起使用的静态库。我可以使用 ios3.0 构建一个静态库,该库可与 ios3.0 中的另一个项目一起使用,但无法在 ios4 中编译。从 ios4 到 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

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

    TestViewController *test = [TestViewController alloc] init;

  13. 用ios3.0模拟器编译

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

回答by Elise van Looij

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.

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

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

Build your static library. By default it will build in /usr/local/lib

构建您的静态库。默认情况下,它将在 /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 projectfile, go to Build >> All configurations and add the following value to the "Library Search Paths" setting: ~/lib

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

回答by Bruno Berisso

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

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

  1. create the project (from iOS template "Cocoa touch Static Library")
  2. add the source files
  3. add one target for sdk
  1. 创建项目(来自 iOS 模板“Cocoa touch 静态库”)
  2. 添加源文件
  3. 为 sdk 添加一个目标

Of course i must add some

当然我必须添加一些

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

to the code. You may add you own conditionals.

到代码。您可以添加自己的条件。

回答by Scott K.

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.

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

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