Search code examples
c++templatesdllexport

Cannot export template function


I have a class named "SimObject":

namespace simBase
{
    class __declspec(dllexport) SimObject: public SimSomething
    {
        public:

            template <class T>
            void updateParamValue( const std::string& name, T val );
    }
}

I have another class named "ITerrainDrawable":

namespace simTerrain
{
    class __declspec(dllexport) ITerrainDrawable : public simBase::SimObject
    {
    }
}

These classes are in different libraries. SimObject is in simBase, ITerrainDrawable is in simTerrain libraries. Even if ITerrainDrawable is derived from SimObject and I included library of simBase, I get a link error:

unresolved external symbol

1>ITerrainDrawable.obj : error LNK2019: unresolved external symbol "public: void __thiscall simBase::SimObject::updateParamValue<float>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,float)" (??$updateParamValue@M@SimObject@simBase@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@M@Z) referenced in function "public: void __thiscall simTerrain::ITerrainDrawable::setTerrainSize(float)" (?setTerrainSize@ITerrainDrawable@simTerrain@@QAEXM@Z)
1>ITerrainDrawable.obj : error LNK2019: unresolved external symbol "public: void __thiscall simBase::SimObject::updateParamValue<class osg::Vec4f>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class osg::Vec4f)" (??$updateParamValue@VVec4f@osg@@@SimObject@simBase@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@VVec4f@osg@@@Z) referenced in function "public: void __thiscall simTerrain::ITerrainDrawable::setSatelliteTextureBorders(class osg::Vec2f,class osg::Vec2f)" (?setSatelliteTextureBorders@ITerrainDrawable@simTerrain@@QAEXVVec2f@osg@@0@Z)

Why do I get this error?

Everything works fine if I don't use template function but I need it.

If I move this function to simTerrain library it works fine but I don't want to use duplicate function because there are many libraries like simTerrain.


Solution

  • C++ does not really support the separate compilation of template code - you need to put the definition of the template in a header file.