Collect2:错误:LD在C 中返回1个退出状态

collect2: error: ld returned 1 exit status in c++

本文关键字:返回 1个 状态 退出 错误 LD Collect2      更新时间:2023-10-16

我刚刚切换到基本操作系统并安装了Sublime Text Editor。我在C 中写了一个基本的Hello World程序来检查工作。我得到了这个错误。

/tmp/cc3Pmvft.o: In function `main':
source.cpp:(.text+0xa): undefined reference to `std::cout'
source.cpp:(.text+0xf): 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*)'
source.cpp:(.text+0x14): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
source.cpp:(.text+0x1c): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/cc3Pmvft.o: In function `__static_initialization_and_destruction_0(int, int)':
source.cpp:(.text+0x4a): undefined reference to `std::ios_base::Init::Init()'
source.cpp:(.text+0x59): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

Hello World节目是

#include <iostream>
using namespace std;
int main(){
     cout << "Hello World!" << endl;
     return 0;
}

我在Google和Stackoverflow中搜索了答案,但找不到解决方案。

您应该用C 编译器将其编译,而不是C,G 。

而不是这个

gcc source.cpp

做这个

g++ source.cpp