views:

142

answers:

1

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?

+1  A: 

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

Sol
This is true, sorry for not specifying this. I ahve done this. I have labelled absolutely every class with this macro
Mark
So what do your function prototypes actually look like?
Sol
Or are those functions part of a class declaration?
Sol
I've added what I believe you are asking for
Mark
If the new and delete are not class members, you need to add `DECL_MEMORY` to them, too. Like this:`DECL_MEMORY void* operator new(unsigned int size, const char* file, int line);`
Sol
(I never use extern like you are using it, not sure if there is a point to that or not.)
Sol