Link to home
Start Free TrialLog in
Avatar of nitmir
nitmir

asked on

Problem statically linking to MFC when STL is used

I am trying to create a simple application which statically links to MFC (so it does not require mfc*.dll), and which also uses STL.  With VS6 this was easy, but with VS.NET2003 I can't figure out how to get it to work.  My app is based on the MFC Application AppWizard, although I removed all files (including disabling precompiled header files) and added just the code below.

Here's my sample code:

#include <afx.h>
#include <string>
using namespace std;

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
      string s;
      s = "This ";
      s += "is a test.";
      CString csTest;
      csTest.Format("String is:\r\n%s", s);
      MessageBox(NULL, csTest, NULL, 0);
      return 0;
}

When compiling with "Use MFC in a Shared DLL" this works perfectly.  However, if I change it to "Use MFC in a Static Library" I get the following errors:

StaticMFC.obj : error LNK2019: unresolved external symbol "public: void __thiscall std::_String_base::_Xran(void)const " (?_Xran@_String_base@std@@QBEXXZ) referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::erase(unsigned int,unsigned int)" (?erase@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@II@Z)
StaticMFC.obj : error LNK2019: unresolved external symbol "public: void __thiscall std::_String_base::_Xlen(void)const " (?_Xlen@_String_base@std@@QBEXXZ) referenced in function "protected: bool __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::_Grow(unsigned int,bool)" (?_Grow@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@IAE_NI_N@Z)

If I remove the code that uses the STL string class, these go away and the project compiles just fine.

What am I missing here?
Avatar of Member_2_1001466
Member_2_1001466

how about
     csTest.Format("String is:\r\n%s", s.c_str ());
Avatar of nitmir

ASKER

The problem is not related to my specific use of the string class; rather, it's with the linkage in general.

I can comment out the entire function, leaving only "string s; s = "This "; return 0;" and it will still not compile.
In that case it seem to be related to VS.NET which I don't have. Other experts can certainly help.
I doubt this will work but I read about a problem similar to yours a while ago...

Try changing
#include <string>
using namespace std;

to
#include <string.cpp>

oops, i meant...

#include <string.h>
Avatar of nitmir

ASKER

<string.h> is a header which includes string-related routines in the C runtimes (like strlen, strspn, etc.).  What I need is the string C++ headers, including the string class.  So that header file will only give me compile errors because the string class can't be found.
Try using std::string , removing the includes
Avatar of nitmir

ASKER

If I remove the "#include <string>", it doesn't know the string class, so even std::string isn't known and I get compiler errors.

If I remove the "using namespace std;" and instead qualify the class to std::string, it compiles, but I get the same errors.

Still no go :(
ASKER CERTIFIED SOLUTION
Avatar of AaronReams
AaronReams

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nitmir

ASKER

This isn't the first time I've heard that someone else has managed to compile a similar application, and I'm starting to suspect my VS.NET 2003 installation.  I'm going to repair the install, make sure I have all required components, and try again.  I'll post another comment as soon as I do.
Yeah, I hear you.   Did you try moving the platform SDK include directory to the top of the list.  I'm not sure how that will affect your problem, but that fixed some ATL linking errors I was having a while back.

Good luck.