"UNIX" is distributed in memory from the generation of an executable file to the process (top)

Source: Internet
Author: User

A source program to generate executable files, need to be implemented by the compiler, and the role of the compiler is to translate the programmer's high-level language into the machine can read the low-level language (binary code), where the compiler plays the role of translation. The GCC cross-compilation tool is used in the Linux operating system .

Although gcc is a compiler , using gcc from a C A language program generates executables that are more than just a compilation process, but go through four interrelated steps: preprocessing (also known as precompilation,preprocessing), compilation (comlilation ), assembly (Assembly) and links (linking)

First step: Preprocessing

GCC-E test.c-o test.i//-O (Small Europe) indicates the target file objective target; O (Greater Europe) indicates the optimization factor

#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 can be seen in the preprocessing phase of the GCC compiler only the header file in the source file merge and the macro definition is replaced by the source file, no code compilation

Step Two: Compiling

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

At this point, the C language has been programmed by the Assembly

Step Three: assembly

Gcc-c Test.s-o hello.o

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

Fourth Step : link

GCC Testo-o Test

A complete implementation of a program's functionality also requires some library functions to be loaded after the various object file modules are consolidated into one executable file. At this point we will mainly go to which printf standard library functions are called and loaded into our source program? In the pre-compilation included in the "Stdio.h" is only the declaration of the function, and does not define the implementation of the function, the system is the implementation of these functions are known as the Libc.so.6 library file, when not specifically specified, GCC will go to the system default search path "/usr/lib" to find , which is linked to the Libc.so.6 library function, so that the function "printf" can be implemented, which is the role of the link.

function library is generally divided into static library and dynamic library two kinds. Static library refers to the compilation of links, the library files of the code are all added to the executable file, so the resulting file is relatively large, but at run time also no longer need the library file. The suffix is generally ". a". Dynamic libraries In contrast, when compiling a link does not add the code of the library file to the executable file, but the library is loaded by the runtime link file when the program executes, which can save the system overhead. The general suffix of the dynamic library is named ". So", as described in the previous libc.so.6 is a dynamic library. GCC uses dynamic libraries by default at compile time.

Once the link is complete, GCC can generate the executable file

Appendix:

. c is a suffix file, C language Source Code documents;

. A is a suffix file, which is the archive file consisting of the target file;

. c,.cc or. cxx as the suffix of the file, is a C + + source code file;

. h is a suffix file, which is the header file contained in the program;

. I is the suffix of the file, is already preprocessed C source code files;

. II is a suffix file, which is a pre-preprocessed C + + source code file;

. M is the suffix of the file that is objective-c source code files;

. O is the suffix of the file, is the compiled target file;

. s is the suffix of the file, is the assembly language source code files;

 . S is the suffix of the file, is a precompiled assembly language source code files.

"UNIX" is distributed in memory from the generation of an executable file to the process (top)

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.