edition
edition

Reputation: 678

How to link with static MFC libraries with the Microsoft Linker tool

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:

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

Answers (1)

Dialecticus
Dialecticus

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

Related Questions