How To Compile C and C++ Applications with GCC? – POFTUT

How To Compile C and C++ Applications with GCC?


GCC is de facto compiler UNIX and LINUX Operating Systems. GCC is the acronym of the GNU Compiler Collection. As the name Collection suggest GCC supports C, C++, Java, Ada, Go, etc. In this post, we will look at how to compile C and C++ applications.

Installing

By default compiler, related tools are not installed. We can install them from repositories easily like below.

Ubuntu, Debian, Mint, Kali:

$ sudo apt-get install gcc -y
Ubuntu, Debian, Mint, Kali Installation
Ubuntu, Debian, Mint, Kali Installation

As we see GCC is already installed.

CentOS, Fedora, Red Hat:

$ yum install gcc -y
CentOS, Fedora, Red Hat Installation
CentOS, Fedora, Red Hat Installation

What Is Compiling A Source Code or Application

Compiling is the process of creating executable files from source code. There are some intermediate states but we do not dive into them. For example to print some messages to the standard output a program is written which is consists of source codes. Then the program is compiled with GCC to create a new executable file that can be run in the Linux. Here is our source code:

#include <stdio.h> 
 
void main(){ 
 
  printf("Hi poftut"); 
 
}

Compile C Source Code with GCC

We have the following source code to compile which just prints some text to the standard output.

$ gcc main.c
Compile
Compile

We have compiled our source code and a binary named a.out is created. Then we executed the new binary and it prints out the message “Hello poftut” .

Set Output Filename

By default after compile operation, the created executable file name will be a.out  as we have seen in the previous example. We can specify the executable file name after compilation like below.

$ gcc main.c -o mybinary
Set Output File Name
Set Output Filename

Debug Executable with GCC

If our program has some problems with performance or errors we need to debug it. To debug an application it must be compiled with debug options to add some debug data into the binary files. Keep in mind debug information will be made binary slower and bigger in size.

$ gcc -g main.c -o mybinary
Debug
Debug Executable with GCC

Optimize Code with GCC

In the previous example, we have seen that debug information made the executable slower and higher in size. For the production environment, we need to make the executable more optimized. We can make code more optimized in performance and size with -O parameters. But keep in mind that in rare situations optimization can make things worst.

$ gcc -O main.c -o mybinary

Include Libraries During Compilation with GCC

We have looked at simple source code but in a real-world project, there are a lot of code files and external libraries. We should specify the library we have used in the related code file.  We can provide external libraries with -l parameters.

$ gcc -O main.c -lm -o mybinary

Here -lm will provide the C standard math library to be used in this application.

LEARN MORE  How To Install and Use Cygwin With Terminal and SSH Examples?

Check Code Quality with GCC

GCC has a good feature which will give suggestions about the code quality. This option will check written code in a more strict manner. But the code should be correct syntactically and compiled correctly. We will use -Wall option to use this feature.

$ gcc -Wall main.c
Check Code Quality
Check Code Quality with GCC

Show GCC Version

Version is an important aspect of compile operation. Because GCC gains, dismiss separate features in each version, and modifying related configuration is important. The version of GCC can get with -v option. This will not provide only the version also give information on the configuration of GCC.

$ gcc -v
Version
Show GCC Version

Leave a Comment