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
  • 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.
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.


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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
only14 challenge 19 81
Windows Process running 3 58
dialer.exe 4 64
My project did see openJDK that I installed. What could be the problem 7 152
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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.…

828 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