-1

My last question on here had issues that apperently were gcc not the code(code errors were solved and then I noticed issues with gcc ). I have since whipped up a short hello world program that I know is sound to test this:

#include <iostream>
using namespace std;

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

but gcc still throws errors.

I named the file "test.cpp" and stored it directly in my home folder then tried:

gcc ./test.cpp -o Test

gcc ~/test.cpp -o Test

gcc /home/josh/test.cpp -o Test

All 3 times with and without capitalizing "Test".

Gcc gives the following errors:

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

My question is: How can I fix Gcc? If not should I try another compiler?

EDIT: I have tried reinstallint gcc and it still fails.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Josh
  • 23
  • 1
  • 7
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. Also spend a bit more efforts with researching before asking questions here please. – πάντα ῥεῖ Jun 17 '17 at 10:00
  • Compilation errors are printed, not thrown. – user207421 Jun 17 '17 at 10:52

2 Answers2

1

O.K. this was a duplicate (I can usually catch that before posting).

The answer was found here: undefined reference to 'std::cout'

I had to use

g++ ~/test.cpp -o Test

instead of

gcc ~/test.cpp -o Test 

NB: Actually, it is better to compile with all warnings and debug info, that is to add
-Wall -Wextra -g to your g++ or gcc command. Read more about Invoking GCC.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
Josh
  • 23
  • 1
  • 7
0

Please try to run g++ or add 'cpp flags' to the command, for example -lstdc++ which will link the code with the standard library of c++.

gcc can compile cpp code but only with the right flags.

Asafm
  • 177
  • 1
  • 13