Exporting std::string from a DLL does not export std::string::npos

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello.
Ive found a problem using a DLL where std::string is explicitly instantiated and dllexported.
The problems shows when linking a client of the DLL that atempt to use std::string::npos.
The error is: error LNK2001: unresolved external symbol "public: static unsigned int const std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::npos".
Ive reduced the problem to a simple DLL that explicitrly instantiate and dllexport std::string and has a function that return a std::string, and a client EXE that calls that function and then uses std::string::npos.
Ive also tried adding a declaration of npos in MyDLL.hpp, as sugested by Sthephan T. Lavavej in some related post, but it didnt work.
MyDLL.hpp
<pre class="prettyprint #ifndef MY_DLL_HPP_INCLUDED
#define MY_DLL_HPP_INCLUDED

// c++ includes
#include <string>

#if defined(BUILDING_MY_LIB)
# define MY_DLL_API __declspec(dllexport)
# define MY_DLL_TEMPLATE
# define MY_DLL_DECLSPECIFIER __declspec(dllexport)
#else
# define MY_DLL_API __declspec(dllimport)
# define MY_DLL_TEMPLATE extern
# define MY_DLL_DECLSPECIFIER __declspec(dllimport)
#endif

MY_DLL_TEMPLATE template class MY_DLL_DECLSPECIFIER std::allocator<char>;
MY_DLL_TEMPLATE template class MY_DLL_DECLSPECIFIER std::basic_string<char>;

namespace my { namespace dll {

std::string MY_DLL_API my_dll_function();

}} // my::dll namespace

#endif /* MY_DLL_HPP_INCLUDED */
[/code]
MyDLL.cpp
<pre class="prettyprint // my includes
#define BUILDING_MY_LIB
#include "MyDLL.hpp"

namespace my { namespace dll {

std::string my_dll_function() {
return "MyDLL";
}

}} // my::dll namespace
[/code]
MyDLLClient.cpp
<pre class="prettyprint //c++ includes
#include <iostream>

// MyDLL includes
#include <MyDLL.hpp>

int main() {
std::string str = my::dll::my_dll_function();
std::string::size_type myPos = str.find_last_of( : );
std::cerr << myPos << std::endl;
return 0;
}
[/code]


View the full article
 
Back
Top