Stroustrup - Programming: Principles and Practice Using C++ - std_lib_facilities.h Works With IDE but Not gcc

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> >&, char const*)'
/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


My code:
1
2
3
4
5
6
#include "std_lib_facilities.h"

int main() {
    cout << "Hello World!\n";
    return 0;
}
Last edited on
Hello killingthemonkey,

If I managed to find the right file it starts with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<iomanip>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<list>
#include <forward_list>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include <array>
#include <regex>
#include<random>
#include<stdexcept> 

Plus other code that you do not need right now. I think this is his attempt to cover every possible header file that might be needed.

You should forget about what is easy and learn what header files you need. This will serve you better.

For your little program just replace #include "std_lib_facilities.h" with #include <iostream> and see what happens.

Andy
Can you compiler this with both?
1
2
3
4
5
6
#include <iostream>

int main() {
    std::cout << "Hello World!\n";
    return 0;
}
Oh boy...

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.

Best,
max
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.
Last edited on
Thank you all for replying.

@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.
Last edited on
I assume he knows what's doing.

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.
Last edited on
@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.]
Last edited on
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.
Topic archived. No new replies allowed.