简体   繁体   English

MinGW G++ 未编译

[英]MinGW G++ not compiling

I'm trying to compile a simple "hello world" C++ program but when I use the g++ command I get the error "undefined reference to `WinMain@16" this happens when I compile in vscode or in terminal, whats happening?我正在尝试编译一个简单的“hello world”C++ 程序,但是当我使用 g++ 命令时,我收到错误“未定义对 WinMain@16 的引用”,这发生在我在 vscode 或终端中编译时,发生了什么? code:代码:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
vector<string> msg {"Hello World"};

for (const string& word : msg)
{
    cout << word << " ";
}
cout << endl;
}

Your error is not a compiler, but a linker error.您的错误不是编译器,而是链接器错误。 That means it still compiles fine but fails to link.这意味着它仍然可以正常编译但无法链接。

A header file gets included (if it can be found in the include path the compiler uses to find headers), so this works fine.头文件被包含(如果它可以在编译器用来查找头的包含路径中找到),所以这可以正常工作。

But for linking two (object-)files together it has to know which files should be linked.但是要将两个(目标)文件链接在一起,它必须知道应该链接哪些文件。 And how should it decide which files to use?它应该如何决定使用哪些文件?

That's the cause to create projects in it (and other IDE's) or to create makefiles.这就是在其中创建项目(和其他 IDE)或创建 makefile 的原因。

If you turn on full command line-logging in "Settings -> Compiler and debugger... -> Global compiler settings -> [the_compiler_you_use] -> Other settings (rightmost tab) -> Compiler loggin" you can see which commands are sent to the compiler/linker and where the difference is between a single file and a project with multiple files.如果您在“设置 -> 编译器和调试器... -> 全局编译器设置 -> [the_compiler_you_use] -> 其他设置(最右边的选项卡)-> 编译器登录”中打开完整的命令行日志记录,您可以看到发送了哪些命令到编译器/链接器以及单个文件和具有多个文件的项目之间的区别。

By the way: your book/tutorial should explain the steps needed to compile and link files, what happens if headers are included and how to use prebuild libraries/dlls.顺便说一句:你的书/教程应该解释编译和链接文件所需的步骤,如果包含头文件会发生什么以及如何使用预构建库/dll。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM