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)#include <tchar.h>
(error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup)main.cpp
except using namespace std
and #include <iostream>
- results in cascading and snowballing error from functions that referencing those headersmain.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.Have not tried and suspect that these also will not work:
int main()
(not sure what they mean, file name or main function name) (thread)cmake
to build on Windows 7 x64 (thread)Why am I getting this error, and what is the solution?
So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.
If the linker cannot find the external definition for a symbol that has no definition internally, it reports an Unresolved External Symbol error.
To fix this issue, add the /NOENTRY option to the link command. This error can occur if you use incorrect /SUBSYSTEM or /ENTRY settings in your project. For example, if you write a console application and specify /SUBSYSTEM:WINDOWS, an unresolved external error is generated for WinMain .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With