0

I have this template smart pointer class in a dll.

sp.h
---------

#ifdef VLIB_EXPORTS
#define VLIB_API __declspec(dllexport)
#else
#define VLIB_API __declspec(dllimport)
#endif


template < typename T > class VLIB_API SP
{
protected:
        T*    m_pData;
    long*       m_pRefCounter;

public:


    SP(void);
    {
        m_pData = NULL;
        m_pRefCounter = NULL;
    }

    ...
    ...
};

ImagePtr.h
---------------
class VLIB_API CVImagePtr
{
    ....
}




MainLib.h
-------------
#include sp.h
#include ImagePtr.h


typedef SP<CVBlob> CVBlobPtr;

class VLIB_API CVLib
{
public:
    virtual CVBlobPtr CreateBlob() = 0;
    virtual CVImagePtr CreateImg() = 0;
};

When I try to use this class in another project (CVMLib), the compiler will complain this: error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall SP::~SP(void)"

but no problem for CVImagePtr.

class VMLIB_API CVMLib : public CVLib
{
public:
    virtual  CVBlobPtr CreateBlob();
    virtual CVImagePtr CreateImg();
};

It seems there's a problem when the class is a template. If so, how do I export a template class?

Can somebody help me resolve this? Thank you!

AvatarBlue
  • 47
  • 7

2 Answers2

0

As suspected, I'm not exporting the template class properly. This is what I did:

MainLib.h

#include sp.h
#include ImagePtr.h

#ifdef VLIB_EXPORTS
#define VLIB_API __declspec(dllexport)
#define EXPIMP_TEMPLATE
#else
#define VLIB_API __declspec(dllimport)
#define EXPIMP_TEMPLATE extern
#endif

EXPIMP_TEMPLATE template class VLIB_API SP<CVBlob>;
typedef SP<CVBlob> CVBlobPtr;

class VLIB_API CVLib
{
public:
    virtual CVBlobPtr CreateBlob() = 0;
    virtual CVImagePtr CreateImg() = 0;
};

You can find more information here: http://support.microsoft.com/kb/168958

AvatarBlue
  • 47
  • 7
-1

You need to mark the class with extern "C" in order to have the non mangled name on the implementation of the class as well as the header.

Have a look at this canonical answer as to why.

Community
  • 1
  • 1
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216