静态库在特定项目中使用框架 [英] Static Library using frameworks in specific projects

查看:100
本文介绍了静态库在特定项目中使用框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含所有泛型类的静态库。其中一些类使用框架。

I have created a static library containing all my generic classes. Some of these classes use frameworks.

现在我有两个项目,一个使用一些使用框架的类,另一个不使用任何使用框架的类。

Now I have two projects, one that uses some classes that use frameworks, and one that doesn't use any of the classes that use frameworks.

因为静态库不支持包含框架(如果我是正确的)。我必须在使用它们的项目中包含框架。但是当我编译不使用任何框架类的项目时,编译器会中断,因为它仍然需要框架。现在我知道它试图从库中编译所有(未使用的)类,因为我使用链接器标记'-ObjC'来防止'无法识别的选择器'错误。

Because Static Libraries don't support including frameworks (if I am correct). I have to include the frameworks in the project that uses them. But when I compile the project that doesn't use any of the framework-classes the compiler breaks because it still requires the frameworks. Now I know it tries to compile all the (unused) classes from the library because I use the Linker Flag '-ObjC' to prevent 'unrecognized selector' errors.

有谁知道如何编译每个项目所需的源文件?并且防止所有框架必须包含在使用我的静态库的所有项目中?

Does anyone know how to compile only the required source files per project? And prevent from all frameworks having to be included in all projects that use my static library?

推荐答案

首先,你是对的因为静态库不能包含任何框架或其他静态库,它只是构成该特定静态库的所有目标文件(* .obj)的集合。

First of all, you are right in that a static library cannot include any framework nor other static libraries, it is just the collection of all object files (*.obj) that make up that specific static library.


有没有人知道如何只为每个项目编译所需的源文件?

Does anyone know how to compile only the required source files per project?

链接器默认情况下仅包含来自静态库的目标文件中包含应用程序引用的符号的链接。因此,如果静态库中有两个文件 am bm ,则只使用<$ c $中的符号c> am 在您的主程序中,然后 bo (从 bc生成的目标文件 )不会出现在您的最终可执行文件中。作为子案例,如果 bm 使用仅声明(未实现)的函数/类 c ,则你不会得到任何链接器错误。一旦在程序中包含 bm 中的一些符号, bo 也将被链接,您将收到链接器错误由于缺少 c 的实现。

The linker will by default only link in object files from the static library that contain symbols referenced by the application. So, if you have two files a.m and b.m in your static library and you only use symbols from a.m in your main program, then b.o (the object file generated from b.c) will not appear in your final executable. As a sub-case, if b.m uses a function/class c which is only declared (not implemented), then you will not get any linker errors. As soon as you include some symbols from b.m in your program, b.o will also be linked and you will get linker errors due to the missing implementation of c.

如果你希望这种选择发生在符号而不是对象级粒度,在Xcode中启用死代码剥离。这对应于gcc选项-Wl,-dead_strip(=项目的构建设置信息窗格中的链接器选项-dead_strip)。这将确保进一步优化。

If you want this kind of selection to happen at symbol rather than at object level granularity, enable dead code stripping in Xcode. This corresponds to the gcc option -Wl,-dead_strip (= linker option -dead_strip in the Build settings Info pane for your project). This would ensure further optimization.

在您的情况下,正如您所说的那样,使用-ObjC链接器标志会破坏此机制。所以这实际上取决于你。如果你删除-Objc标志,你会得到你想要的免费行为,同时失去对选择器的更严格检查。

In your case, though, as you correctly say, it is the use of the "-ObjC" linker flag that defeats this mechanism. So this actually depends on you. If you remove the -Objc flag, you get the behavior you like for free, while losing the stricter check on selectors.


并防止所有框架都必须包含在使用我的静态库的所有项目中?

And prevent from all frameworks having to be included in all projects that use my static library?

Xcode / GCC支持一个名为< a href =http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html\"rel =noreferrer>弱链接,允许懒惰加载框架或静态库,即仅在实际使用其中一个符号时。
弱链接可以通过链接器标志(参见上面的Apple doc)或通过Xcode UI(目标 - >信息 - >常规 - >链接库)启用。

Xcode/GCC support an linking option which is called "weak linking", which allows to lazily load a framework or static library, i.e., only when one of its symbols is actually used. "weak linking" can be enabled either through a linker flag (see Apple doc above), or through Xcode UI (Target -> Info -> General -> Linked Libraries).

无论如何,在编译/链接时,框架或库必须在所有情况下都可用:weak选项仅影响框架在运行时首次加载的时刻。因此,我不认为这对你有用,因为你无论如何都需要在你的所有项目中包含框架,这是你不想要的。

Anyhow, the framework or library must be available in all cases at compile/link time: the "weak" option only affects the moment when the framework is first loaded at runtime. Thus, I don't think this is useful for you, since you would need anyway to include the framework in all of your projects, which is what you do not want.

作为旁注, weak_linking 是一个选项,当使用仅在较新的SDK版本(例如,4.3.2)上提供的功能时,大多数情况下都是有意义的,同时还支持在较旧的SDK上部署版本(比如3.1.3)。在这种情况下,您依赖于以下事实:较新的SDK框架将在较新的部署设备上实际可用,并且您有条件地编译需要它的功能,因此在旧设备上它们将不被要求(并且因此不会产生尝试加载更新版本的框架和崩溃)。

As a side note, weak_linking is an option that mostly make sense when using features only available on newer SDK version (say, 4.3.2) while also supporting deployment on older SDK versions (say, 3.1.3). In this case, you rely on the fact that the newer SDK frameworks will be actually available on the newer deployment devices, and you conditionally compile in the features requiring it, so that on older devices they will not be required (and will not produce thus the attempt at loading the newer version of the framework and the crash).

更糟糕的是,GCC不支持与Microsoft编译器称为自动链接的功能,允许通过源文件中的#pragma注释指定要链接的库。这可以提供一种解决方法,但不存在。

To make things worse, GCC does not support a feature known as "auto-linking" with Microsoft compilers, which allow to specify which library to link by means of a #pragma comment in your source file. This could offer a workaround, but is not there.

所以,我很遗憾不得不说你应该使用可以同样满足您需求的不同方法:

So, I am really sorry to have to say that you should use a different approach that could equally satisfy your needs:


  1. 删除-ObjC标志;

  1. remove the -ObjC flag;

根据外部框架的依赖关系将静态库拆分为两个或多个部分;

split your static library in two or more parts according to their dependencies from external frameworks;

求助于包含源代码文件直接。

resort to including the source files directly.

这篇关于静态库在特定项目中使用框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆