Why it doesn't compile? Linux g++ problem? or book error?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.