dll项目之间使用extern方法?
我有一个调试条件来管理内存,
extern void* operator new(unsigned int size, const char* file, int line);
extern void operator delete(void* address, const char* file, int line);
extern void Delete(void* address);
#define FUN_NEW new(__FILE__, __LINE__)
#define FUN_DELETE delete
它存在于 Memory.h 中,并在 Memory.cpp 中实现。 Memory.h 定义为:
#ifdef MEMORY_EXPORT
#define DECL_MEMORY __declspec(dllexport)
#else
#define DECL_MEMORY __declspec(dllimport)
#endif
class DECL_MEMORY Memory : public Singleton<Memory>
{
现在,我有 SoundStuff.h 和 SoundStuff.cpp,它们位于单独的项目中,也以类似的方式转换为 dll到上面。 SoundStuff
所属的项目与 Memory
所属的项目具有项目依赖关系。在 SoundStuff.cpp 的实现中,调用 Memory.h 中的 FUN_DELETE
。它是通过单独项目中的函数调用的,但不管怎样都会调用。这会导致链接器错误。
错误 LNK2019:无法解析的外部 符号“void __cdecl 运算符 删除(无效*,字符常量*,int)” (??3@YAXPXPBDH@Z) 中引用 功能 __unwindfunclet$?Init@SoundStuff@@AAEXXZ$1 SoundStuff.obj
这是为什么以及如何修复它?
I have a debug condition to manage memory where I have
extern void* operator new(unsigned int size, const char* file, int line);
extern void operator delete(void* address, const char* file, int line);
extern void Delete(void* address);
#define FUN_NEW new(__FILE__, __LINE__)
#define FUN_DELETE delete
This exists in Memory.h and is implemented in Memory.cpp. Memory.h is defined as:
#ifdef MEMORY_EXPORT
#define DECL_MEMORY __declspec(dllexport)
#else
#define DECL_MEMORY __declspec(dllimport)
#endif
class DECL_MEMORY Memory : public Singleton<Memory>
{
Now, I have SoundStuff.h and SoundStuff.cpp, which are in a seperate project, also being converted to a dll in a similar manner to above. The project that SoundStuff
belongs to has a project dependency to the project that Memory
belongs to. In the implementation of SoundStuff.cpp, FUN_DELETE
, from Memory.h, is called. It is called through a function in a separate project, but it is called regardless. This is leading to linker errors.
error LNK2019: unresolved external
symbol "void __cdecl operator
delete(void *,char const *,int)"
(??3@YAXPAXPBDH@Z) referenced in
function
__unwindfunclet$?Init@SoundStuff@@AAEXXZ$1 SoundStuff.obj
Why is this and how can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须明确告诉编译器您想要导出哪些函数。要做到这一点需要一些小技巧,我是这样做的:
我想要导出的每个函数(或类)都被标记为
CORE_EXPORT
。要构建 DLL,请定义USING_DLL
,并在 CoreFunctions 项目(而不是 DoSomeStuff 项目)中定义CORE_EXPORTS
。这将确保您的函数/类在构建 CoreFunctions DLL 时声明__declspec( dllexport )
(因此它们被导出),并在构建 DoSomeStuff 时声明__declspec( dllimport )
(所以它们是进口的)。You have to explicitly tell the compiler which functions you'd like to export. There's a little song-and-dance to do this, here's how I do it:
Each function (or class) I would like to export gets tagged with
CORE_EXPORT
. To build for DLLs, defineUSING_DLL
, and in your CoreFunctions project (but not your DoSomeStuff project) defineCORE_EXPORTS
. That will ensure that your functions/classes are declared__declspec( dllexport )
when the CoreFunctions DLL is building (so they are exported), and__declspec( dllimport )
when DoSomeStuff is building (so they are imported).