Here is the collection of my notes on general C++ projects development. Hope it will help with your understanding on this programming language.

Difference between gcc and g++

Generally speaking, they are all compilers from GNU, but it is worth noting that:

  1. Both gcc and g++ can compile C and C++ code. However, gcc will recognize .c file as a C program while g++ will recognize it as a C++ program. For .cpp file, both of them will consider it as a C++ program.
  2. During the compiling process, g++ will calls gcc automatically. So they are equivalent for C++ program. However, we can’t use gcc commands to link C++ libraries, so we use g++ for linking libraries in general.
  3. You could use both g++/gcc for compiling and g++/gcc -lstdc++ for linking. For example:
1
gcc main.cpp -lstdc++

Compiling in C— Under the Hood

one command version: gcc hello.c

This simple command actually performs:

  1. PreProcessing
  2. Compilation
  3. Assembly
  4. Linking

Here the default output file is a.out

PreProcessing

In this stage, the compiler will add the header files into the source code.

  • Params: -E
  • Usage: gcc -E hello.c -o hello.i
  • Effect: preprocess hello.c and the output is hello.i

Compilation

The second stage is compiling where the gcc will check the syntax of the source code and translate it into assembly language.

  • Params: -S
  • Usage: gcc -S hello.i -o hello.s
  • Effect: Compile the hello.i into hello.s

Assembling

In the assembling stage, the purpose is to convert the .s file into binary file .o .

  • Params: -c
  • Usage: