Compiling C and C++ Programs on Linux

Introduction

Linux is a powerful platform in software development, particularly popular for projects developed in low-level programming languages like C and C++. These two languages hold a significant place in the software world as they offer fast and efficient solutions at the system level. However, to run the code written in these languages on a computer, it needs to be compiled into a format the machine can understand. The compilation process is a critical step in software development and requires careful technical execution.

In this article, we will go through the step-by-step process of compiling C and C++ programs on Linux. Additionally, we will explain the basic principles of compilation and why it is essential. We will discuss how to use commonly known compilers such as the GNU Compiler Collection (GCC) and G++ and demonstrate how the process works through sample code.

Learning Objectives

By the end of this article, you will be able to:

  • Understand what compilation is and why it is done.
  • Compile a simple C program on Linux.
  • Compile a simple C++ program on Linux.

What is Compilation and Why is it Done?

Compilation is the process of converting the code written in a high-level programming language into machine language that the computer can execute directly. Code written in languages like C and C++ is not understandable by the computer in its original form. Therefore, a compiler is used to convert the code into machine language. The compiler also checks the code for correctness, identifying errors that may need to be resolved. This ensures that programs are safer and less error-prone.

In addition, compilation can enhance software performance by optimizing the code, making it run more efficiently. In large projects, a good compilation process plays a critical role in ensuring the software runs smoothly. Furthermore, software can be compiled specifically for different operating systems, providing platform independence.

Compiling a Simple C Program (with Basic Code and Compilation Steps)

Let’s start by writing and compiling a simple “Hello, World!” program in the C language on Linux. For this, we will use GCC (GNU Compiler Collection), which is widely used to compile C programs.

Linux C and C++ compilation
C and C++ compiling guide for Linux
How to compile programs on Linux
GCC and G++ compilation steps
Compiling code on Linux
Simple C Code:
#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

This program is a simple C program that prints “Hello, World!” to the screen. Save the program as a file, for example, program.c.

Compiling the C Program:

To compile and run the C program, follow these steps:

  1. Open the terminal: The terminal is used for command-line operations and is the most suitable tool for compilation on Linux.
  2. Compile the C program using GCC: Use the following command to compile the program.c file
gcc program.c -o program

Here, gcc stands for GNU C Compiler. The -o parameter specifies the name of the output file. In this case, we name the compiled program program.

3. Run the executable file: After the compilation is successfully completed, you can run the program with the following command:

./program

Once you see the “Hello, World!” message in the terminal, your program has been successfully compiled and executed.

Compiling a Simple C++ Program (with Basic Code and Compilation Steps)

Now, we will perform the same steps with the C++ language. C++ is similar to C but offers more advanced features. For compilation, we will use G++, the C++ version of GCC,Beginner Projects in C++ Programming Language.

Simple C++ Code:
#include <iostream>

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

This is a simple C++ program that also prints “Hello, World!” to the screen. Save the file as program.cpp.

Compiling the C++ Program:

To compile and run the C++ program, follow these steps:

  1. Open the terminal: Launch the terminal and navigate to the directory where your program is saved.
  2. Compile the C++ program using G++: Use the following command to compile the file
g++ program.cpp -o program

This command compiles the program.cpp file using the G++ compiler and names the output file program.

3. Run the executable file: After the compilation, run the program with the following command:

./program

After completing these steps, you will see the “Hello, World!” output in the terminal.

Conclusion

In this article, we learned how to compile and run C and C++ programs on Linux. The compilation process is a crucial step in software development and can be done efficiently with the right tools. Thanks to powerful tools like GCC and G++, you can easily compile and run your C and C++ projects. This fundamental knowledge provides a strong foundation as you move on to more complex projects. With these simple steps, you can compile your programs and begin your journey into more advanced software development processes.

You May Be Interested In:
Amazon Product
Mastering Python for Ethical Hacking: A Comprehensive Guide to Building Hacking Tools

Mastering Python for Ethical Hacking: A Comprehensive Guide to Building Hacking Tools

Let’s embark on this journey together, where you will learn to use Python not just as a programming language, but as a powerful weapon in the fight against cyber threats

-5% $20 on buymeacoffee

6 thoughts on “Compiling C and C++ Programs on Linux”

    • In addition to C and C++, GCC supports other languages such as Fortran, Ada, and Objective-C. However, you may need to install the appropriate front-end compiler for each language.

      Reply
  1. For more complex projects, creating a Makefile can automate the process of compiling multiple files and provide a more organized structure.

    Reply
  2. Man, your step by step explanations and sample codes make it perfect for beginners. I have never had such a detailed understanding of the compilation process before.

    Reply

Leave a Reply