The solution for unresolved external symbol _main is prompted

  • 2020-05-05 11:48:47
  • OfStack

unresolved   external   symbol   _main When creating MFC project,   does not use MFC   AppWizard wizard. If   does not set the project parameters properly,   will generate many connection errors at compile time.  , error   LNK2001,   typical error:

libcmtd.lib(crt0.obj)   :   error   LNK2001:   unresolved   external   symbol   _main

LIBCD.lib(wincrt0.obj)   :   error   LNK2001:   unresolved   external   symbol   _WinMain@16

msvcrtd.lib(crtexew.obj)   :   error   LNK2001:   unresolved   external   symbol   _WinMain@16

nafxcwd.lib(thrdcore.obj)   :   error   LNK2001:   unresolved   external   symbol   __beginthreadex

nafxcwd.lib(thrdcore.obj)   :   error   LNK2001:   unresolved   external   symbol   __endthreadex





Here's how:

1.   Windows subsystem setup error,   prompt:

libcmtd.lib(crt0.obj)   :   error   LNK2001:   unresolved   external   symbol   _main

Windows project will use Windows subsystem,   instead of Console,   can be set like this:

[Project]   -- >   [Settings]   -- >   select the "Link" property page,

Change /subsystem:console to /subsystem:windows
in Project   Options




2.   Console subsystem setting error,   prompt:

LIBCD.lib(wincrt0.obj)   :   error   LNK2001:   unresolved   external   symbol   _WinMain@16

Console project to use Console subsystem,   instead of Windows,   Settings:

[Project]   -- >   [Settings]   -- >   select "Link" property page,

In Project   Options change /subsystem:windows to /subsystem:console





3.   program entry setting error,   prompt:

msvcrtd.lib(crtexew.obj)   :   error   LNK2001:   unresolved   external   symbol   _WinMain@16

In general,   MFC project entry function is WinMain,   if the Unicode version of the project is compiled,   entry must be changed to wWinMainCRTStartup,   so you need to reset the program entry:

[Project]   -- >   [Settings]   -- >   select "Link" property page,

In Category, select Output,

Then fill wWinMainCRTStartup in Entry-point   symbol,   is





4.   thread runtime library setting error,   prompt:

nafxcwd.lib(thrdcore.obj)   :   error   LNK2001:   unresolved   external   symbol   __beginthreadex

nafxcwd.lib(thrdcore.obj)   :   error   LNK2001:   unresolved   external   symbol   __endthreadex

This is because when MFC wants to use multithreading,   needs to change the Settings:

[Project]   -- >   [Settings]   -- >   select "C/C++" property page,

From Category, choose Code   Generation,

Then choose Debug   Multithreaded or multithreaded
from Use   run-time   library
Among them, the

Single-Threaded   single thread static link library (release version)

Multithreaded   multithreaded static link library (release version)

multithreaded   DLL   multithreaded dynamic link library (release version)

Debug   Single-Threaded   single thread static link library (debug version)

Debug   Multithreaded   multithreaded static link library (debug version)

Debug   Multithreaded   DLL   multithreaded dynamic link library (debug version)

Single-threaded:   is mostly used in
environment when DOS does not require multi-threaded calls
Multithreading:   can run
concurrently
Static libraries:   directly links libraries to programs Link,   can run
without MFC libraries
Dynamic library:   requires the corresponding DLL dynamic library for   programs to run

release version:   was officially released using

debug version: used in the debugging phase of  

Related articles: