在不获取 LNK4006 的情况下,在 Visual C++ 中链接具有依赖项的库 [英] Link libraries with dependencies in Visual C++ without getting LNK4006
问题描述
我有一组静态编译的库,这些库之间有相当深入的依赖关系.例如,可执行文件 X 使用库 A 和 B,A 使用库 C,B 使用库 C 和 D:
I have a set of statically-compiled libraries, with fairly deep-running dependencies between the libraries. For example, the executable X uses libraries A and B, A uses library C, and B uses libraries C and D:
X -> A
A -> C
X -> B
B -> C
B -> D
当我将 X 与 A 和 B 链接时,如果 C 和 D 没有也被添加到库列表中,我不想得到错误—A 和 B 在内部使用这些库的事实是 X 的一个实现细节应该不需要知道.此外,当在依赖树中的任何位置添加新依赖时,使用 A 或 B 的任何程序的项目文件都必须重新配置.对于深度依赖树,所需库的列表可能会变得非常长且难以维护.
When I link X with A and B, I don't want to get errors if C and D were not also added to the list of libraries—the fact that A and B use these libraries internally is an implementation detail that X should not need to know about. Also, when new dependencies are added anywhere in the dependency tree, the project file of any program that uses A or B would have to be reconfigured. For a deep dependency tree, the list of required libraries can become really long and hard to maintain.
所以,我在 A 项目中使用 Librarian 部分的Additional Dependencies"设置,添加 C.lib.在 B 的项目的同一部分,我添加了 C.lib 和 D.lib.这样做的效果是图书管理员将 C.lib 捆绑到 A.lib 中,将 C.lib 和 D.lib 捆绑到 B.lib 中.
So, I am using the "Additional Dependencies" setting of the Librarian section in the A project, adding C.lib. And in the same section of B's project, I add C.lib and D.lib. The effect of this is that the librarian bundles C.lib into A.lib, and C.lib and D.lib into B.lib.
然而,当我链接 X 时,A.lib 和 B.lib 都包含它们自己的 C.lib 副本.这会导致大量警告沿着
When I link X, however, both A.lib and B.lib contain their own copy of C.lib. This leads to tons of warnings along the lines of
A.lib(c.obj):警告 LNK4006 符号"(_symbol) 已经在 B.lib(c.obj) 中定义;忽略第二个定义.
A.lib(c.obj) : warning LNK4006 "symbol" (_symbol) already defined in B.lib(c.obj); second definition ignored.
如何在不收到警告的情况下完成此操作?有没有办法简单地禁用警告,或者有更好的方法?
How can I accomplish this without getting warnings? Is there a way to simply disable the warning, or is there a better way?
编辑:我看到不止一个答案表明,由于缺乏更好的替代方案,我只是禁用了警告.嗯,这是问题的一部分:我什至不知道如何禁用它!
EDIT: I have seen more than one answer suggesting that, for the lack of a better alternative, I simply disable the warning. Well, this is part of the problem: I don't even know how to disable it!
推荐答案
据我所知,您无法禁用链接器警告.但是,您可以使用链接器的命令行参数忽略其中的一些,例如./忽略:4006
As far as I know you can't disable linker warnings. However, you can ignore some of them, using command line parameter of linker eg. /ignore:4006
将其放在链接器->命令行设置下的项目属性中(不记得确切位置).
Put it in your project properties under linker->command line setting (don't remember exact location).
另请阅读:
瓦克
这篇关于在不获取 LNK4006 的情况下,在 Visual C++ 中链接具有依赖项的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!