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

2.8k views Asked by At

I have just switched to elementary os and installed sublime text editor. i wrote a basic hello world program in c++ to check the working. And I get this error.

/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

The hello world program was,

#include <iostream>
using namespace std;

int main(){
     cout << "Hello World!" << endl;
     return 0;
}

I have searched for answer in google and stackoverflow but couldn't find a solution.

1

There are 1 answers

0
Schtolc On BEST ANSWER

You should compile it with C++ compiler, not C, g++ for example.

Instead of this

gcc source.cpp

do this

g++ source.cpp