LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

前端 未结 12 2012
广开言路
广开言路 2020-12-02 18:36

I have the following error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup,

There are a lot of threads relating to this error bu

相关标签:
12条回答
  • 2020-12-02 19:27

    If you have a "Win32 project" + defined a WinMain and your SubSystem linker setting is set to WINDOWS you can still get this linker error in case somebody set the "Additional Options" in the linker settings to "/SUBSYSTEM:CONSOLE" (looks like this additional setting is preferred over the actual SubSystem setting.

    0 讨论(0)
  • 2020-12-02 19:27

    I had this problem minutes ago. It went away when I added 'extern "C"' to the main() definition.

    Oddly, another simple program I wrote yesterday is almost identical, does not have the extern "C", yet compiled without this linker error.

    This makes me think the problem is some subtle setting to be found deep in some configuration dialog, and that 'extern "C"' doesn't really solve the underlying problem, but superficially makes things work.

    0 讨论(0)
  • 2020-12-02 19:28

    I had this error when accidentally putting the wmain inside a namespace. wmain should not be in any namespace. Moreover, I had a main function in one of the libs I was using, and VS took the main from there, what made it even stranger.

    0 讨论(0)
  • 2020-12-02 19:28

    Set the system to console, following the previous suggestions. Only, also had to change the character set to Unicode, see the snapshot of Visual Studio 2015 above.

    0 讨论(0)
  • 2020-12-02 19:30

    What is your project type? If it's a "Win32 project", your entry point should be (w)WinMain. If it's a "Win32 Console Project", then it should be (w)main. The name _tmain is #defined to be either main or wmain depending on whether UNICODE is defined or not.

    If it's a DLL, then DllMain.

    The project type can be seen under project properties, Linker, System, Subsystem. It would say either "Console" or "Windows".

    Note that the entry point name varies depending on whether UNICODE is defined or not. In VS2008, it's defined by default.

    The proper prototype for main is either

    int _tmain(int argc, _TCHAR* argv[])
    

    or

    int _tmain()
    

    Make sure it's one of those.

    EDIT:

    If you're getting an error on _TCHAR, place an

    #include <tchar.h>
    

    If you think the issue is with one of the headers, go to the properties of the file with main(), and under Preprocessor, enable generating of the preprocessed file. Then compile. You'll get a file with the same name a .i extension. Open it, and see if anything unsavory happened to the main() function. There can be rogue #defines in theory...

    EDIT2:

    With UNICODE defined (which is the default), the linker expects the entry point to be wmain(), not main(). _tmain has the advantage of being UNICODE-agnostic - it translates to either main or wmain.

    Some time ago, there was a reason to maintain both an ANSI build and a Unicode build. Unicode support was sorely incomplete in Windows 95/98/Me. The primary APIs were ANSI, and Unicode versions existed here and there, but not pervasively. Also, the VS debugger had trouble displaying Unicode strings. In the NT kernel OSes (that's Windows 2000/XP/Vista/7/8/10), Unicode support is primary, and ANSI functions are added on top. So ever since VS2005, the default upon project creation is Unicode. That means - wmain. They could not keep the same entry point name because the parameter types are different. _TCHAR is #defined to be either char or wchar_t. So _tmain is either main(int argc, char **argv) or wmain(int argc, wchar_t **argv).

    The reason you were getting an error at _tmain at some point was probably because you did not change the type of argv to _TCHAR**.

    If you're not planning to ever support ANSI (probably not), you can reformulate your entry point as

    int wmain(int argc, wchar_t *argv[])
    

    and remove the tchar.h include line.

    0 讨论(0)
  • 2020-12-02 19:36

    In my case, it's because I accidentally removed (not deleted) the stdafx.h and targetver.h files in the Header Files section.

    Add these files back to Header Files and the problem is solved.

    I had these:

    #pragma comment( linker, "/entry:\"mainCRTStartup\"" ) // set the entry point to be main()
    

    I just need to comment that (by prepending //) and it's good.

    0 讨论(0)
提交回复
热议问题