GNU, GCC, G++, GDB — What are these terms? Explained!!

Behind The Scenes (BTS) execution of a computer program.

A.G. Yogi
4 min readAug 24, 2019

Prerequisite— Basic overview of computer programming. Check my previous post to grasp these concepts more quickly.

source: internet

In my previous post, I explained what is the relevance of programming in this technical era, which programming language to choose if you are a beginner and, to identify the most suitable language based on the context you will be going to use it.

Most of us have seen how a simple program looks on a computer, it is just a formatted text written in a human-readable format or High-level language, we can say. The first thing that comes in our mind when we start writing a program is the compiler translates the human-readable code into a language that the computer understands (i.e. machine code or byte code). This is absolutely true but, in a limited scope of thinking. There are many phases through which our program gets translated and ends up giving the machine code and finally the linking phase which links the various modules of the program into a single object file to create an executable file.

The flow of execution of a program in C.

There are 4 phases for a Program to finally get translated into an executable file viz.

  1. Preprocessing: It expands the code (for example — expansion of macros, removal of comments and conditional compilation).
  2. Compilation: Translates the code into assembly language, which an assembler can understand.
  3. Assembly: Assembler translates the code into machine code or byte code.
  4. Linking: It links various modules of the code, i.e. the function calls and finally delivers the executable.

Below is the Linux command mentioned with which you can see all the aforementioned intermediate files.

$gcc –Wall –save-temps filename.c –o filename

Compiler vs. Interpreter

Compiler: Scans the entire program and translates it as a whole into machine code. It continues translating the program until the first error is met, in which case it stops. Hence debugging is easy. For example Java.

Interpreter: Translates program one statement at a time and does not generate Object Code. It gives error only after scanning the entire program. For example Python.

GNU, GCC, and G++

GNU, recursive acronym for “GNU’s Not Unix!”, is a Unix-like operating system and free software under General Public License (GPL). It is intended to develop and share software for free, for all its users.

GCC (GNU Compiler Collection) is a compiler system produced by the GNU Project under General Public License, supports various programming languages.

Earlier it was named GNU C Compiler as it only supported C programming language. GCC can compile both C and C++ files (for-example .c/.cpp files). It provides various options, for example:

  • -o Option (to specify the output name for the executable)
  • -wall Option (to enable warnings)
  • -e Option (to produce preprocessor output)
  • -S Option (file would contain the assembly output)
  • -c Option (file would contain machine code)
gcc sourcecode.c -o executable

G++ (GNU C++ Compiler) is a GNU C++ compiler invocation command, which is used for preprocessing, compilation, assembly and linking of source code to generate an executable file. It provides a similar set of options as defined for GCC to get output in various intermediate stages of code compilation. For example:

g++ -o main.exe hello.cpp

GDB (GNU Debugger)

GNU Debugger (GDB) is a portable debugging tool for C and many other programming languages and runs on Unix-like operating systems. This tool comes in handy when our C program crashes (eg. core dump segmentation fault) and to know what exactly wrong has happened inside the code. GNU Debugger executes on the binary files/executables. It provides a GDB prompt which opens after typing gdb in your Linux terminal, where you can operate it using the commands it provides.

A segmentation fault occurs when we are trying to access a read-only memory or trying to access the address via a pointer that is already freed. The UNIX signal associated with segmentation fault is SIGSEGV (signal number 11).

Some commands that GDB provides:

  • r (run) — to execute the program from start till the very end.
  • b (break) — provides breakpoint on a particular line to debug the code.
  • disable — to disable a breakpoint.
  • enable — to enable a disabled breakpoint.
  • n (next) — go to next line of code, not into functions.
  • step — go to next instruction, diving into functions.
  • l (list) — displays the code.
  • p (print) — to display the stored value.
  • q (quit) — to exit the GDB prompt.
  • clear — clears all breakpoints.
  • continue — continues normal execution of code.

This article is a useful guide to those who want to learning programming, while diving deep into the compilation concepts and for those developers who are going to start their profession as hardcore programmers. These are some concepts which often confuse the programmers as all of them seem similar in their acronyms.

Thanks for reading the post until the very end. Please share this if you enjoyed reading it.

References:

https://www.programiz.com

www.geeksforgeeks.org

https://en.wikipedia.org

--

--

A.G. Yogi

This profile is all about life of a Software Developer.