Welcome to Blogs @ Andrew Qu
Blog Index
All blogs
Search results

How to Fix "Multiply Defined Functions" when Mixing C/C++ and MFC Static Libraries


At times, you want to have your MFC application in a single EXE file. Then you need to link MFC libraries statically. Visual Studio will give link errors in this case if you are also linking some C/C++ libraries. This post details the work around to fix the problem.

The Problem

If your project contains static C/C++ libraries and MFC code and MFC is statically linked, you will most likely get link errors:

error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMT.lib

MSDN Solution

MSDN article http://support.microsoft.com/kb/148652 shows how to work around the problem.
Basically, you need to change the order of some default link-in libraries. In project link settings

  1. You first ignore default libraries: Nafxcw.lib;Libcmt.libi>
  2. You then add additional dependencies: Nafxcw.lib;Libcmt.lib
More Details

MSDN article is missing some details. Below are the library names to use if your are using Debug configuration or using Unicode:

For Debug config., the library files are : Nafxcwd.lib;Libcmtd.lib

If you are using Unicode, then change Nafxcwd.lib to Uafxcwd.lib
Another Acompanying Problem

You may also get this error for Release configurations:

MSB6006: "mt.exe" exited with code 31.

To fix this error, you can either do a [Rebuild All] or set link option
[Manifest File] -> [Generate Manifest] to No

*** END ***
Ads from Google
Dr Li Anchor Profi
Engineering anchorage plate design system
©Andrew Qu, 2015. All rights reserved. Code snippets may be used "AS IS" without any kind of warranty. DIY tips may be followed at your own risk.