简体   繁体   English

如何将库静态链接到DLL?

[英]How do I statically link a library into a DLL?

I'm in DLL hell. 我在DLL地狱中。 I have 2 DLLs let's say A.dll and B.dll which have a name collision - the same C++ class name is used in each, etc. Both DLLs are available as static libs. 我有2个DLL,比如说A.dll和B.dll,它们的名称冲突-彼此使用相同的C ++类名,等等。这两个DLL都可以作为静态库使用。

Can I create a 'wrappe' DLL, say Aprime.dll which exports a similar class, methods, etc. as in A.dll, and delegates the functionality to the class in A.lib, but statically linked into the Aprime.dll? 我是否可以创建一个“包裹”的DLL,例如Aprime.dll,它导出与A.dll类似的类,方法等,并将功能委派给A.lib中的类,但静态链接到Aprime.dll? Wouldn't that avoid the name collision? 这样不能避免名称冲突吗?

I've been trying this, but not sure I have the MSVS project set up correctly. 我一直在尝试,但是不确定我是否正确设置了MSVS项目。 Aprime.dll is being produced, but according to Dependency Walker Aprime.dll is still loading A.dll. 正在生成Aprime.dll,但根据Dependency Walker的说法,Aprime.dll仍在加载A.dll。

I've been searching, but most stuff I find applies only to statically linking in the CRT or MFC, which have their own switches. 我一直在搜索,但是我发现的大多数内容仅适用于CRT或MFC中的静态链接,它们具有自己的开关。

I added A.lib under Linker -> Input -> Additional Dependencies. 我在链接器->输入->其他依赖项下添加了A.lib。

Am I missing some magic linker command line switch or something? 我是否缺少一些魔术链接器命令行开关或其他东西?

Yes that method should work. 是的,该方法应该可行。 The key though is to ensure that you do not include any of the original A.dll header files in the Aprime.dll header file. 但是,关键是确保您不要在Aprime.dll头文件中包含任何原始的A.dll头文件。 Otherwise if someone includes Aprime.h/pp then it will include Ah/pp and you will then have a clash again. 否则,如果有人包含Aprime.h / pp,则它将包含Ah / pp,然后您将再次发生冲突。

So you want something like: 所以你想要像这样的东西:

Ah

// Library A includes
class test
{
};

Bh BH

// Library B includes
class test
{
}

Aprime.h Aprime.h

// Proxy class
class myTest
{
}

Aprime.cpp Aprime.cpp

#include "A.h"
#include "Aprime.h"

...

main.cpp main.cpp中

#include "Aprime.h"
#include "B.h"

...

Note that main never includes both A and B. In only includes Aprime and B. B is still static lib and Aprime is a DLL. 请注意,main永远不会同时包含A和B。In仅会包含Aprime和B。B仍然是静态lib,而Aprime是DLL。

Yes, you can, and it's supported at a very low level by Windows. 是的,您可以,并且Windows仅在很小的程度上支持它。 Aprime.DLL is effectively empty except for a bunch of references to A.DLL . 除一堆对A.DLL的引用外, Aprime.DLL是空的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM