1st C++ Program From Tutorial Won't Compile With GCC

I've just tried to compile the first program from the tutorial referenced on this site, which is this:

1
2
3
4
5
6
7
8
9
10
// my first program in C++

#include <iostream>
using namespace std;

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

But this is what I get when I try to compile it with GCC, even after explicitly specifying it as a C++ program:

jwesleycooper@linux-twom:~/C> gcc -x c++ cpp1.cpp 
/tmp/cc9hRvBj.o: In function `main':
cpp1.cpp:(.text+0x14): undefined reference to `std::cout'
cpp1.cpp:(.text+0x19): 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*)'
/tmp/cc9hRvBj.o: In function `__static_initialization_and_destruction_0(int, int)':
cpp1.cpp:(.text+0x41): undefined reference to `std::ios_base::Init::Init()'
cpp1.cpp:(.text+0x46): undefined reference to `std::ios_base::Init::~Init()'
collect2: ld returned 1 exit status


Can someone please explain to me what is going wrong, why, and exactly what I must do to correct it?
Just use:
g++ cpp1.cpp
It'll know it's C++ from the extension and it'll also link with the standard library and console library for you. With gcc, you'd have to explicitly link them.
Last edited on
Thanks for telling me that... so will that do the same with C programs as well?
Topic archived. No new replies allowed.