Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use gcc command under Linux system

Shulou Source: shulou.com Published: 2022-06-02 04:29:15 09月29日 Update

This article mainly explains "how to use the gcc command under the Linux system". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the gcc command under the Linux system".

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

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

Let's master the basic application of the executable file in the process of compiling it through gcc.

Prepare: first create a new .c file such as 666.c using vim in the current directory

$vim 666.c

(the use of vim can be described in detail in another blog post, which will not be repeated here.)

Insert a piece of code.

# include main () {int areci; axiom6; for (iPretreatment 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

two。 Compile

1. Compilation is to check whether the syntax is wrong or not, and compile the preprocessed file into an assembly file.

two。 Command:

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

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

three。 Compilation

1. Assembler is to generate an assembly file into an object file (binary file) through assembly, and the text code becomes binary code. (the binary code file has the suffix .o).

two。 Command:

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

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

(at this time, it will be garbled because it is a binary file.)

four。 Link

1. The link finds the dependent library file (static and dynamic) and links the target file as an executable program.

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

If there is no dynamic library (general)

direct

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

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

The executable program 666 is generated

If you want to execute using this command:

$. / executable program

Where. / means in the current directory.

For example. / 666

(note: in general, if we use the gcc executable file name, the a.out executable file will be generated by default. / a.out will be used when we execute it directly.)

Thank you for your reading, the above is the content of "how to use the gcc command under the Linux system". After the study of this article, I believe you have a deeper understanding of how to use the gcc command under the Linux system. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: File command preprocessing target program compilation system binary code dynamic file name source file syntax link learning generation that is content directory error Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux vpn NVidia Microsoft Docker