TeachingBee

How to Compile a C Program in Ubuntu

compile C program in Ubuntu-teachingbee

Ubuntu, a widely-used distribution of the Linux operating system, provides a conducive environment for software development. For budding developers or those new to the Linux ecosystem, compiling a C program might seem daunting. This article aims to demystify the process, offering a step-by-step guide to compiling and running C programs in Ubuntu.

Detailed Steps to Compile a C Program in Ubuntu

Steps to compile a C program In Ubuntu are:

Writing Your First C Program

  • Let’s begin by writing simple C program in a text editor of your choice.
  • Save the file with a .c extension to desired location, e.g., hello.c.
// Simple Hello World Program In C

#include <stdio.h>  // Include the standard input/output library.

int main() {
    // This is the main function, where program execution starts.

    // Print a message to the console.
    printf("Hello, World!\n"); // The '\n' represents a newline character.

    // Return 0 to indicate successful execution to the operating system.
    return 0;
}

Checking if GCC is Installed

  • Before compiling, it’s crucial to ensure you have the GCC compiler.
  • In the terminal, type:
  gcc --version
  • If GCC is installed, this command will display its version.
  • If not, you’ll need to install it.
Screenshot 2023 10 25 at 10.44.19 AM

A C compiler is a vital tool in software development, responsible for translating human-readable C source code into machine-readable binary code. It not only detects syntax and semantic errors but also optimises code to enhance efficiency and performance, resulting in faster and more reliable software execution.562CD9

Installing the GCC Compiler (if not installed)

  • Use the command to install GCC using APT package manager:
  sudo apt install gcc
  • This installs the GNU Compiler Collection, which includes the C compiler.

Generating Object File

  • Compilation translates the human-readable C code into machine code. So for this, we will use the GCC compiler installed above.
  • The command compiles the hello.c file. The -o flag specifies the output file, which in this case is named hello. If no file name is specified for object file, by default object file is created by name a.out.
 gcc -o hello hello.c
  • This process generates an object file in the current folder, which is a binary file that can be executed.

Note: In the above command, you need to specify the path to locate a .c file if it is located in a different directory.

Storing the Object File in a Different Location

  • Alternatively, you can specify a path along with the name to determine where the compiled object file will be stored.
  • For example, if you want to store the object file in a directory named bin, you can use:
  gcc -o bin/hello hello.c
  • This will compile hello.c and store the resulting object file hello in the bin directory.
  • Ensure the directory exists before compiling, or you’ll receive an error. If it doesn’t exist, you can create it using the command mkdir bin.

Running the Compiled Program

  • Post compilation, you can run the program using:
 ./hello
  • The ./ prefix indicates that the executable is in the current directory.
compile a C program in Ubuntu

Running the Object File from a Different Location

  • If you’ve stored the object file in a different directory, you’ll need to navigate to that directory or specify its path when running the file.
  • For the above example, from the main directory, you can run the program using:
  ./bin/hello

Key Takeaways

  • GCC compiler needs to be installed to compile C programs on Ubuntu. The command gcc --version checks if it’s installed.
  • To compile a C program ‘hello.c’, use gcc -o hello hello.c. This generates an executable object file named ‘hello’.
  • The -o flag in the GCC command specifies the output file name for the compiled program.
  • To run the compiled program, use ./hello if the object file is in the current directory. Use a path like ./bin/hello if the object file is stored elsewhere.
  • Compilation converts human-readable C code to machine code (object file) that can then be executed on the OS.

Checkout more similar blogs

Download Code Blocks for macOS

Download Code Blocks for Windows 10&11

Download Turbo C Compiler on Windows

Checkout more C Tutorials here.

I hope You liked the post ?. For more such posts, ? subscribe to our newsletter. Try out our free resume checker service where our Industry Experts will help you by providing resume score based on the key criteria that recruiters and hiring managers are looking for.

FAQs

Why do we need the GCC compiler?

What happens if I don’t use the -o flag during compilation?

Why is the ./ prefix used before running the executable?

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

Subtraction of Two Numbers Program in C

In this blog post, we will checkout the Subtraction of Two Numbers Program in C. How to Write Subtraction Program in C? To create a C program to subtract two

Add Two Numbers In C

In this blog post, we will checkout the how to add two numbers in C. Algorithm to Add Two Numbers in C To create a C program to add two

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In