MinGw编译器错误-collect2.exe:错误:ld返回1退出状态

时间:2020-08-23 14:20:20

标签: c++ gcc mingw

我试图用 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.

我输入了以下代码:

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

1 个答案:

答案 0 :(得分:1)

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

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

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

相关问题