Have you ever wondered what dependencies are required to compile a simple hello-world program? Even a small hello-world program needs a set of header files and libraries that are used by the compiler. The header file e.g., iostream is required to find the declaration of functions which are not available in the hello-world program e.g., std::cout. The libraries are required to find definitions of functions e.g., std::operator<< during the linkage process. As a result of the compilation process an executable is created that runs on the machine.
The compilation process
When a compiler like g++ is used to compile a C++ program, the compilation process actually involves multiple steps depending on what output is desired. To see the steps involved in the compilation process, -v needs to be passed to the compiler. Using a small hello world program:
#include<iostream>
int main() {
std::cout << "Hello world";
return 0;
}
g++ is not a compiler, it is a compiler-driver. This may sound strange to many but that is true. The compiler in this case is cc1plus, and the invocation is below:
$ g++ hello.cpp -v
/usr/lib/gcc/x86_64-linux-gnu/7/cc1plus -quiet -v -imultiarch x86_64-linux-gnu \
-D_GNU_SOURCE hello.cpp -quiet -dumpbase hello.cpp -mtune=generic -march=x86-64 \
-auxbase hello -version -fstack-protector-strong -Wformat -Wformat-security \
-o /tmp/ccWH0EQc.s
During the compilation cc1plus needs to find the header file iostream which is present in /usr/include/c++/7. Next up is the assembler invocation. It reads the output of the compiler (/tmp/ccWH0EQc.s) and outputs an object-file (/tmp/ccTpqU8Z.o). The assembler does not have any dependencies. Finally, the linker (collect2/ld) takes the object file, resolves the symbols against libstdc++ and libc, and produces the executable.
g++ is a tour conductor, not a performer. It hands your source to cc1plus (the actual compiler), passes the assembly to as (the assembler), and gives the object file to ld (the linker). Each performer does one job, and the conductor makes sure they all get on stage in the right order.
g++ is not a compiler, it is a compiler-driver. The compiler is cc1plus.