Takeshi
Takeshi

Reputation:

DLL exporting only certain templatized functions

Hey all, so this seems to be a rather strange issue to me. I have a very simple templatized container class which is part of a DLL. The entire class is defined in a header file, to allow automatic template generation. Now another part of the DLL actually requests a certain template type to be generated, so the code should exist within the DLL. However, when using the object from another executable, the constructor/destructor, plus multiple other functions work, however 2 functions are not found by the linker. Following are is the code for these two functions, as well as for a working function.

const T** getData() const
{
    return m_data;
}

int getNumRows() const
{
    return m_nRows;
}

int getNumCols() const
{
    return m_nCols;
}

So the getNumRows() and getNumCols() functions are not found by the linker, however the getData() function is. Is this a common problem, do the functions need to have a templatized parameter in order to be generated?

@1 800 INFORMATION

I have exported this from the DLL via a standard macro:

#ifdef ACORE_EXPORTS
#define ACORE_API __declspec(dllexport)
#else
#define ACORE_API __declspec(dllimport)
#endif

And at the class definition:

template < class T >
class ACORE_API matrix

Upvotes: 0

Views: 106

Answers (2)

Johan Ericsson
Johan Ericsson

Reputation: 71

The compiler will only generate member functions that are actually called.

For instance:

template <class T>
class MyClass
{public:
    int function1()
    {
        return 0;
    }
    int function2()
    {
        T t;
        t->DoSomething();
        return 0;
    }
};

and then later

MyClass<int> m;
m.function1();

compiles, because MyClass::function2() was never compiled.

You can force the instantiation of the entire class by doing this:

template class MyClass<int>;

In that case, every method in the class will be instantiated. In this example, you get a compiler error.

Upvotes: 7

1800 INFORMATION
1800 INFORMATION

Reputation: 135295

Are you actually exporting the functions from the library? You would mention the names in the .def file, or use the dllexport and dllimport directives in order to accomplish this.

Upvotes: 1

Related Questions