Reputation: 365
From this post, I need to export function ci_find_substr
, so in my header file, i declare function:
template<typename Twst>
__declspec(dllexport) int ci_find_substr (const Twst& str1,const Twst& str2 ,
const std::locale& loc = std::locale());
but when compile this code from another project:
int k=ci_find_substr (wstring(mycstring), wstring(L".doc");
I get an error Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol "int __cdecl ci_find_substr (class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >,class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >,class std::locale const &)" (?ci_find_substr @@YAHV?$basic_string@_WU?$char_traits@_W@std@@V?
I also tried the same thing in this post
template<typename Twst>
int ci_find_substr (const Twst& str1,const Twst& str2,
const std::locale& loc = std::locale());
__declspec(dllexport) int ci_find_substr (const wstring& str1,const wstring& str2,
const std::locale& loc = std::locale()); // Explicit instantiation
But still no success! Where did I go wrong? How do I fix my code?
Upvotes: 0
Views: 839
Reputation: 9682
You can't export a template from a DLL, you can only export a specialisation of said template!
#ifdef COMPILING_DLL
// ensure you are declaring 'COMPILING_DLL' somewhen when building your DLL
#define DLL_EXPORT __declspec(dllexport)
#else
// when using the function it must be DLL import!
#define DLL_EXPORT __declspec(dllimport)
#endif
// header
template<typename Twst>
DLL_EXPORT int ci_find_substr (const Twst& str1,const Twst& str2 ,
const std::locale& loc = std::locale());
// in the source file...
//
// |-- 'template'
// | specialisation type--|
// | |-- DLLEXPORT |
// V V V
template DLL_EXPORT int ci_find_substr<std::wstring>(
const std::wstring& str1,
const std::wstring& str2,
const std::locale& loc);
Upvotes: 1