I have a dll (my_library.dll
) that exports a struct using __declspec(dllexport)
. Since this struct contains an std::vector<std::wstring> member
, I've also exported functions for it like so:
template class __declspec(dllexport) std::allocator<std::wstring>;
template class __declspec(dllexport) std::vector<std::wstring>;
Please note that I've defined macros such that dll exports above struct and vector when compiling and they are imported (via __declspec(dllimport))
when the dll is being used by another application.
The above dll builds fine.
Now this my_library.dll (and the corresponding my_library.lib
) is linked to an exe (my_exe.exe
). This exe has a .cpp file (exe_source.cpp
) that defines a global std::vector<std::wstring>
variable. This source file compiles fine.
However when building this exe, I get the following error:
my_library.lib(my_library.dll) : error LNK2005: "public: __thiscall std::vector,class std::allocator
,class std::allocator,class std::allocator
::~vector,class std::allocator ,class std::allocator,class std::allocator (void)" (??1?$vector@V?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@V?$allocator@V?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@@2@@std@@QAE@XZ) already defined in exe_source.obj
What I suspect is that the my_library.dll has all std::vector<std::wstring>
functions defined and exported, and using the global std::vector<std::wstring>
variable in the exe_source.cpp
is also resulting in definition of many std::vector<std::wstring>
functions, leading to linker complaining that multiple definitions of such functions are found.
Am I understanding the error correctly?
And how to fix this?
Thanks for your time.
vector
isn't going to be the samevector
the DLL is using. – PaulMcKenzie