C++ LNK2019:未解析的外部符号 _main 在函数 ___tmainCRTStartup 中引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11247699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:59:17  来源:igfitidea点击:

LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

c++windowsvisual-studio-2008windows-7lnk2019

提问by forest.peterson

I have the following error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup,

我有以下错误 LNK2019:未解析的外部符号 _main 在函数 ___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:

我试过:

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

没有尝试过并怀疑这些也不起作用:

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

为什么我会收到此错误,解决方案是什么?

回答by Seva Alekseyev

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 _tmainis #defined to be either mainor wmaindepending on whether UNICODE is defined or not.

你的项目类型是什么?如果它是“Win32 项目”,则您的入口点应该是(w)WinMain. 如果它是“Win32 控制台项目”,那么它应该是(w)main. 名称_tmain是 #defined 为mainwmain取决于是否定义了 UNICODE。

If it's a DLL, then DllMain.

如果是 DLL,则DllMain.

The project type can be seen under project properties, Linker, System, Subsystem. It would say either "Console" or "Windows".

项目类型可以在项目属性、链接器、系统、子系统下看到。它会说“控制台”或“Windows”。

Note that the entry point name varies depending on whether UNICODE is defined or not. In VS2008, it's defined by default.

请注意,入口点名称取决于是否定义了 UNICODE。在 VS2008 中,它是默认定义的。

The proper prototype for main is either

main 的正确原型是

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

如果您在 _TCHAR 上遇到错误,请放置一个

#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...

如果您认为问题出在其中一个标题上,请使用 main() 转到文件的属性,然后在预处理器下启用预处理文件的生成。然后编译。您将获得一个与 .i 扩展名相同的文件。打开它,看看 main() 函数是否发生了什么令人不快的事情。理论上可能存在流氓#defines...

EDIT2:

编辑2:

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.

定义了 UNICODE(这是默认值)后,链接器期望入口点是 wmain(),而不是 main()。_tmain 具有与 UNICODE 无关的优势 - 它可以转换为 main 或 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).

前段时间,有理由同时维护 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 #defined 为 char 或 wchar_t。所以 _tmain 要么是 main(int argc, char **argv) 要么 wmain(int argc, wchar_t **argv)。

The reason you were getting an error at _tmainat some point was probably because you did not change the type of argvto _TCHAR**.

_tmain在某个时候收到错误的原因可能是因为您没有更改argvto的类型_TCHAR**

If you're not planning to ever support ANSI (probably not), you can reformulate your entry point as

如果您不打算支持 ANSI(可能不支持),您可以将您的入口点重新表述为

int wmain(int argc, wchar_t *argv[])

and remove the tchar.hinclude line.

并删除tchar.h包含行。

回答by Dizzyspiral

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 -> Generaland change the Configuration Typeto Dynamic Library (.dll).

为我的项目创建新配置后,DLL 出现此错误。我不得不去Project Properties -> Configuration Properties -> General并将其更改Configuration TypeDynamic 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 WinMaininstead of DllMain.

因此,如果您在尝试了所有其他方法后仍然遇到问题,那么值得检查一下配置类型是否符合您对项目的期望。如果设置不正确,编译器将寻找错误的主符号。就我而言,它正在寻找WinMain而不是DllMain.

回答by Mustafa Abukhadra

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

当我尝试关闭控制台应用程序项目中的预编译头文件并删除头文件 stdafx.h 时出现此错误

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

要解决此问题,请转到您的项目属性 -> Linker -> SubSystem 并将值更改为Not Set

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

在您的主类中,使用其他人已经提到的标准 C++ 主函数原型:

int main(int argc, char** argv)

回答by TomSmartBishop

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.

如果你有一个“Win32 项目”+ 定义了一个 WinMain 并且你的子系统链接器设置被设置为 WINDOWS 你仍然可以得到这个链接器错误,以防有人将链接器设置中的“附加选项”设置为“/SUBSYSTEM:CONSOLE”(看起来像这个附加设置比实际的子系统设置更受欢迎。

回答by Shania

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;

我发现当我选择 Project->Properties->Linker->System->SubSystem->Console(/subsystem:console) 选项时,然后确保包含函数:int _tmain(int argc,_TCHAR* argv[] ){return 0} 所有的编译、链接和运行都可以;

回答by Pagefault

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.

不小心将 wmain 放入命名空间时出现此错误。wmain 不应位于任何命名空间中。此外,我在使用的其中一个库中有一个 main 函数,而 VS 从那里获取了 main 函数,这让它变得更奇怪了。

回答by DarenW

I had this problem minutes ago. It went away when I added 'extern "C"' to the main() definition.

几分钟前我遇到了这个问题。当我将 'extern "C"' 添加到 main() 定义时,它就消失了。

Oddly, another simple program I wrote yesterday is almost identical, does not have the extern "C", yet compiled without this linker error.

奇怪的是,我昨天编写的另一个简单程序几乎完全相同,没有 extern "C",但编译时没有出现此链接器错误。

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.

这让我认为问题是在某些配置对话框深处发现的一些微妙设置,并且“extern“C”并没有真正解决潜在的问题,但表面上使事情起作用。

回答by forest.peterson

this mainworks 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)

main适用于 linux 和 windows - 通过反复试验和其他人的帮助找到它,所以无法解释它为什么起作用,它只是int main(int argc, char** argv)

no tchar.hnecessary

没有tchar.h必要

and here is the same answer in Wikipedia Main function

这是维基百科主功能中的相同答案

回答by Hendy Irawan

In my case, it's because I accidentally removed (not deleted) the stdafx.hand targetver.hfiles in the Header Filessection.

就我而言,这是因为我不小心删除(未删除)头文件部分中的stdafx.htargetver.h文件。

Add these files back to Header Filesand 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.

我只需要评论(通过预先添加//),这很好。

回答by Sebastian K

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

由于一个有趣的原因,我在 Visual Studio 2015 中也发生了这种情况。只需在此处添加它,以防它发生在其他人身上。

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.

我在项目中已经有很多文件,我正在添加另一个具有主要功能的文件,但是当我最初添加文件时,我在扩展名中打错了字(.coo 而不是 .cpp)。我更正了,但是当我完成时,我收到了这个错误。事实证明,Visual Studio 很聪明,当添加文件时,由于初始扩展名,它决定它不是源文件。

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

右键单击解决方案资源管理器中的文件并选择属性 -> 常规 -> ItemType 并将其设置为“C/C++ 编译器”修复了该问题。