LNK2019:未解析的外部符号_main在函数___tmainCRTStartup中引用 [英] LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

查看:266
本文介绍了LNK2019:未解析的外部符号_main在函数___tmainCRTStartup中引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下错误LNK2019:在函数_ _tmainCRTStartup中引用的未解析的外部符号 main

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.

我试过:

  • wWinMainCRTStartup as entry point in the linker properties http://social.msdn.microsoft.com/Forums/en/vclanguage/thread/14e85604-6929-4707-a22e-8cdf596926a6
  • 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 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
  • try Project + properties, C/C++, Code generation, Buffer security check = No http://social.msdn.microsoft.com/Forums/hi-IN/vclanguage/thread/e2ea62c3-beb3-47a4-8963-60b799e3375a
  • 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 http://social.msdn.microsoft.com/Forums/hi-IN/vclanguage/thread/e2ea62c3-beb3-47a4-8963-60b799e3375a
  • commented out headers in main.cpp except 'using namespace std' and #include - 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 http://social.msdn.microsoft.com/Forums/ar-SA/vcgeneral/thread/105a366f-c38d-4c1c-9278-eca64589e7ca and http://social.msdn.microsoft.com/Forums/zh/Vsexpressvc/thread/341780c2-162e-4b36-9402-283c0cf7c0ac

尚未尝试怀疑这些也不会工作:

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) http://social.msdn.microsoft.com/Forums/zh/Vsexpressvc/thread/341780c2-162e-4b36-9402-283c0cf7c0ac
  • using cmake to build on windows 7 x64 http://hdf-forum.184993.n3.nabble.com/error-LNK2019-unresolved-external-symbol-main-referenced-in-function-tmainCRTStartup-td3138042.html

我得到这个错误和解决方案是什么?

why am I getting this error and what is the solution?

推荐答案

您的项目类型是什么?如果它是一个Win32项目,你的入口点应该是(w)WinMain 。如果它是一个Win32控制台项目,那么它应该(w)main _tmain 的名称是#defined为 main wmain

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

项目类型可以在项目属性,链接器,系统,子系统下看到。它会说控制台或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[])



or

int _tmain()

确认它是其中之一。

编辑:

_TCHAR,放置

#include <tchar.h>

如果你认为问题是其中一个头,转到文件的属性与main (),并在预处理器下启用生成预处理文件。然后编译。您将得到一个名称为.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...

EDIT2:

用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建立。 Unicode支持在Windows 95/98 / Me中非常不完整。主要的API是ANSI,并且Unicode版本存在于此处,但不是普遍存在。此外,VS调试器显示Unicode字符串时遇到问题。在NT内核OS(即Windows 2000 / XP / Vista / 7/8/10)中,Unicode支持是主要的,并且在顶部添加了ANSI函数。所以从VS2005开始,项目创建时的默认值是Unicode。这意味着 - wmain。它们不能保留相同的入口点名称,因为参数类型不同。 _TCHAR是#defined为char或wchar_t。所以_tmain是主要的(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).

code> _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 包含行。

这篇关于LNK2019:未解析的外部符号_main在函数___tmainCRTStartup中引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆