LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using sample C++ code from a book...won't compile (https://www.linuxquestions.org/questions/programming-9/using-sample-c-code-from-a-book-wont-compile-298012/)

Baix 03-05-2005 12:51 PM

Using sample C++ code from a book...won't compile
 
Hey guys,
Just got C++ How to Program 3rd Edition by Deitel & Deitel and I'm trying to compile the following using gcc:

Code:

// Fig. 1.2: fig01_02.cpp
// A first program in C++
#include <iostream>

int main()
{
  std::cout << "Welcome to C++!\n";

  return 0;      // indicate that program ended successfully
}

However I get this error when trying to compile:

Code:

me@tux C++ $ gcc Fig01_02.cpp
/tmp/ccMzI0Dh.o(.text+0x1b): In function `main':
: undefined reference to `std::cout'
/tmp/ccMzI0Dh.o(.text+0x20): In function `main':
: 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/ccMzI0Dh.o(.text+0x49): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccMzI0Dh.o(.text+0x7a): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccMzI0Dh.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'collect2: ld returned 1 exit status


wapcaplet 03-05-2005 01:02 PM

Try compiling using g++ instead of gcc.

michaelk 03-05-2005 01:04 PM

I assume you compiling using g++ instead of gcc?
Code:

// Fig. 1.2: fig01_02.cpp
// A first program in C++
#include <iostream>
add-> using namespace std; 

int main()
{
  cout << "Welcome to C++!"<<endl;

  return 0;      // indicate that program ended successfully
}

Wondering when this book was published since the ANSI standard was redesigned. (I can't tell you when). You can search the web for c++ tutorials on the subject.

Baix 03-05-2005 01:26 PM

Cool g++ worked...I assume this book is out of date? looks like its from 2001

Hivemind 03-05-2005 02:04 PM

The code is not invalid according to the C++ standard and the change suggested by michaelk is not necessary at all,
it only tells us michaelk doesn't know much about namespaces.
You should be fine with that book. Just remember to compile c++ programs with g++ and c programs with gcc.


All times are GMT -5. The time now is 11:12 PM.