dll项目之间使用extern方法?

发布于 2024-08-07 23:15:35 字数 1107 浏览 5 评论 0原文

我有一个调试条件来管理内存,

    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.hSoundStuff.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 技术交流群。

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

发布评论

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

评论(1

青春如此纠结 2024-08-14 23:15:35

您必须明确告诉编译器您想要导出哪些函数。要做到这一点需要一些小技巧,我是这样做的:

#ifdef USING_DLL
#ifdef CORE_EXPORTS
#define CORE_EXPORT __declspec( dllexport )
#else
#define CORE_EXPORT __declspec( dllimport )
#endif
#else
#define CORE_EXPORT
#endif

我想要导出的每个函数(或类)都被标记为 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:

#ifdef USING_DLL
#ifdef CORE_EXPORTS
#define CORE_EXPORT __declspec( dllexport )
#else
#define CORE_EXPORT __declspec( dllimport )
#endif
#else
#define CORE_EXPORT
#endif

Each function (or class) I would like to export gets tagged with CORE_EXPORT. To build for DLLs, define USING_DLL, and in your CoreFunctions project (but not your DoSomeStuff project) define CORE_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).

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