The first programming language I took when I started my undergraduate study in computer science was C. The second language was C++, taken in the following semester.I really liked C/C++ back then, but I never got a chance to actually use C/C++ in any real-life project. After transfering to the University of Auckland in New Zealand, I had to pick up Java as almost all computer science courses taught in Java back in early 2000.

Recently at work, there is a legacy program written in C++ that needs to be updated for a speicial need. We have been out sourcing this task to an external contractor who’s been charging work dearly. So I think it’s a good excuse to revise C++ programming language. Fortunatley, my work has the Lynda.com subscription which is made available to all staff. I’m doing this course called “C++ Essential Training” by Bill Weinman.

In one of his lecture, Bill demonstrated why printf or puts statement is preferred over cout by checking the size of compile binary files. I could compile this simple “Hello world” program on my desktop running Fedora 29, but it failed to compile it statically.

// hello-cout.cpp
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

To compile the above code statically:

❯❯❯ c++ -static-libstdc++ hello-cout.cpp
/usr/bin/ld: cannot find -lstdc++
collect2: error: ld returned 1 exit status

Since I was able to compile the code dymically (by default), I’m pretty sure that the libstdc++ is installed. To verify that:

❯❯ rpm -q libstdc++ libstdc++-devel
libstdc++-8.2.1-6.fc29.x86_64
libstdc++-devel-8.2.1-6.fc29.x86_64

According to this, it turns out that I also need another package libstdc++-static.x86_64. So let’s install it and try to compile again:

❯❯❯ sudo dnf install libstdc++-static -y
❯❯❯ rpm -q libstdc++ libstdc++-devel libstdc++-static
libstdc++-8.2.1-6.fc29.x86_64
libstdc++-devel-8.2.1-6.fc29.x86_64
libstdc++-static-8.2.1-6.fc29.x86_64

And it worked. Here are the two other versions of “Hello, World!” program using printf and puts:

#include <cstdio>
using namespace std;

int main() {
    printf("Hello, World!");
    return 0;
}
#include <cstdio>
using namespace std;

int main() {
    puts("Hello, World");
    return 0;
}

Here is the size of binary files. The file size of hello-cout is considerably bigger than the other 2 files.

❯❯❯ ls -lh hello-*
-rwxrwxr-x. 1 kenno kenno 1.2M Dec 24 19:08 hello-cout
-rwxrwxr-x. 1 kenno kenno  18K Dec 24 19:10 hello-printf
-rwxrwxr-x. 1 kenno kenno  18K Dec 24 19:11 hello-puts

Ref: