开发者

use of extern methods between dll projects?

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?


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜