【问题标题】:MinGw compiler errror - collect2.exe: error: ld returned 1 exit statusMinGw 编译器错误 - collect2.exe:错误:ld 返回 1 退出状态
【发布时间】:2020-12-12 07:48:50
【问题描述】:

我正在尝试在 Sublime text 中编译 c++。我已经设法在我的计算机上安装了 MinGw 的相关软件包。除了在C:\MinGW\bin目录下的系统环境变量中创建路径文件,它的确切位置。随后在编辑器中生成了一个新的构建系统,包含以下内容:

{
"cmd" : "gcc $file_name -o ${file_base_name} && ${file_base_name}",
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path"
}

如果我的编译器安装正确完成,我进一步检查了 cmd,确实如此。

>gcc --version
gcc (MinGW.org GCC Build-2) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

我在 sublime 中输入了这段代码:

#include<iostream>
using namespace std;
int main(){
    cout<<"hello world";
}

编译结果是这样的:

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\44750\AppData\Local\Temp\ccVYP6T2.o:item.cpp:(.text+0x19): undefined reference to `std::cout'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\44750\AppData\Local\Temp\ccVYP6T2.o:item.cpp:(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\44750\AppData\Local\Temp\ccVYP6T2.o:item.cpp:(.text+0x35): undefined reference to `std::ios_base::Init::~Init()'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\44750\AppData\Local\Temp\ccVYP6T2.o:item.cpp:(.text+0x56): undefined reference to `std::ios_base::Init::Init()'
collect2.exe: error: ld returned 1 exit status
[Finished in 0.8s]

【问题讨论】:

  • 用 g++ 构建 C++,而不是 gcc。

标签: c++ gcc mingw


【解决方案1】:

您将gcc 设置为编译器,但您正在构建

使用gcc 构建可能允许它编译 C++,但它不会自动链接到所需的标准库——这会导致您遇到链接失败错误。

要么将 cmd 更改为使用 g++,要么添加标志“-x c++”,以便它知道这是 C++ 代码,这应该允许它拉入正确的库。

【讨论】:

  • 哎呀,错过了 - 感谢您指出。已更新。
  • 刚刚尝试使用 c 并保持与前面提到的相同的方面,但仍然得到相同的错误:` #include int main() { // printf() 显示里面的字符串引用 printf("Hello, World!");返回0; } ` 得到了这个The system cannot execute the specified program. [Finished in 3.4s with exit code 1] [cmd: gcc saacs.c -o saacs &amp;&amp; saacs]
  • 我可以把编译器一起删除并安装一个新的
  • 您可以从winlibs.com 获得无需安装的独立版本。解压就行了。这样你就可以在不删除旧编译器的情况下试用它(只要确保不混合路径)。
猜你喜欢
  • 2014-09-03
  • 2015-02-19
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多