链接静态库,共享另一个静态库 [英] Linking static libraries, that share another static library

查看:191
本文介绍了链接静态库,共享另一个静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前拥有一个非常大的代码库的Xcode项目,我将其称为项目X ,我将其分为一组子项目(项目A,B ,C )。



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



我有另一个xcode项目项目Z ,需要由项目B和C 生成的静态库。这就是问题。当项目Z 进入链接阶段时,会出现一些问题 - 在项目B和C 的libs中找到重复的符号,以便在项目中原来链接的代码一个



我对于静态图书馆的世界很新奇,我不确定如何推进 Project Z ,或者如何修改其他项目,以便它们与相同的项目A lib链接。我有一种感觉是不可能的。我在这里有什么选择?



编辑:



我应该澄清项目B 项目C 需要构建为单独的静态库,因为一些客户端将只需要一个或另一个。



此外,我有OSX和iOS平台上的这个困境。



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

解决方案

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



每个静态库都应该有自己的代码。最终应用程序负责将所有必需的库连接在一起(包括库所需的库)。这样一来,每个事情都有一个副本。


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 ).

So far, each of these projects compiles, on their own, just fine. They all produce static libraries. Project B and Project C are dependent on the static library produced by Project A in order to build.

I have another xcode project, Project Z, that requires the static libraries produced by Projects B and C. Herein lies the problem. When Project Z enters the linker phase, things blow up - duplicate symbols are found within the libs for Projects B and C for the code they originally linked against in Project 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 A lib. I have a feeling it's not possible. What are my options here?

Edit:

I should clarify that Project B and Project C need to build into separate static libs because some clients will only require one or the other.

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

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.

解决方案

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.

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.

这篇关于链接静态库,共享另一个静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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