I am new to building static libraries and would like to create 2(+) libraries each of which has some unique code and some shared code. My intention is that other projects will link one or more of these static libraries.
我是构建静态库的新成员,并且希望创建2个(+)库,其中每个库都有一些独特的代码和一些共享代码。我的意图是其他项目将链接这些静态库中的一个或多个。
Util.h/m <-- Shared
ImplOne.h/m <-- Unique to 'ImplOne'
ImplTwo.h/m <-- Unique to 'ImplTwo'
I am using XCode and generating the libraries by building Util.m
and ImplOne.m
in one case, and Util.m
and ImplTwo.m
in the other.
我正在使用XCode并通过构建Util生成库。m和ImplOne。m在一种情况下,直到。m和ImplTwo。m。
Of course the issue is that I now cannot use these libraries together because they will have duplicate symbols. What is a better architecture for this situation?
当然,问题是我现在不能同时使用这些库,因为它们将具有重复的符号。对于这种情况,更好的体系结构是什么?
2 个解决方案
#1
3
I think the cleanest alternative would be to make a separate library for the shared Utils.h/m
. The drawbacks are that your users would need to link utils
along with implone
and/or impltwo
, and that in order to link impoone
and impltwo
together both libraries need to be compiled with the same version of utils
. In return, the source of the libraries would stay nice and clean.
我认为最干净的选择是为共享的Utils.h/m创建一个单独的库。缺点是您的用户需要将utils与implone和/或impltwo链接在一起,而要将impoone和impltwo链接在一起,则需要使用相同版本的utils进行编译。作为回报,图书馆的来源将保持整洁。
An alternative would be to use macros to alter function and variable names in the utils
to avoid linker errors. The most important consequence of this is code duplication: both implone
and impltwo
would link identical pieces of code under different names. The other one is readability: it would suffer tremendously, because each reference to a function in utils
would need to be wrapped in a macro. This makes the second approach dirty, so I would definitely recommend making utils
a separate library.
另一种方法是使用宏修改utils中的函数和变量名,以避免链接器错误。最重要的结果是代码复制:implone和impltwo都将在不同的名称下链接相同的代码片段。另一个是可读性:它将遭受极大的痛苦,因为每个对utils中的函数的引用都需要用宏来包装。这使得第二种方法变得很脏,所以我绝对建议将utils设置为一个单独的库。
#2
2
I have a similar problem to this in a current codebase. My solution is similar to the answer that just came in as I wrote this, but I'll leave it here as a comparison/reference.
在当前的代码基中,我也有类似的问题。我的答案和我刚写这个的时候得到的答案相似,但是我把它留在这里作为比较/参考。
I have a common static library project (let's call it libCommonBits.a). I have a second static library which has some UI code in it (let's call that libCommonUI.a), which also depends on libCommonBits.a. My main app needs to use both libraries.
我有一个公共静态库项目(我们称之为libCommonBits.a)。我有第二个静态库,其中包含一些UI代码(我们称之为libCommonUI.a),它也依赖于libcommonbit .a。我的主应用程序需要同时使用这两个库。
My solution is to not actually link the libCommonBits.a into the libCommonUI.a. It compiles against the header files fine, and just means that my main app links against two static libraries instead of one.
我的解决方案是不链接libCommonBits。一个libCommonUI.a。它很好地根据头文件进行编译,这意味着我的主应用程序链接到两个静态库而不是一个静态库。
I'm hoping this approach is taken on by more people distributing libraries. It's frustrating when you use a static library that has already linked in common libraries (JSONKit or similar), and you also use that separately in your codebase. In that case, the symbol renaming might be the only solution.
我希望这种方法能被更多的分发图书馆的人采用。当您使用一个已经在公共库(JSONKit或类似库)中链接的静态库时,您会感到沮丧,并且您还会在代码库中单独使用它。在这种情况下,符号重命名可能是唯一的解决方案。
#1
3
I think the cleanest alternative would be to make a separate library for the shared Utils.h/m
. The drawbacks are that your users would need to link utils
along with implone
and/or impltwo
, and that in order to link impoone
and impltwo
together both libraries need to be compiled with the same version of utils
. In return, the source of the libraries would stay nice and clean.
我认为最干净的选择是为共享的Utils.h/m创建一个单独的库。缺点是您的用户需要将utils与implone和/或impltwo链接在一起,而要将impoone和impltwo链接在一起,则需要使用相同版本的utils进行编译。作为回报,图书馆的来源将保持整洁。
An alternative would be to use macros to alter function and variable names in the utils
to avoid linker errors. The most important consequence of this is code duplication: both implone
and impltwo
would link identical pieces of code under different names. The other one is readability: it would suffer tremendously, because each reference to a function in utils
would need to be wrapped in a macro. This makes the second approach dirty, so I would definitely recommend making utils
a separate library.
另一种方法是使用宏修改utils中的函数和变量名,以避免链接器错误。最重要的结果是代码复制:implone和impltwo都将在不同的名称下链接相同的代码片段。另一个是可读性:它将遭受极大的痛苦,因为每个对utils中的函数的引用都需要用宏来包装。这使得第二种方法变得很脏,所以我绝对建议将utils设置为一个单独的库。
#2
2
I have a similar problem to this in a current codebase. My solution is similar to the answer that just came in as I wrote this, but I'll leave it here as a comparison/reference.
在当前的代码基中,我也有类似的问题。我的答案和我刚写这个的时候得到的答案相似,但是我把它留在这里作为比较/参考。
I have a common static library project (let's call it libCommonBits.a). I have a second static library which has some UI code in it (let's call that libCommonUI.a), which also depends on libCommonBits.a. My main app needs to use both libraries.
我有一个公共静态库项目(我们称之为libCommonBits.a)。我有第二个静态库,其中包含一些UI代码(我们称之为libCommonUI.a),它也依赖于libcommonbit .a。我的主应用程序需要同时使用这两个库。
My solution is to not actually link the libCommonBits.a into the libCommonUI.a. It compiles against the header files fine, and just means that my main app links against two static libraries instead of one.
我的解决方案是不链接libCommonBits。一个libCommonUI.a。它很好地根据头文件进行编译,这意味着我的主应用程序链接到两个静态库而不是一个静态库。
I'm hoping this approach is taken on by more people distributing libraries. It's frustrating when you use a static library that has already linked in common libraries (JSONKit or similar), and you also use that separately in your codebase. In that case, the symbol renaming might be the only solution.
我希望这种方法能被更多的分发图书馆的人采用。当您使用一个已经在公共库(JSONKit或类似库)中链接的静态库时,您会感到沮丧,并且您还会在代码库中单独使用它。在这种情况下,符号重命名可能是唯一的解决方案。