MSVC需要为内联编译器生成的构造函数导入DLL

发布于 2025-01-20 01:24:08 字数 1122 浏览 1 评论 0原文

我有一个奇怪的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文