I seem to be the perennial newbie here, but I have set myself the goal of working through this book. Using CodeLite, if I place the header file created by Stroustrup in the same directory as the .cpp file, it compiles with no issues. If I create a .cpp file and place the header file in the same directory, and try to compile with gcc, gcc HelloWorld.cpp -std=c++11 -o Hello, I receive the following:
1 2 3 4 5 6 7
usr/bin/ld: /tmp/ccYbIQlc.o: in function `main':
HelloWorld.cpp:(.text+0x12): undefined reference to `std::cout'
/usr/bin/ld: HelloWorld.cpp:(.text+0x17): 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> >&, charconst*)'
/usr/bin/ld: /tmp/ccYbIQlc.o: in function `__static_initialization_and_destruction_0(int, int)':
HelloWorld.cpp:(.text+0x4b): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: HelloWorld.cpp:(.text+0x60): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
This looks like another one of those headers that tries to include everything from soup to nuts in one #include preprocessor. Like <bits/stdc++.h>.
My issue with these is that they are non-standard, platform-dependent headers that, if someone else tries to use your code, will cause compiler errors because they don't have that specific header.
Go with what @Handy Andy suggested, figure out which standard C++ header files you need and only #include those headers.
The C++ compiler executable is named g++, not gcc.*
Therefore the command you need to try is g++ HelloWorld.cpp -std=c++11 -o Hello
Preferably you would ask the compiler to help catch your mistakes, by enabling warning messages: g++ HelloWorld.cpp -std=c++11 -Wall -Wextra -pedantic-errors -o Hello
*Actually gcc is quite capable of interpreting C++ code, but it does not link the C++ standard library by default. This is why the linker is complaining.
@mbozzi - You nailed it dead on the nose. Wrong compiler.
*** I missed your postscript. That little bit of explanation explains a lot.
@agent max - Stroustrup wrote the language. I assume he knows what's doing. He also says to only use the header until chapter 10 and that after chapter 21 I'll understand it.
Including everything in the standard library does eliminate a whole bunch of potential portability issues that newer programmers are especially likely to encounter.
This practice doesn't do any real harm, unless you care about how long your code takes to compile.
@mbozzi - These early programs are really short and compile time is negligible. It does make for a for a larger than necessary executable, but if you read the comments at the top of the file, he states that we'll stop using it after ten chapters.
Apologies in advance for derailing the thread further...
While I agree it does no harm in this case (as long as the student understands it's just for brevity), it must be said that just because somebody is very smart (like I presume the creator of the C++ language is), that doesn't mean they are a good teacher. [I am saying in general; I have not studied Stroustrup's teaching methods.]
I'm not saying the header is bad, per se; I'm saying to use it with caution. If you're giving your code out to someone else, get rid of it because if they don't have that header, it will cause a compiler error.
By all means, use it for messing around, but just be aware of its limitations.