C++ DLL 链接未解析的外部
我正在处理一个相当大的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您编译
Core
时,您希望这些函数是dllexport
;但是,当您编译 DLL 时,您希望它们是dllimport
。在你的例子中,你总是将它们定义为dllexport
,因此当你链接DLL时,它会抱怨你已经声明了一个函数(甚至说你会导出它)而没有定义它。解决方案很简单。无需手动
__declspec
ing,而是根据您是Core
还是 DLL 创建宏:对
EXPORT
中的函数使用EXPORT
>Core 和IMPORT
用于外部 DLL 中的函数:When you compile the
Core
you want these functions to bedllexport
; However, when you compile the DLL, you want them to bedllimport
. In your case, you're always defining them asdllexport
, 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
__declspec
ing, create a macro based on whether you're theCore
or the DLL:Use
EXPORT
for functions in theCore
andIMPORT
for functions in external DLLs:使用您的代码片段并在 DLL 上运行 Dumpbin.exe /exports 会产生以下输出:
请注意静态成员的导出在那里,但名称与您的名称略有不同。如果我通过 undname.exe 运行您的导出名称,我会得到:
请注意差异。你的目标项目中有一个邪恶的宏。通过将其添加到头文件来解决您的问题:
这可能会产生一些副作用:)
Using your snippet and running Dumpbin.exe /exports on the DLL produces this output:
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:
Note the difference. You've got an evil macro in your target project. Fix your problem by adding this to the header file:
This might have some side-effects :)
也许是一个愚蠢的问题,但你在某个地方定义它吗?你的定义看起来像这样:
Maybe a silly question but are you defining it somewhere? Your definition would look something like: