[UNIX] the generation of an executable file to the distribution (on) of processes in the memory/the generation of executable files, unix executable files

Source: Internet
Author: User

[UNIX] the generation of an executable file to the distribution (on) of processes in the memory/the generation of executable files, unix executable files

From the generation of an executable file to the distribution of processes in the memory (on)

Generation of executable files

To generate executable files for a source program, it must be implemented through the compiler. the compiler translates the high-level languages compiled by programmers into low-level languages (Binary Code) that can be understood by machines ), here, the compiler serves as a translation. The GCC cross-compilation tool is used in Linux.

 

AlthoughGCCIs a compilerBut use GCC fromOneC language programs generate executable files not only in the compilation process, but also through four interrelated steps: preprocessing (also called preprocessing), comlilation, assembly (assembly) and Link (linking)

 

Step 1: preprocessing

Gcc-E test. c-o test. I //-o (LEO) indicates the objective object of the target file; O (LEO) indicates the optimization coefficient.

# Include <stdio. h>

Int main ()

{

Printf ("hello kano ");

Return 0;

}

 

After preprocessing

Typedef int (* _ gconv_trans_fct) (struct _ gconv_step *,

Struct _ gconv_step_data *, void *,

_ Const unsigned char *,

_ Const unsigned char **,

_ Const unsigned char *, unsigned char **,

Size_t *);

...

#2 "test. c" 2

Int main ()

{

Printf ("hello kano ");

Return 0;

}

The above Code shows that in the preprocessing phase, the GCC compiler only merges the header files in the source file and replaces the macro definition with the source file without compiling the code.

 

Step 2: Compile

Gcc-S test. I-o test. s

. File "test. c"

. Section. rodata

. Align 4

. LC0:

. String "hello kano"

. Text

. Globl main

. Type main,

Main:

Pushl p

Movl % esp, p

Subl $8, % esp

Now the assembly language has been programmed in C language.

 

Step 3: assemble

Gcc-c test. s-o hello. o

The compiler has completed the C language to machine code compilation process and generated the target file.

 

Step 4: Link

Gcc testo-o test

To fully implement a program function, some library functions need to be loaded to integrate each target file module into an executable file. At this time, we will mainly discuss how the printf standard library functions are called and loaded to our source program? "Stdio. h "is only the declaration of the function, but does not define the implementation of the function. The system has implemented these functions named libc. so.6 is included in the library file. If it is not specified, Gcc will search for it under the default search path "/usr/lib", that is, link to libc. go to the so.6 library function to implement the function "printf", which is the role of the link.

Function libraries are generally divided into static libraries and dynamic libraries. A static library is used to add all the code of the library file to the executable file when compiling the link. Therefore, the generated file is large, but the library file is no longer needed at runtime. The suffix is generally ". ". On the contrary, the dynamic library does not add the code of the library file to the executable file during the compilation link, but loads the library from the link file during the program execution, this saves the system overhead. The dynamic library is generally suffixed with ". so". As mentioned above, libc. so.6 is the dynamic library. By default, dynamic libraries are used for Gcc compilation.

After the link is completed,

You can generate executable files.

 

Appendix:

. C is a suffix file, c language source code file;

. A is a file with a suffix. It is a file library consisting of the target file;

. C,. cc or. cxx is a C ++ source code file;

. H is a suffix file, which is the header file included by the program;

. I is a file with a suffix. It is a pre-processed C source code file;

. Ii is a file with a suffix. It is a pre-processed C ++ source code file;

. M is a suffix file, which is an Objective-C source code file;

The. o file is the compiled target file;

. S is a suffix file, which is an assembly language source code file;

. S is a precompiled assembly language source code file.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.