C++ DLL 链接未解析的外部

发布于 2024-08-28 21:11:46 字数 438 浏览 6 评论 0原文

我正在处理一个相当大的 Core 项目,我正在尝试对其进行调整以使用我构建的 DLL 引擎,我收到了一堆错误,例如:

未解析的外部符号“private:静态类

当在 DLL 中包含来自 Core 的一些标头时,该类通过 __declspec(dllexport) 导出,但任何具有静态成员的标头都会抛出有关静态成员的大量错误。

这是一个相当大的问题项目中,我不能完全删除我看到的每个静态类成员,有没有关于此类的事情?

正在导入的类的基本示例:

class __declspec(dllexport) MyClass
{
    public:
        static bool m_someVar;
}

为了清楚起见,我只想解决定义了 m_someVar 的问题/在类实现文件中声明(忘记这个术语)

I have a rather big Core project that I'm working with, I'm attempting to adapt it to use a DLL Engine I've built, I'm getting a bunch of errors like:

unresolved external symbol "private: static class

When including some of the headers from the Core in the DLL, the class is exported via __declspec(dllexport) but any header with static members throws out a crapload of errors regarding the static members.

This is a rather big project, I can't exactly run around removing every static class member I see, is there anyway around this kind of thing?

A basic example of a class that's being imported:

class __declspec(dllexport) MyClass
{
    public:
        static bool m_someVar;
}

For clarity sake I'd just like to address that m_someVar is defined/declared (forget the term) in the classes implementation file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘羡 2024-09-04 21:11:46

当您编译Core时,您希望这些函数是dllexport;但是,当您编译 DLL 时,您希望它们是dllimport。在你的例子中,你总是将它们定义为dllexport,因此当你链接DLL时,它会抱怨你已经声明了一个函数(甚至说你会导出它)而没有定义它。

解决方案很简单。无需手动 __declspecing,而是根据您是 Core 还是 DLL 创建宏:

#ifndef I_AM_A_DLL
#define EXPORT __declspec(dllexport)
#define IMPORT __declspec(dllimport)
#else
#define EXPORT __declspec(dllimport)
#define IMPORT __declspec(dllexport)
#endif

EXPORT 中的函数使用 EXPORT >Core 和 IMPORT 用于外部 DLL 中的函数:

class EXPORT MyClass
{
    public:
        static bool m_someVar;
}

When you compile the Core you want these functions to be dllexport; However, when you compile the DLL, you want them to be dllimport. In your case, you're always defining them as dllexport, thus when you link the DLL it complains that you've declared a function (and even said you'd export it) without ever defining it.

The solution is simple. Instead of manually __declspecing, create a macro based on whether you're the Core or the DLL:

#ifndef I_AM_A_DLL
#define EXPORT __declspec(dllexport)
#define IMPORT __declspec(dllimport)
#else
#define EXPORT __declspec(dllimport)
#define IMPORT __declspec(dllexport)
#endif

Use EXPORT for functions in the Core and IMPORT for functions in external DLLs:

class EXPORT MyClass
{
    public:
        static bool m_someVar;
}
不奢求什么 2024-09-04 21:11:46

使用您的代码片段并在 DLL 上运行 Dumpbin.exe /exports 会产生以下输出:

1    0 0001107D ??4MyClass@@QAEAAV0@ABV0@@Z = @ILT+120(??4MyClass@@QAEAAV0@ABV0@@Z)
2    1 00017000 ?m_someVar@MyClass@@2_NA = ?m_someVar@MyClass@@2_NA (public: static bool MyClass::m_someVar)

请注意静态成员的导出在那里,但名称与您的名称略有不同。如果我通过 undname.exe 运行您的导出名称,我会得到:

Undecoration of :- "?m_someVare@MyClass@@0EA"
is :- "private: static unsigned char MyClass::m_someVare"

请注意差异。你的目标项目中有一个邪恶的宏。通过将其添加到头文件来解决您的问题:

#undef bool

这可能会产生一些副作用:)

Using your snippet and running Dumpbin.exe /exports on the DLL produces this output:

1    0 0001107D ??4MyClass@@QAEAAV0@ABV0@@Z = @ILT+120(??4MyClass@@QAEAAV0@ABV0@@Z)
2    1 00017000 ?m_someVar@MyClass@@2_NA = ?m_someVar@MyClass@@2_NA (public: static bool MyClass::m_someVar)

Note how the export for the static member is there but has a subtly different name from yours. If I run your export name through undname.exe, I get:

Undecoration of :- "?m_someVare@MyClass@@0EA"
is :- "private: static unsigned char MyClass::m_someVare"

Note the difference. You've got an evil macro in your target project. Fix your problem by adding this to the header file:

#undef bool

This might have some side-effects :)

零崎曲识 2024-09-04 21:11:46

也许是一个愚蠢的问题,但你在某个地方定义它吗?你的定义看起来像这样:

bool MyClass::m_someVar = false;

Maybe a silly question but are you defining it somewhere? Your definition would look something like:

bool MyClass::m_someVar = false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文