Microsoft KB Archive/126646

= You receive error messages when you try to compile an MFC application by using the single-threaded run-time library in Visual C++: "Error LNK2001: unresolved external symbol" =

Article ID: 126646

Article Last Modified on 1/6/2006

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 4.2 Professional Edition
 * Microsoft Visual C++ 4.2 Enterprise Edition
 * Microsoft Visual C++ 4.1 Subscription
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 2.1
 * Microsoft Visual C++ 2.0 Professional Edition

-



This article was previously published under Q126646



SYMPTOMS
When compiling an MFC application using the single-threaded run-time library, you receive the following two unresolved external error messages:

nafxcwd.lib(thrdcore.obj) : error LNK2001:

unresolved external symbol "__beginthreadex"

nafxcwd.lib(thrdcore.obj) : error LNK2001:

unresolved external symbol "__endthreadex"



CAUSE
Starting with version 3.0, all MFC classes are "thread safe" and require the multi-threaded run-time libraries to link successfully. Many people try to use the single-threaded run-time libraries because they assume these libraries are needed to enable the application to run in Win32s. This is not the case. MFC versions 3.0 and later will use a single thread, so as long as you are not creating additional threads in the application, the application will run under Win32s.



RESOLUTION
To avoid these unresolved external errors, do not set the Project Settings to Single-Threaded for an MFC version 3.0 or later application. This setting can be changed by doing the following:
 * On Microsoft Visual C++ 2005 or on Microsoft Visual C++ .NET

To set the option, read the online help topic under Setting Visual C++ Project Properties.
 * On Microsoft Visual C ++ 2., 5.0, and 6.0
 * Select the Project menu.
 * Select the Settings... option.
 * Select the C/C++ tab.
 * Select Code Generation on the Category list box.
 * Finally, make a selection other than Single-Threaded on the Use Run Time Library list box.
 * On Microsoft Visual C++ 4. 
 * Select the Build menu.
 * Select the Settings... option.
 * Select the C/C++ tab.
 * Select Code Generation on the Category list box.
 * Finally, make a selection other than Single-Threaded on the Use Run Time Library list box.



STATUS
This behavior is by design.



MORE INFORMATION
For Win32s, MFC version 3.0 and later versions do not create additional threads. However, the MFC does use Thread Local Storage (TLS) in the multithreaded libraries to provide per-process data in DLLs that are used by multiple applications under Win32s.

In the Project Settings dialog box, if you select the run-time library option to link "Multithreaded using DLL," you can take advantage of using the shared DLL, MSVCRT 0.DLL (where  represents the major version of Visual C++). There are separate versions of MSVCRT 0.DLL for Windows NT and Win32s that cannot be interchanged. "Multithreaded using DLL" is required when building an AFXDLL.

Linking to the multithreaded libraries may cause multiple defined symbol errors. This usually occurs when the application uses user-created or third-party static libraries that were created as single-threaded.

The best solution in this case is to rebuild the static libraries to use the same library options as MFC (either /MT or /MD, depending on which MFC lib is used).

In this case, you may be able to use /NOD:LIBC.LIB. However, if the static library accesses any static global C Run-Time data such as _errno or stdin, you will get unresolved externals on these symbols.

One solution which will always work is to isolate the single-threaded code into a DLL, and call it, protected by semaphores, from the multithreaded MFC code.

