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.
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.


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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
centeredAverage challenge 8 186
only14 challenge 19 84
matchUp  challenge 9 126
Making an alias 7 115
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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.…

696 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