gcc Command in Linux



gcc stands for GNU Compiler Collection is a command used in Linux to compile programs written in C and C++ programming language. To compile a program, you simply type the gcc followed by the name of your default source file, like gcc program.c. Once the command is executed, it will create an executable file usually named a.out by default.

The gcc command also supports several options for linking libraries, debugging and setting standards. It is a powerful tool that you can use to turn your code into runnable programs on your Linux system.

Table of Contents

Here is a comprehensive guide to the options available with the gcc command in linux −

How to Instal gcc Command in Linux?

By default, the gcc compiler is not installed on your Linux system, but you can manually install it using your default Linux package manager.

On Linux systems like Ubuntu, Debian, Raspberry Pi or Kali Linux, you can install gcc using the following command −

sudo apt install gcc

On other systems like Fedora and CentOS, the gcc can be installed through −

sudo yum install gcc

Or,

sudo dnf install gcc

The Arch Linux users can install gcc on their systems with the following command −

sudo pacman -S gcc

For OpenSUSE system, you can use below-given command to install gcc

sudo zypper install gcc

Syntax of gcc Command

The basic syntax to use the gcc command on Linux is given below −

gcc [options] [source file]

Here,

  • options are flags that modify the behavior of the GCC command.
  • source file is the source code file to be compiled.

gcc Command Options

With gcc command, there are multiple options you can use, a few of them are discussed in the table provided below −

Option Description
-B <directory> Specifies the directory to search for compiler executables and libraries.
-c Compiles source files without linking.
-E Preprocesses the source files only, stopping before the compilation step.
-g Includes debugging information in the output.
--help Shows a help message listing all available options.
-o <file> Defines the name of the output file.
-pie Generates a position-independent executable.
-pipe Uses pipes rather than temporary files for communication between stages of compilation.
-S Compiles only; dont link or assemble.
-shared Creates a shared library.
-time Reports the time taken for each stage of compilation.
-Wa, <options> Provides comma-separated options to the assembler.
-Wp, <options> Provides comma separated options to the preprocessor.
-Wl, <options> Provides comma separated options to the linker.
-v Display the program invoked by the compiler.
--version Displays the version information.
-x <language> Specifies the language of the input files

To explore more information, simply open the manual on the terminal using −

man gcc

Examples of gcc Command in Linux

Lets discuss a few examples of gcc commands in Linux systems. This will help you in learning how to get started with the command.

  • Basic Usage
  • Specify Output Filename
  • Generate Object File
  • Show All Warnings
  • Generate Assembly Code

Basic Usage

The basic use of gcc on Linux is to compile a single C source file. It is possible by simply using the gcc command along with the source file name you want to compile. Heres an example −

gcc main.c

The above command will compile main.c that include the C code and generate an executable file at the current location named a.out by default. Make sure to replace main.c with your desired C source file.

Basic Usage of gcc Command

Once the compilation is done, you can run the default output file to get the result −

./a.out
Run The Default Output File

Specify Output Filename

You can also specify the name of the output file by using the gcc command along with the -o option and the file name. For example −

gcc main.c -o program

After the execution of the above command, the main.c will be compiled. After the compilation, an executable named program is created.

Specify Output Filename

Generate Object File

The gcc command along with -c option can be used to compile a source file into an object file without linking. This process will benefit you in case you want to compile multiple source files separately and link them later. For example −

gcc -c main.c

The above command will compile main.c into an object file named main.o.

Generate Object File

Show All Warnings

If you want to enable all commonly used warning messages during compilation, you can simply use the -Wall option with the gcc compiler command. For example −

gcc -Wall main.c

The above command will compile main.c and show all warnings if any occur during the compilation.

Show All Warnings Using gcc

Generate Assembly Code

You can also stop the code at the compilation stage and output the assembly code by using gcc with -S option. For example −

gcc -S main.c

The above command will compile main.c and generate an assembly file named main.s.

Generate Assembly Code

In this way, you can use gcc on Linux for compiling your C source files.

Conclusion

The gcc command is a powerful and versatile tool used for compiling C and C++ source files directly from the terminal. It provides a number of options that can change the way of the compilation process.

In this guide, we have explored the syntax of gcc command with a few different options that you can use with it. Apart from that, different examples are provided to help you in getting a good understanding about its usage on different Linux systems.

Advertisements