LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2005, 12:51 PM   #1
Baix
Member
 
Registered: Jun 2004
Distribution: Gentoo, LFS, Slackware
Posts: 203

Rep: Reputation: 30
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
 
Old 03-05-2005, 01:02 PM   #2
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Try compiling using g++ instead of gcc.
 
Old 03-05-2005, 01:04 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,750

Rep: Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929
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.

Last edited by michaelk; 03-05-2005 at 01:05 PM.
 
Old 03-05-2005, 01:26 PM   #4
Baix
Member
 
Registered: Jun 2004
Distribution: Gentoo, LFS, Slackware
Posts: 203

Original Poster
Rep: Reputation: 30
Cool g++ worked...I assume this book is out of date? looks like its from 2001
 
Old 03-05-2005, 02:04 PM   #5
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
system() and execl anyone got any sample code please? twirl Programming 2 09-08-2005 02:01 AM
Trying to compile souce code that came with AUP book.... Scrag Programming 4 11-16-2004 09:24 PM
Perl: What's this mean? (Code sample) Benr Programming 5 10-23-2004 07:20 AM
Open Firmware code for booting OS from SATA : sample code available somewhere ? drsparikh Linux - Hardware 0 03-12-2004 11:16 AM
Any sample code of how to use Rand_seed() cybercop12us Programming 0 04-03-2003 07:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration