29

I have the following error:

LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

There are a lot of threads relating to this error, but none of those solutions worked for me. And, none explained why this error is here.

I tried:

  • wWinMainCRTStartup as entry point in the linker properties (thread)
  • set the linker to "Windows" (same thread as above)
  • Right click on solution name->Add->Existing Item->file with main (same thread as above)
  • #include <tchar.h> (error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup)
  • try Project + properties, C/C++, Code generation, Buffer security check = No (thread)
  • Options: C/C++, Code generation, Runtime library=/MTd; C/C++, Code generation, Basic Runtime Checks=default; C/C++, Code generation, Buffer security check=No; Linker, Advanced, Entry Point=main (thread)
  • commented out headers in main.cpp except using namespace std and #include <iostream> - results in cascading and snowballing error from functions that referencing those headers
  • I deleted everything in main.cpp except test code, and excluded all source files except main.cpp; as expected it worked, so a small step in the right direction. The problem must be with one of the header files.
  • create new project with Win32 Windows application template (thread and thread)

Have not tried and suspect that these also will not work:

  • use int main() (not sure what they mean, file name or main function name) (thread)
  • using cmake to build on Windows 7 x64 (thread)

Why am I getting this error, and what is the solution?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
forest.peterson
  • 755
  • 2
  • 13
  • 30

12 Answers12

31

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.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • I tried changing int main() to int _tmain() and there is still a lnk2019 error – forest.peterson Jun 28 '12 at 15:36
  • int _tmain(int argc, _TCHAR* argv[]) results in error C2061: syntax error : identifier '_TCHAR'; I tried variations of this with int main(int argc, char** argv) that returned the intial lnk2019 error – forest.peterson Jun 28 '12 at 15:40
  • if I remove all the headers with the exception of #include and comment out all but a test line of text then this compiles, so it seems like the issue is in one of the header files - what is the process to track-down an issue in a header that results in the lnk2019 main error – forest.peterson Jun 28 '12 at 15:44
  • 2
    If you're getting an error on _TCHAR, place an #include Also, see my edit. – Seva Alekseyev Jun 28 '12 at 15:47
  • that worked. I did not make to connection with #include given in some of the threads I referenced and int _tmain(int argc, _TCHAR* argv[]); can you explain what is going on functionally - what was wrong with int main(), why all this other stuff? – forest.peterson Jun 28 '12 at 16:12
  • We say that by upvoting the answer :) – Seva Alekseyev Jun 04 '13 at 18:47
  • I changed Subsytem from Console to Windows and it solved my problem!! – support_ms Dec 16 '16 at 13:41
6

Because it hasn't been mentioned yet, this was the solution for me:

I had this error with a DLL after creating a new configuration for my project. I had to go to Project Properties -> Configuration Properties -> General and change the Configuration Type to Dynamic Library (.dll).

So if you're still having trouble after trying everything else, it's worth checking to see if the configuration type is what you expect for your project. If it's not set correctly, the compiler will be looking for the wrong main symbol. In my case, it was looking for WinMain instead of DllMain.

Dizzyspiral
  • 745
  • 6
  • 12
3

I got this error while I was trying to turn off precompiled headers in a Console Application Project and removing the header file stdafx.h

To fix this go to your project properties -> Linker -> SubSystem and change the value to Not Set

In your main class, use the standard C++ main function protoype that others have already mentioned :

int main(int argc, char** argv)
2

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.

TomSmartBishop
  • 415
  • 3
  • 12
1

I find that when i choose option of Project->Properties->Linker->System->SubSystem->Console(/subsystem:console), and then make sure include the function : int _tmain(int argc,_TCHAR* argv[]){return 0} all of the compiling ,linking and running will be ok;

Shania
  • 11
  • 1
1

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.

Pagefault
  • 339
  • 1
  • 12
1

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.

DarenW
  • 16,549
  • 7
  • 63
  • 102
0

this main works in both linux and windows - found it through trial and error and help from others so can't explain why it works, it just does int main(int argc, char** argv)

no tchar.h necessary

and here is the same answer in Wikipedia Main function

forest.peterson
  • 755
  • 2
  • 13
  • 30
0

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.

Hendy Irawan
  • 20,498
  • 11
  • 103
  • 114
0

I had this happen in Visual Studio 2015 too for an interesting reason. Just adding it here in case it happens to someone else.

I already had number of files in project and I was adding another one that would have main function in it, however when I initially added the file I made a typo in the extension (.coo instead of .cpp). I corrected that but when I was done I got this error. It turned out that Visual Studio was being smart and when file was added it decided that it is not a source file due to the initial extension.

Right-clicking on file in solution explorer and selecting Properties -> General -> ItemType and setting it to "C/C++ compiler" fixed the issue.

Sebastian K
  • 6,235
  • 1
  • 43
  • 67
0

I had the problem before, but it was solved. The main problem was that I mistakenly spell the int main() function. Instead of writing int main() I wrote int mian()....Cheers !

  • 1
    This answer is too specific to yourself. Since `main` is not misspelled anywhere in the OP's question, there's no reason to assume this is the problem. – lwassink Jul 19 '16 at 02:46
  • 1
    OP didn't show any source code, he just stated the error. I had the same error today, but later realized that i misspelled the word main, so after correcting it the error disappear .. – Serign Modou Bah Jul 19 '16 at 07:28
0

Screen snapshot Visual Studio 2015

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.

LastBlow
  • 617
  • 9
  • 16