MSVC需要为内联编译器生成的构造函数导入DLL
我有一个奇怪的C ++问题,不知道如何解决它。
我有一个公共标题:
// a.h
#ifdef A_EXPORTS
#define A_API __declspec(dllexport)
#else
#define A_API __declspec(dllimport)
#endif
struct A_API IWorker
{
virtual bool foo() { return false; }
};
我用-DA_EXPORTS
构建了a.dll
。
但是我的test.exe
只需要一个模拟的iworker
。
// test.cpp
#include "a.h"
class CMockWorker : public IWorker
{
public:
bool foo() override { return true; }
};
我构建了test.exe
,发现它取决于a.dll
,因为外部符号public:__cdecl iworker :: iworker(void)__ptr64
我知道iWorker
由于VTBL而具有编译器生成的构造函数,但是可以在线上,因此我们可以摆脱a.dll
?
谢谢!
正如 @Hans-Passant所提到的那样,是的,一旦我删除了虚拟方法的定义,问题就解决了。
// a.h
struct IWorker
{
virtual bool foo() = 0;
};
但是在现有项目中,一些所谓的接口类具有相似的虚拟机体,并且很难重构它们。
因此,我真的很想找到理想的解决方案,以强制使用C'tor。也许今天不是,希望它可以在将来被掩盖。
I got a weird C++ question and have no idea how to resolve it.
I have a public header:
// a.h
#ifdef A_EXPORTS
#define A_API __declspec(dllexport)
#else
#define A_API __declspec(dllimport)
#endif
struct A_API IWorker
{
virtual bool foo() { return false; }
};
I built a.DLL
with -DA_EXPORTS
.
But my test.exe
just needs a mocked IWorker
.
// test.cpp
#include "a.h"
class CMockWorker : public IWorker
{
public:
bool foo() override { return true; }
};
I built test.exe
, and found it depends on a.DLL
because of external symbol public: __cdecl IWorker::IWorker(void) __ptr64
I know that IWorker
has a compiler-generated constructor because of the vtbl, but could it be inline so we can get rid of a.DLL
?
Thanks!
As @hans-passant mentioned, yes, once I removed the definition of virtual methods, problem resolved.
// a.h
struct IWorker
{
virtual bool foo() = 0;
};
But in existing project, some so-called interface class has similar dummy body and hard to refactor them.
So I really want to find the ideal solution to force-inline the c'tor, if has. Maybe not today, hope it could be impl-ed in the future.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论