Reputation: 678
I am attempting to link my program with the static MFC libraries, as shown in the following target code in my makefile (the rest ommitted):
!include <win32.mak>
all: window.exe
.cpp.obj:
$(cc) $(cdebug) $(cflags) $(cvars) $*.cpp
window.exe: window.obj
$(link) $(ldebug) $(guiflags) -out:window.exe window.obj mfc100d.lib mfcs100d.lib $(winlibs)
Unfortunately, nmake
halts the linking process and displays the following errors:
nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in mfc100d.lib(mfc100d.dll)
LINK : warning LNK4098: defaultlib 'nafxcwd.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
mfcs100d.lib(appmodul.obj) : warning LNK4217: locally defined symbol __setmbcp imported in function "int __stdcall AfxInitialize(int,unsigned long)" (?AfxInitialize@@YGHHK@Z)
window.exe : fatal error LNK1169: one or more multiply defined symbols found
Besides telling the Microsoft Linking tool to ignore nafxcwd.lib
, do I need to specify any additional library names or preprocessor definitions?
Upvotes: 0
Views: 3428
Reputation: 16769
First of all: do not use NODEFAULTLIB option. It is not meant for you at this stage. This is important. You have to tweak other options instead.
The way MFC is linked to you app must be the same way that the CRT is linked. MFC linking is set in your project properties > Configuration Properties > General > Use of MFC. There are two options, for static and dynamic linking.
CRT linking (and compiling) is set in your project properties > Configuration Properties > C/C++ > Code Generation > Runtime Library. There should be 4 options. Those that do not have DLL in the name are for static linking. Those that have DLL in the name are for dynamic linking. Those that have Debug in the name are meant for Debug configurations.
Just match these two options and the problem should go away (or if you have more than one problem then at least the text in output build should change).
In nmake
project if you want to link to static MFC then do not link mfc100d.lib
and mfcs100d.lib
libraries to your project, because those libs are meant for dynamic linking. Link to nafxcwd.lib
for Debug (which you already are linking, otherwise there would not be any error), or nafxcw.lib
for Release. Also, must define _AFX
macro, and there has to be an option either /MT
for Release, or /MTd
for Debug build.
If however you want to link MFC dynamically then define _AFXDLL
macro, and add option /MD
(for Release) or /MDd (for Debug build). For more info see section "Building with NMAKE" in Microsoft's technical note TN033.
One problem may be that you have a third party library that is already set in a certain way, and you must then follow that way in your settings. Another problem is if you have two such libraries, but they are set in different ways. Then it is impossible to link them together. If this concerns you I'll expand the answer.
Upvotes: 2