Introduction to GCC Command in Linux
In this article we will see an outline on GCC Command in Linux, GCC is abbreviated as GNU Complier Collection. GCC can compile C, C++, Ada and many more programming languages which are understandable by the system. As Linux is open source and free OS, it has become very popular among all the programmers. So to compile programming languages in Linux, GCC is used. GCC can help us to write and execute C language in Linux with a more advanced way.
To check the default version of gcc compiler in your system, you can use the command as –version in your Linux command prompt.
Basic GCC Syntax
gcc [options] [source_file] [object_files] [-o output_file]
Let us take a simple C program and execute in Linux with the help of Linux.
To Execute a C program, we need to follow three steps. They are:
- Write: C Program for which you want to compile in Linux environment.
- Compile: Program to check if any error exists or not.
- Run: Program to see the output in Linux environment.
The above steps are elaborated with examples and syntax below:
GCC Options in Linux Environment
Here are the few options given to use while compiling different programming languages in Linux. We have also explicitly used these options below to compile a C Program below.
|Gcc –c||Compiles source files to object files without linking to any other object files.|
|gcc –Idir||Includes the directories of header files|
|gcc –llib||link the code with the library files|
|gcc -o output file||Build the output generated to output file|
|gcc –w||Disables all warning messages during the compilation.|
|gcc –Wall||enables all warning messages during the compilation|
|gcc –Wextra||Enables extra warning messages during the compilation.|
Step 1: Write a C Program
Create a C program to print “Hello World” in Linux by following the below steps. Make sure you save the C program with .c as its extension. The below steps are to create a .c file and write the code in it. Save before you close the file.
- touch main.c
- vi main.c
- write the below code:
printf("\n Hello World \n");
- save the code in linux
Step 2: Compile the C Program
Now below are the options to compile a simple C program using GCC in Linux. You can use the options as per your requirement and build your program to get desired output.
1. The basic syntax to compile a C code is: To compile a C Code, use the below syntax. This syntax is used without any options.
When you compile the above code, you will get the output with the filename as a.out. The default output after compiling the C Program is resulted in”a.exe” or “a.out” format.
2. We can also specify explicitly mention the output file name by using –o as an option.
gcc main.c –o output
3. To see the warnings while we compile a C program: we need to use an option –wall while compiling the C Program as below:
printf("\n Hello World [%d]\n", i);
gcc –wall main.c –o output
Once we set –wall option, we can see the warnings that can occur in our code. Here our code will give uninitialized warning for the variable “i”.
4. To get preprocessed output with –E option: the output will be produced on stdout to redirect our result in other file. Here output.i would contain the preprocessed result.
gcc –E main.c > output.i
5. To get intermediate files using –save-temps: We can store all the intermediate files that are generated during the compilation in the same directory from where we do the compilation.
gcc –save-temps main.c
gcc –save-temps main.c
a.out main.c main.i main.o main.s
Here we can see the intermediate and executable files as well.
6. To see the error while compiling the C Program: To see the error during the compilation of C Program, we can use the option –W. This is one of the best practices to use to avoid errors.
gcc main.c –Werror –o output
7. To debug C Program in Linux: To debug C Program in Linux during compilation can be done by using –ggdb.
gcc –ggdb main.c –wall –o output
8. Verbose option is to see the complete description used in Linux during the compilation. The command –v is used as below:
gcc –v main.c –o output
Step 3: Run the C Program
The final step is to run the C program in Linux OS by using the below syntax:
In our example, we can run our program by using below syntax:
Output: Hello World
Here in this article, we came to know about how to Write a C Program in Linux, Compile the C Program and Run the c Program. GCC is very easy to use and has given us many options to simplify or run the C Program in Linux OS. Make sure all the packages are installed in Linux and then you can run C, C++, Ada and many more languages in Linux.
This has been a guide to GCC Command in Linux. Here we have discussed introduction to GCC Command in Linux, GCC option in Linux Environment with appropriate example. You may also have a look at the following articles to learn more –