LNK2019:函数 ___tmainCRTStartup 中引用的未解析外部符号 _main
我有以下错误:
LNK2019:函数 ___tmainCRTStartup 中引用的未解析外部符号 _main
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.
我试过了:
wWinMainCRTStartup
作为链接器属性中的入口点 (线程)- 将链接器设置为Windows"(与上面的线程相同)
- 右击解决方案名称->添加->现有项目->带main的文件(同上线程)
#include <tchar.h>
(错误 LNK2019:未解析的外部符号 _main 在函数 ___tmainCRTStartup 中引用)- 尝试项目 + 属性、C/C++、代码生成、缓冲区安全检查 = 否 (线程)
- 选项:C/C++、代码生成、运行库=/MTd;C/C++,代码生成,基本运行时检查=默认;C/C++,代码生成,缓冲区安全检查=否;链接器,高级,入口点=main (线程)
- 注释掉
main.cpp
中的标头,除了using namespace std
和#include <iostream>
- 导致级联和滚雪球错误引用这些标头的函数 - 我删除了
main.cpp
中除测试代码外的所有内容,并排除了除main.cpp
外的所有源文件;正如预期的那样,它是朝着正确方向迈出的一小步.问题一定出在其中一个头文件上. - 使用 Win32 Windows 应用程序模板创建新项目 (线程 和 线程)
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
exceptusing 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 exceptmain.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:
- 使用
int main()
(不确定它们的意思,文件名或主函数名)(线程) - 使用
cmake
在 Windows 7 x64 上构建 (线程)
- 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?
推荐答案
你的项目类型是什么?如果它是Win32 项目",您的入口点应该是 (w)WinMain
.如果它是Win32 控制台项目",那么它应该是 (w)main
.名称 _tmain
被 #定义为 main
或 wmain
,具体取决于是否定义了 UNICODE.
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.
如果是 DLL,则 DllMain
.
If it's a DLL, then DllMain
.
项目类型可以在项目属性、链接器、系统、子系统下看到.它会说控制台"或Windows".
The project type can be seen under project properties, Linker, System, Subsystem. It would say either "Console" or "Windows".
请注意,入口点名称取决于是否定义了 UNICODE.在VS2008中是默认定义的.
Note that the entry point name varies depending on whether UNICODE is defined or not. In VS2008, it's defined by default.
main 的正确原型是
The proper prototype for main is either
int _tmain(int argc, _TCHAR* argv[])
或
int _tmain()
确保它是其中之一.
如果您在 _TCHAR 上遇到错误,请放置一个
If you're getting an error on _TCHAR, place an
#include <tchar.h>
如果您认为问题出在某个标题上,请使用 main() 转到文件的属性,然后在 Preprocessor 下启用预处理文件的生成.然后编译.您将获得一个具有相同名称且扩展名为 .i 的文件.打开它,看看 main() 函数是否发生了任何不愉快的事情.理论上可以有流氓#defines...
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...
定义 UNICODE(这是默认值)后,链接器期望入口点是 wmain(),而不是 main()._tmain 具有与 UNICODE 无关的优势 - 它可以转换为 main 或 wmain.
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.
前段时间,有理由同时维护 ANSI 构建和 Unicode 构建.在 Windows 95/98/Me 中,Unicode 支持非常不完整.主要的 API 是 ANSI,Unicode 版本随处可见,但并不普遍.此外,VS 调试器无法显示 Unicode 字符串.在 NT 内核操作系统(即 Windows 2000/XP/Vista/7/8/10)中,Unicode 支持是主要的,并且在顶部添加了 ANSI 函数.所以从 VS2005 开始,项目创建时的默认设置是 Unicode.这意味着 - wmain.由于参数类型不同,它们无法保持相同的入口点名称._TCHAR #定义为 char 或 wchar_t.所以 _tmain 要么是 main(int argc, char **argv) 要么是 wmain(int argc, wchar_t **argv).
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).
您在某个时候在 _tmain
收到错误的原因可能是因为您没有将 argv
的类型更改为 _TCHAR**
.
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**
.
如果您不打算支持 ANSI(可能不支持),您可以将入口点重新定义为
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[])
并删除 tchar.h
包含行.
相关文章