xcode 链接静态库,共享另一个静态库

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

Linking static libraries, that share another static library

xcodelinkerstatic-libraries

提问by Jeff

I currently have a single Xcode project for a very large code base, I'll call it Project X, which I am dividing into a bunch of sub projects ( Projects A, B, C).

我目前有一个用于非常大的代码库的 Xcode 项目,我将其称为Project X,我将其划分为一堆子项目(项目 A、B、C)。

So far, each of these projects compiles, on their own, just fine. They all produce static libraries. Project Band Project Care dependent on the static library produced by Project Ain order to build.

到目前为止,这些项目中的每一个都可以自行编译。它们都生成静态库。项目 B项目 C依赖于项目 A生成的静态库才能构建。

I have another xcode project, Project Z, that requires the static libraries produced by Projects B and C. Herein lies the problem. When Project Zenters the linker phase, things blow up - duplicate symbols are found within the libs for Projects B and Cfor the code they originally linked against in Project A!

我有另一个 xcode 项目Project Z,它需要项目 B 和 C生成的静态库。问题就在这里。当项目 Z进入链接器阶段时,事情就炸了——在项目 B 和 C的库中发现了它们最初在项目 A 中链接的代码的重复符号!

I'm pretty new to the world of static libraries, and I'm unsure of how to move forward with Project Z, or how to modify the other projects so that they are linking against the same Project Alib. I have a feeling it's not possible. What are my options here?

我对静态库的世界很陌生,我不确定如何推进Project Z,或者如何修改其他项目以便它们链接到同一个Project Alib。我有一种感觉,这是不可能的。我在这里有哪些选择?

Edit:

编辑:

I should clarify that Project Band Project Cneed to build into separate static libs because some clients will only require one or the other.

我应该澄清一下,项目 B项目 C需要构建到单独的静态库中,因为有些客户只需要其中一个。

Also, I'm having this dilemma on both OSX and iOS platforms.

此外,我在 OSX 和 iOS 平台上都遇到了这种困境。

I realize that I could solve this problem on OSX by building the projects as dynamic libraries. However, I'd prefer not to do this, and it still leaves me with same issue on iOS.

我意识到我可以通过将项目构建为动态库来解决 OSX 上的这个问题。但是,我不想这样做,它仍然让我在 iOS 上遇到同样的问题。

回答by Rob Napier

Static libraries should never include other static libraries (or third party code in general). A static library is just a bundle of .o files glued together. So if you have multiple copies of the same information, it's going to blow up.

静态库不应该包含其他静态库(或一般的第三方代码)。静态库只是一堆 .o 文件粘在一起。所以如果你有多个相同信息的副本,它就会爆炸。

Each static library should just have its own code in it. The final application is responsible for linking all the required libraries together (including libraries required by libraries). This way there is exactly one copy of each thing linked.

每个静态库都应该有自己的代码。最终的应用程序负责将所有需要的库链接在一起(包括库需要的库)。这样,每个链接的东西都只有一个副本。

回答by Jonah

This sounds like exactly the sort of problem CoacoaPodswas created to solve. If you define pods for each of theses projects then Zshould be able to determine and link against all of its dependencies all the way up the chain without introducing duplicate symbols.

这听起来正是CoacoaPods旨在解决的问题。如果您为这些项目中的每一个都定义了 pod,那么Z应该能够确定并链接它的所有依赖项,而不会引入重复的符号。