Detailed explanation of gcc command usage under Linux system

Detailed explanation of gcc command usage under Linux system

1. Installation of gcc (taking Ubuntu as an example) sudo apt-get install build-essential

After installation, you can check the version through gcc --version.

Let's master its basic application in the process of compiling executable files through gcc.

Preparation: First, use vim to create a .c file in the current directory, for example 666.c

 $ vim 666.c

(The use of vim can be described in detail in another blog of mine, so I won’t go into details here)

Insert a code.

 #include <stdio.h>
main()
{
	int a,i;
	a=6;
	for(i=0;i<3;i++)
	{
		printf("%d",a);
	}
} 

(Every time we use cat to check)

1. Preprocessing

1. Preprocessing expands macro definitions (like #define), expands header files (like stdio.h), compiles conditions (like ifdef), expands all macros, and deletes all comments (like "//"). Preprocessing cpp precompiles source code and header files into a .i file. (Note that syntax is not checked at this time, so no error will be reported even if there are syntax errors.)

2. Command:

 $ gcc -E (source file name) -o (preprocessor file name)

or:

 $ gcc (source file name) > (preprocessor file name)

For example, according to 666.c, the preprocessing can be:

gcc -E 666.c -o 666.i

It can also be gcc 666.c > 666.i

2. Compilation

1. Compilation means checking whether the syntax is correct and compiling the preprocessed file into an assembly file.

2. Command:

$ gcc -S (source file) -o (assembly file)

For example, gcc -S 666.i -o 666.s

3. Compilation

1. Assembly means generating target files (binary files) from assembly files. Through assembly, text codes are converted into binary codes. (Binary code files have the suffix .o).

2. Command:

 $ gcc -c (assembly file) -o (object file)

For example, gcc -c 666.s -o 666.o

(Because it is a binary file, it will be garbled)

4. Links

1. Link to find dependent library files (static and dynamic) and link the target files into an executable program.

 $ gcc -c [target file] -o [executable program] -l [dynamic library name]

If there is no dynamic library (usually)

direct

 $ gcc -c [target file] -o [executable program] 

For example, gcc -c 666.o -o 666

At this time, an executable program 666 will be generated

If you want to execute this command:

 $ ./executable program

The ./ means in the current directory.

For example./666

(Note: In general, if we use the gcc executable file name, it will generate an a.out executable file by default. In this case, we can use ./a.out to execute it directly.)

This is the end of this article about the detailed usage of gcc command under Linux system. For more relevant Linux gcc command content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Specific use of Linux gcc command
  • How to install Linux online software gcc online
  • Installation process of GCC compiler on Linux

<<:  Detailed explanation of how to effectively store IP addresses in MySQL and how to convert between string IP and numerical values

>>:  CSS3 mouse hover transition zoom effect

Recommend

About the configuration problem of MyBatis connecting to MySql8.0 version

When learning mybatis, I encountered an error, th...

Win10 install Linux ubuntu-18.04 dual system (installation guide)

I installed a Linux Ubuntu system on my computer....

MySQL 5.7.17 installation and use graphic tutorial

MySQL is a relational database management system ...

A bug fix for Tomcat's automatic shutdown

Preface Recently, a Java EE web project that has ...

Detailed explanation of how to enter and exit the Docker container

1 Start the Docker service First you need to know...

Analysis of the principle of Mybatis mapper dynamic proxy

Preface Before we start explaining the principle ...

Nginx solves cross-domain issues and embeds third-party pages

Table of contents Preface difficulty Cross-domain...

Teach you how to install mysql database on Mac

Download MySQL for Mac: https://downloads.mysql.c...

Nginx learning how to build a file hotlink protection service example

Preface Everyone knows that many sites now charge...

JavaScript to achieve the effect of clicking on the submenu

This article shares the specific code of JavaScri...

CSS animation property usage and example code (transition/transform/animation)

During development, a good user interface will al...

What command is better for fuzzy searching files in Linux?

1. Introduction This article mainly explains how ...

Problems and solutions for MYSQL5.7.17 connection failure under MAC

The problem that MYSQL5.7.17 cannot connect under...