Problem statically linking to MFC when STL is used

Posted on 2004-09-17
Last Modified: 2013-11-25
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?
Question by:nitmir
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
LVL 13

Expert Comment

ID: 12083726
how about
     csTest.Format("String is:\r\n%s", s.c_str ());

Author Comment

ID: 12083788
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.
LVL 13

Expert Comment

ID: 12083822
In that case it seem to be related to VS.NET which I don't have. Other experts can certainly help.
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.


Expert Comment

ID: 12089500
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>


Expert Comment

ID: 12089505
oops, i meant...

#include <string.h>

Author Comment

ID: 12091627
<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.
LVL 15

Expert Comment

ID: 12095325
Try using std::string , removing the includes

Author Comment

ID: 12096191
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 :(

Accepted Solution

AaronReams earned 200 total points
ID: 12104640
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.

Author Comment

ID: 12107461
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.

Expert Comment

ID: 12107489
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.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question