3
votes

I am trying to create a class library dll that exports a template class. I have a local version of the same class and it must be coincidental, but it works correctly. I know this doesn't really mean much, clearly something is wrong.

The exported template class in the library dll is:

template <class CcmBlock>
class CCMSHARED_EXPORT CcmFilter
{
public:
    CcmFilter()
    {
        mBlock = nullptr;
        ////mBlockList = new std::list<CcmBlock*>();
    }

    void add(CcmFrame* frame)
    {
        if (frame == nullptr)
            return;

        mBlock = new CcmBlock(
                    frame->getFrameData(),
                    frame->getSampleRate(),
                    0xFFFFFFFF,
                    frame->getBlockSize(),
                    frame->getDomain()
                    );
        mBlockList->push_back(aBlock);
    }

    CcmBlock* get()
    {
        return mBlock;
    }
private:
    CcmBlock* mBlock;
    ////std::list<CcmBlock*>* mBlockList;
};

On the application side:

    CcmFilter<FooBlock>* oneFilter = new CcmFilter<FooBlock>();
    //Filter<Block>* filter = new Filter<Block>();

    CcmFrame* frame = new CcmFrame(0, 50000, 40, 1024, Domain::Time);

    oneFilter->add(frame);
    CcmBlock* block = oneFilter->get();
    FooBlock* fooBlock = dynamic_cast<FooBlock*>(block);
    if (fooBlock == nullptr)
    { //Report Error }
    else
    { // Do the work}

and the FooBlock class is derived from CcmBlock as follows:

class FooBlock : public CcmBlock
{
public:
    FooBlock(int* ipblock, DWORD dwSampleRate, DWORD dwMicrophoneIndex, DWORD dwBlockSize, Domain domain);

    void process();
};

The library compiles and builds the dll. When I attempt to build the application I get messages:

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl CcmFilter::CcmFilter(void)" (__imp_??0?$CcmFilter@VFooBlock@@@@QEAA@XZ) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QEAA@PEAVQWidget@@@Z)

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl CcmFilter::add(class CcmFrame *)" (__imp_?add@?$CcmFilter@VFooBlock@@@@QEAAXPEAVCcmFrame@@@Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QEAA@PEAVQWidget@@@Z)

2
Templates can't be part of a DLL. You can have template instantiations that are part of the DLL.Vaughn Cato
@Vaughn isn't std::list in a dll?Nefarious
No, it is in a header.Vaughn Cato
@Vaughn I can't count the number of times I read "It's a header, and it really is" and still mad the mental block about it being in the dll. Thanks, I appreciate you pointing this out to me again :)Nefarious

2 Answers

2
votes

DLL-s are loaded at runtime, while templates are instantiated at compile time.

A work-around which is not ideal but might work for a very limited use case is to explicitly instantiate a template somewhere after their declaration. That is, add something like:

template class CcmFilter<FooBlock>;

For details, see https://msdn.microsoft.com/en-us/library/by56e477(VS.80).aspx

0
votes

Just do not use declspec. But in your case template will be compiled each time when you include it in *.cpp file. It wont be exported.

In order to export some well-known implementations of your template you probably should use template explicit instantiation definitions in tandem with __declspec(export).