Home>Article>Operation and Maintenance> What is the process of generating s files under linux called?

What is the process of generating s files under linux called?

青灯夜游
青灯夜游 Original
2022-06-17 11:46:25 3781browse

The process of generating s files under Linux is called "compiling". The compiler gcc in Linux will perform syntax analysis, semantic analysis and optimization on the preprocessed ".i" file to generate an assembly code file (".s" file). The execution syntax is "gcc -S filename.i - o file name.s"; then the assembler will convert the assembly code file into an intermediate object file; finally the linker ld will link the object file with the required static link library and dynamic link library to become an executable file.

What is the process of generating s files under linux called?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

The process of compiling a C file into an executable file under Linux

Using gcc to compile a C file into an executable file can be separated It is four steps: pre-compilation, compilation, assembly, and connection.

1. Precompile (generate.ifile)

The precompiler cpp compiles the source files and related header files ( For example, the header file stdio.h in the example code is precompiled into an .i file.

Command executed:gcc -E file name.c -o file name.i

Function of precompilation:

a, process all "#include" precompilation directive

b, process all "#define" directives, delete all "#define" in the code, and expand all macro definitions

c, Process all conditional precompilation directives, such as #if #elif #else #ifdef #ifnodef #endif, etc.

d, delete all comments

e, add line numbers and file name identifiers, In order to give a prompt message when an error occurs

2,Compile (generate.sfile)

The compiler gcc performs syntax analysis, semantic analysis and optimization on the preprocessed files to generate assembly code files.

Executed command:gcc -S file name.i -o file name.s

##3. Assembly (generate .o file)

The assembler converts the assembly code file into an intermediate object file

Command executed:

gcc -c file name.s -o file name.o(Note: here It is lowercase -c, not uppercase -C. I stepped on the trap here and an exception occurred)

4. Link (generate executable file)

The linker ld links the target file with all required additional target files (such as static link library, dynamic link library) into an executable file

Executed command:

gcc file name. o -o File name

Example of compiling a c file into an executable file under Linux

1. Prepare C File

In the command line mode, enter: vim hello.c

to enter the editing mode, enter the following code:

#include int main(void){ printf("Hello World!\n"); return 0; }

First click the ESC key to exit the editing mode, and then Enter: wq (note the colon when typing) to return to the command line.

2. Compile

Compile the C file into an executable file

Command to execute:

gcc hello.c -o hello

Enter the ls command and you can see that there is an additional file in the current folder: hello

3. Execute

Execute:

./hello

can output hello World!

Related recommendations: "

Linux Video Tutorial"

The above is the detailed content of What is the process of generating s files under linux called?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn