PRB: Link Error LNK2001: Unresolved External Symbol _main (291952)



The information in this article applies to:

  • Microsoft Visual Studio, Enterprise Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

This article was previously published under Q291952

SYMPTOMS

When you build a project of one of these types
  • Win32 console application project
  • Active Template Library (ATL) DLL/EXE project with release configuration
  • A project with main as custom entry point
you may get the following error message from linker:
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main

CAUSE

A Win32 console application requires the main function as the entry point. Linker returns this error message when it cannot find the main function in any of the files attached to the project.

If your project is an ATL project, the release configurations for ATL projects define _ATL_MIN_CRT, which excludes CRT (C runtime) startup code from your .exe or .dll file. Linker gives this error message when you have _ATL_MIN_CRT defined and you are using the CRT functions that need CRT startup code.

If the project is not one of the types listed in the "Symptoms" section of this article, but you are still getting the error, you might have main selected as an entry point symbol for the project in linker settings but have not provided a main function in the files added to the project.

RESOLUTION

  • If you have created a Win32 console application instead of a Win32 application by mistake, there are two ways to fix this problem:
    • From the Project menu, choose Settings, click the C/C++ tab, and change preprocessor definitions from WIN32, _DEBUG, _CONSOLE, and _MBCS to WIN32, _DEBUG, and _WINDOWS. Next, click the Link tab, and under Project Options, change /subsystem:console to /subsystem:windows. -or-

    • Create a new project and select Win32 Application instead of Win32 Console Application. Add the files to that project.
  • If you have created a Win32 console application and forgot to provide a main function, write a main function in one of the source files added to the project.
  • If you have selected main as the custom entry point by mistake, from the Project menu, choose Settings and click the Linker tab. Select output as the category and remove main from the entry-point symbol text box.
  • If your project is an ATL project, there are two ways to fix the problem:
    • Remove _ATL_MIN_CRT from the list of preprocessor definitions to allow CRT startup code to be included: From the Build menu, choose Settings. Hold down the CTRL key while selecting all of the release configurations. On the C/C++ tab, choose the General category, and then remove _ATL_MIN_CRT from the preprocessor definitions edit box. -or-

    • If possible, remove calls to CRT functions that require CRT startup code and use their Win32 equivalents. For example, use lstrcmp instead of strcmp. Known functions that require CRT startup code include the string and floating point functions.

STATUS

This behavior is by design.

MORE INFORMATION

The following are the entry points for the various types of projects:
  • main: console project
  • WinMain: Win32 project
  • DllMain: Win32 DLL project (if you link with the CRT libraries, you don't have to specify one because it adds a default DllMain)
The LNK2001 error specifies the symbol that causes the error. It means that the linker could not find the symbol in your project and the libraries that your project uses. Ensure that the symbol is present in a source file in your project. Otherwise, link to the appropriate static library, import library, or .obj file.

AppWizard-generated ATL projects have this _ATL_MIN_CRT defined for release configurations. ATL is aimed at minimizing the image size and the reliance on run-time .dll files. It provides alternative implementations for common CRT APIs that would otherwise require the CRT startup code. The use of these APIs is controlled by the _ATL_MIN_CRT macro. If you are using _ATL_MIN_CRT, this does not mean that you cannot use the CRT routines. However, if you use the routines that require the CRT startup code, then you will get a linker error that _main is unresolved. Providing your own implementation of _main does not solve this problem.

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

138400 Troubleshooting LNK2001 or L2029 Unresolved External Error

125750 PRB: Error LNK2001: '_WinMain@16': Unresolved External Symbol

131204 PRB: Wrong Project Selection Causes LNK2001 on _WinMain@16

166480 INFO: Active Template Library (ATL) Frequently Asked Questions

MSDN Online Library, Microsoft Visual C++ for Beginners
http://msdn.microsoft.com/library/techart/vc4begin.htm

Modification Type:MinorLast Reviewed:8/15/2005
Keywords:kbprb KB291952