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?
Who is Participating?
AaronReamsConnect With a Mentor Commented:
hi nitmir,

I just wanted to let you know that I was able to compile and run this just fine.  Basically what I did was

1) Create new MFC application project in VS.NET 2003 (with static linking to MFC)
2) Removed all files from the project.
3) Created a new file called TestString.cpp and pasted your code in
4) In project options set "Not using precompiled headers"
5) Compiled and run.

Note: you have one error in your code that cause an exception.

--> csTest.Format("String is:\r\n%s", s);
shoule be
--> csTest.Format("String is:\r\n%s", s.c_str());

So if I were you I'd try the steps I mentioned above again and see if you can get it working.  Let me know if I can help you out with anything else.

One other tip is which might make a difference is, for a previous project I worked on, I had to move PlatformSDK\include directory in Tools > Options > Projects > VC++ directories to the top of the list.  This might actually be your problem.
how about
     csTest.Format("String is:\r\n%s", s.c_str ());
nitmirAuthor Commented:
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.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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;

#include <string.cpp>

oops, i meant...

#include <string.h>
nitmirAuthor Commented:
<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
nitmirAuthor Commented:
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 :(
nitmirAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.