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" (126646)



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
  • Microsoft Visual C++, 32-bit Learning Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 5.0
  • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
  • Microsoft Visual C++, 32-bit Professional Edition 4.2
  • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
  • Microsoft Visual C++, 32-bit Editions 4.1
  • Microsoft Visual C++, 32-bit Editions 4.0
  • Microsoft Visual C++, 32-bit Editions 2.1
  • Microsoft Visual C++, 32-bit Editions 2.0

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.x, 5.0, and 6.0
    1. Select the Project menu.
    2. Select the Settings... option.
    3. Select the C/C++ tab.
    4. Select Code Generation on the Category list box.
    5. Finally, make a selection other than Single-Threaded on the Use Run Time Library list box.
  • On Microsoft Visual C++ 4.x
    1. Select the Build menu.
    2. Select the Settings... option.
    3. Select the C/C++ tab.
    4. Select Code Generation on the Category list box.
    5. 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, MSVCRTx0.DLL (where x represents the major version of Visual C++). There are separate versions of MSVCRTx0.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.

REFERENCES

For more information about the run-time library options, see the online help topic for the compiler options /MT and /MD.

Modification Type:MajorLast Reviewed:1/6/2006
Keywords:kbtshoot kberrmsg kbCompiler kbprb KB126646 kbAudDeveloper