LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 07-11-2006, 09:15 AM   #1
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Rep: Reputation: 30
Why it doesn't compile? Linux g++ problem? or book error?


Hey there.
I've done this simple code
Code:
#include <iostream>
using namespace std;


extern ostream* file;
int main() {
        *file << "hello world!" << endl;
        return 0;
}
why it doesn't compile? it gives me an error which says
Code:
[zuki@localhost C++]$ g++ zuki.cpp -o test
/home/zuki/tmp/cczkzetY.o: In function `main':
zuki.cpp:(.text+0x1d): undefined reference to `file'
collect2: ld returned 1 exit status
But I've copied the exact code from the book, so maybe it's a LINUX problem?

Thanks
 
Old 07-11-2006, 09:36 AM   #2
cdhgee
Member
 
Registered: Oct 2003
Location: St Paul, MN
Distribution: Fedora 8, Fedora 9
Posts: 513

Rep: Reputation: 30
This isn't a compilation problem, it's a linkage problem. You've declared file as an extern ostream*; the extern specifies that the actual declaration is defined elsewhere (probably in a library). The linker can't find the other reference to which the extern refers, hence the error.

You need to find out which library is required to compile this code snippet.

What's it supposed to do? There may be an easier way to do it.
 
Old 07-11-2006, 09:38 AM   #3
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Original Poster
Rep: Reputation: 30
instead of writing it to the screen, I'd like to write that line to a file...
 
Old 07-11-2006, 09:41 AM   #4
cdhgee
Member
 
Registered: Oct 2003
Location: St Paul, MN
Distribution: Fedora 8, Fedora 9
Posts: 513

Rep: Reputation: 30
Try something like this:

Code:
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Hello, world" << endl;
  myfile.close();
  return 0;
}
 
  


Reply

Tags
c++, file, files, g++, linker


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using sample C++ code from a book...won't compile Baix Programming 4 03-05-2005 02:04 PM
Trying to compile souce code that came with AUP book.... Scrag Programming 4 11-16-2004 09:24 PM
book/ reference to learn how to compile, install softwares? sirpelidor Linux - Software 2 10-29-2003 06:29 AM
Linux floppy book error HELLP!!!!! :( jollyjoice Linux - Newbie 4 08-31-2003 02:31 PM
Compile dmfe.c for installing net device driver on Book PC gogo Linux - Software 2 07-24-2003 12:51 PM


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

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration