error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
Even if your project has a main()
method, the linker sometimes gets confused. You can solve this issue in Visual Studio 2010 by going to
Project -> Properties -> Configuration Properties -> Linker -> System
and changing SubSystem
to Console.
LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
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.
LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup with DLLs
Almost four years later I finally revisited this issue and after going through all object files to see which one was the culprit I finally found the error in src/SDL.c
. It has these lines at the end:
#if defined(__WIN32__)
#if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
/* Need to include DllMain() on Watcom C for some reason.. */
BOOL APIENTRY
_DllMainCRTStartup(HANDLE hModule,
DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#endif /* building DLL with Watcom C */
#endif /* __WIN32__ */
So SDL always defined a _DllMainCRTStartup
symbol which seems to have confused the linker and led to the error shown in the OP. Killing the code above (or defining HAVE_LIBC
) finally solves the problem and I can build SDL2 just fine with /MT
(though I still don't know why compiling with /MD
didn't cause any problems because AFAICS _DllMainCRTStartup
will also be defined when compiling with /MD
but it doesn't seem to cause any harm in that case).
Issue solved, finally.
error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup, but this time it's NOT a Windows/Console problem!
SDL_main.h is included automatically from SDL.h, so you always get the nasty #define.
Just write:
#include <SDL.h>
#undef main
And it should work fine
Related Topics
Does True Equal to 1 and False Equal to 0
Why Can't We Pass Arrays to Function by Value
What Does the Standard Library Guarantee About Self Move Assignment
How to Evaluate Mathematical Expressions in C++
How to Take Screenshots of a Window with C++ in Windows
Emulate "Double" Using 2 "Float"S
Sending an Email from a C/C++ Program in Linux
Determine If Linux or Windows in C++
Using Hardware Performance Counters in Linux
Get Computer Name and Logged User Name
(C/C++) How to Generate Executable File That Can Run on Both Windows and Linux
Execute a Process from Memory Within Another Process
How to Force Linker to Use Shared Library Instead of Static Library
How to Prevent Paging for One Program/Process
How to Build an Import Library (.Lib) and a Dll in Visual C++