0

I define this in dll compile process.

# if defined(BUILD_DLL)
# define COMMON_API __declspec(dllexport)
# else
# define COMMON_API __declspec(dllimport)
#endif

and:

template <typename T> class COMMON_API Singleton {
public:
static T& GetInstance()
{
    static T s_instance;
    return s_instance;
};
    ***
};

and:

class COMMON_API CRegExecThreadPool:public Singleton<CRegExecThreadPool>
{
  friend class Singleton<CRegExecThreadPool>;
  ***
}

then I try use it in exe:

Commonlib::CRegExecThreadPool::GetInstance();

but got the link error,I don't understand why. what can I do?

"__declspec(dllimport) public: static class Commonlib::CRegExecThreadPool & __cdecl Commonlib::Singleton::GetInstance(void)"

error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: static class Commonlib::CRegExecThreadPool & __cdecl Commonlib::Singleton::GetInstance(void)" (_imp?GetInstance@?$Singleton@VCRegExecThreadPool@Commonlib@@@Commonlib@@SAAEAVCRegExecThreadPool@2@XZ)

guijiyang
  • 11
  • 2
  • 1
    you can't export template from dll. `template` is only compile time entity (it is only a recipe). Only instances of template become part of final code. You can export from dll template instantiation for some specific types. – Marek R Feb 06 '22 at 12:51
  • yes, thank you. you are right! – guijiyang Feb 06 '22 at 12:57

0 Answers0