xcode 在 OS X 上创建和使用静态库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13011540/
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-15 01:53:35  来源:igfitidea点击:

Create and use static library on OS X

objective-cxcodemacoscocoastatic-libraries

提问by Dr.Kameleon

OK, I'm trying to create a Cocoa Library (static) and use, but I keep getting errors.

好的,我正在尝试创建一个 Cocoa 库(静态)并使用,但我不断收到错误消息。

I created a super-basic static Library (TSXLib) with just one additional class in it.

我创建了一个超级基本的静态库 ( TSXLib),其中只有一个额外的类。

#import <Foundation/Foundation.h>

@interface ClassOne : NSObject

- (void)doIt;

@end


#import "ClassOne.h"

@implementation ClassOne

- (void)doIt
{
    NSLog(@"Oops... I did it again!");
}

@end

Then, I set the Dynamic Library Install Name(in Build Settings) to :

然后,我将动态库安装名称(在构建设置中)设置为:

@executable_path/../Frameworks/libTSXLib.a

Now in my Test Project :

现在在我的测试项目中:

  • I drag'n'drop the libTSXLib.afile (and copied it to target)
  • Added a Build Phase (Copy Files) where I'm copying the libTSXLib.ato Frameworks
  • I'm then going to my AppDelegate.mand try importing my library's class
  • At #import <ClassOne.h>, the compiler throws an error that it can't find the class
  • 我拖放libTSXLib.a文件(并将其复制到目标)
  • 添加了一个构建阶段(复制文件),我将在其中复制libTSXLib.aFrameworks
  • 然后我要去我的AppDelegate.m并尝试导入我的图书馆的课程
  • #import <ClassOne.h>,编译器抛出一个错误,它找不到类


Any ideas?

有任何想法吗?



NOTE :I'm actually quite confused regarding libraries, frameworks, etc (that's why I tend to avoid them as much as possible). All I'm trying to do is pack some of classes/functions so that I can easily re-use them in different projects. Whether it is a framework, or a library, I really don't care. What I need is that : pack and re-use my code. (the ability to block anyone from seeing/using what's in, when bundled, would be a Plus)

注意:我实际上对库、框架等很困惑(这就是为什么我倾向于尽可能避免使用它们)。我要做的就是打包一些类/函数,以便我可以轻松地在不同的项目中重用它们。无论是框架,还是库,我真的不在乎。我需要的是:打包并重用我的代码。(阻止任何人查看/使用其中的内容的能力,当捆绑时,将是一个加号)

回答by Dr.Kameleon

OK, so, coming back after some time, here's what I did to get it working :

好的,所以,一段时间后回来,这是我为使其正常工作所做的工作:

Step 1 : Create the Library

第 1 步:创建库

  • Create a New Project, using the built-in Cocoa Library template
  • Set Library typeas Static.
  • Add your Classes/Functions/Whatever
  • Under Build Phases, take care of which headers are going to go Public.
  • 使用内置的 Cocoa 库模板创建一个新项目
  • 库类型设置为静态
  • 添加您的类/函数/任何
  • Build Phases 下,注意哪些标题将变为Public

Step 2 : Use the Library in a test project

第 2 步:在测试项目中使用库

  • Drag'n'dropthe final .a library file into the project (doesn't matter if you also copy it to the target directory)
  • Link against the library
  • Update the User Header Search Pathsto your initial Library .afile origin (using recursion (like /the/path/to/your/library/folder/**)
  • Set Always search user pathsto YES
  • Add -ObjCto Other Linker Flags, under Build Settings.
  • 将最终的 .a 库文件拖放到项目中(如果你也将其复制到目标目录也无所谓)
  • 链接到图书馆
  • 用户标题搜索路径更新为您的初始库.a文件来源(使用递归(如/the/path/to/your/library/folder/**
  • 始终搜索用户路径设置为YES
  • Build Settings下添加-ObjCOther Linker Flags