C's diary-compilation and execution, and diary Compilation

Source: Internet
Author: User
Tags define null

C's diary-compilation and execution, and diary Compilation

 

Program = algorithm (SOUL) + data structure (processing object) + programming method (appropriate) + language (Tool)


[# Include <stdio. h> ]:C language standard input and output library functions

The above line of code is the first program we have entered the programming world. Today we will start from here to analyze the secrets behind the program.

Certificate ---------------------------------------------------------------------------------------------------------------------------------------

 
When we use these function libraries, (the compilation system) requires us to complete the compilation of these functions.

Include imports a header file (for example, # include <stdio. h>) of the C base library. The header file structure of the standard library is like this.

/* stdio.h standard header */#ifndef _STDIO#define  _STDIO#endif    /*macros*/#define NULL     _NULL#define EOF       -1#define BUFSIZ  512     ... int fclose(FILE *);inf feof(FILE*);int getc(FILE *);int getchar(void);int printf(const char *,...);int putchar(int );int scanf(const char *,...);
   ...

The header file contains macro definition, condition declaration, function declaration, and so on.

/* Printf. c */# include "xstdio. h "... /* use a pointer to perform operations on the output stream */int (printf) (const char * fmt ,...) {int ans; va_list ap; va_start (ap, fmt); ans = _ Printf (......); va end (ap); return (ans );}

Printf. c defines the Hidden Library Function printf called by a macro. When printf is used in a function, the hidden function in the C standard function library must be used to process the string before linking. Then, the system will call the API or variable provided by the system, such as va_list.

It can be roughly understood as declaring in the header file, implementing in the standard library function, and calling in the source code. The basis for implementation is to provide APIs through the operating system.

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

[Preliminary knowledge]: computer software structure; compilation and link Process

[Computer software structure]

Development tools/Applications
| Function interface
Function library ------------- C basic function library, Java swing function library, etc. o /. obj | Linux glibc API for operating system interface (Application Programming Interface), windows API system Runtime Library ------------- Linux Runtime Library, Windows Runtime library, user-Defined libraries and other compiled binary files. o /. obj | System Call interface system kernel ------------- hardware driver: a program designed by the hardware developer to run hardware based on the system call interface of an OS. | Hardware interface/| \ hardware -----------> ---- |

 

[Compilation process]

// Example: hello. c # include <stdio. h> void main {printf ("Hello world ");}

 [Pre-compile]Hello. c + stdio. h-> hello. I
Delete # define, expand the macro definition (# Replace the uppercase constant after define;
Process conditional pre-compiled commands, # if and so on;
Processing # include pre-compilation command, insert the files included in include to the current command location;
Delete all annotation tags;


[Compile]Hello. I-> hello. s
Function: The program is compiled into an assembly code file through syntax, lexical, and semantic checks;
Steps:
<1> lexical analysis: divides the program code into a series of marks, such [! (= And so on, and then classify and place the mark!
Mark category: keyword
Identifier-> symbol table
Literal> text table
Special symbols
<2> syntax analysis: analyze the syntax of the mark to obtain the syntax tree!
The syntax tree is a tree with expressions as nodes. The leaf node of the tree is a variable, and the non-leaf node is a symbolic constant.
<3> Semantic Analysis: Performs static Semantic Analysis on the syntax tree, such as type judgment and type conversion!
After semantic analysis, the variable types forced to be converted are updated to the symbol table.
[Compiler: source code optimization]
Function: converts a syntax tree to an intermediate code (for example, a three-address code) and performs a simple operation.

----------- ATT: the source code and intermediate code are irrelevant to the machine !, The operation after the intermediate code is related to the machine !! ------------

[Assembly]Hello. s-> hello. o
Function: Compile the code into machine commands according to the table of comparison;
Steps:
<1> code generation: different code sequences (assembly language> machine language) are generated based on the characters, registers, and Data Types of the target machine );
<2> target code optimization: select an appropriate addressing method and Displacement Calculation multiplication to perform operations on the binary data.

----------- ATT: The variable address space is still not allocated after the target file is compiled, and the variable/function address is allocated at the link !! ------------

Static Link]Hello. o + X. o + XXX. o-> the final Executable File .exe/. deb
Requirement: After a program is divided into multiple modules, inter-module communication, for example, variable access/function access, must use the absolute address of the variable/function in the memory.
Function: links the target files and libraries of multiple modules to form executable files. The most common library is the Runtime Library.
Note: the target file cannot complete tasks by itself. For example, the printf function in hello. c is correct in the header file, but the header file is implemented only when the system Runtime Library is called.
The Runtime Library is a set of basic functions that support program running.
The library is essentially a group of packaged and stored target files !!
Steps:
<1> address and space allocation: The system allocates space for variables/functions based on their types.
<2> symbol resolution: modifies the reference of variable or function symbols.
<3> relocation: Perform assembly and other operations during compilation, but set all operation addresses to 0. during compilation, replace 0 with the allocated address, we will relocate the address correction process. In fact, the address is patched to point them to the correct address.

[C's diary-Static and Dynamic Links]

 

--------------------------- Let's summarize (only one static link and one dynamic link can exist )---------------------------------

 

[Compilation]
The Compiling System (different systems) is to convert the source code (the same on different systems) to the Target Program (. obj) file by compiling.

 

[LINK] (static link)

The function (API) of the operating system library converted from the target program and the C standard library is connected (the executable file segment is loaded into the virtual memory of the Application)-> the target file (such as .exe on Windows) that can be executed by a system is essentially a machine binary file.


[Execution]

  [LINK] (Dynamic Link)Static link smart edition, which needs to be checked during connection

  [Officially executed]
Different versions are available for different systems when downloading software. We can download the compiled executable files and use various execution methods (double-click the GUI and use the CLI Commands) load the executable binary file into the memory to form a process, and then schedule the process to the CPU clock pulse for execution, user input and output or other services.


Certificate ----------------------------------------------------------------------------------------------------------------------------------------------

With the above foundation, we start to think about it ~

Q: What is a function library?

A: We can simply understand that there is a library with multiple files defined in advance, which contains one or more functions.

Q: What is this database?

A: It can be understood as <stdio. h> in C, that is, the standard input and output Library and the jar package in java.

Q: What is a pre-defined file?

A: A compiled but unlinked source code is a file, such as. o in C and. class in Java.

Q: How do I understand functions in files?

A: The function is defined in the source code. For example, you have defined a function add (int x, int y ){...};; you can introduce/import the library by calling and use these functions, such as add (2, 3 );

Q: How to understand import/import

A: You have access permission .. or you can say that you can access this function before you can talk about using it, and include <stdio. h> and import .. in disguise, this access permission is provided. However, they are different. include is to insert the function library to the current position. import provides a short description of the path of the class library. If necessary, find the class library in that path.

Q: Can this function library/class library be used in common?

A: Most of the target files compiled by different compilers cannot be used in common due to different naming rules and formats. Most of the class libraries on different operating systems cannot be used because different system APIs are called, the conclusion is that class libraries compiled by a specific compiler can be used only on a specific system. Of course, java is an exception. It will be said later, but java cannot use all the class libraries compiled by other compilers.

Q: How is a function library used?

A: After the access permission is granted, the system calls the API in the source code. If it is a static link, first link the library file and then run it. If it is a dynamic link, run it first, and then perform smart links during running.

......

 

The following things are all said on the shoulders of the above knowledge points, and the height determines the field of view ~ Please correct me for understanding the error. Thank you!
---------------------------------------------------------------------------------
[Relationship between C standard library functions and operating system APIs]
The library functions and data types of Standard C can be compiled and executed on any operating system and the results are the same. However, the internal implementation and storage methods may not be the same, there is no program that does not depend on the underlying layer. The C compiler can be considered as a special application in the operating system. It serves as a bridge between the application and the operating system. The library function called by C is a function located in the system kernel, this library function runs based on the System-provided APIs (operating system functions). Both windows and linux have their own APIs. These APIs are the same (such as standard library functions of C ), some differences (file storage functions ). These API functions are composed of an assembly language or binary code that can be recognized by the underlying hardware.
In linux and windows, C's standard library functions must be the same (compatible), that is, the APIs and implementation methods provided by the operating system are the same. However, other non-standard functions may not be the same,

---------------------------------------------------------------------------------
[Why does linux not support direct opening of exe files ?]
Frist: What is an operating system?
An operating system is the most basic user software used to manage and control hardware and software resources. It is located between hardware and application software and provides interfaces between hardware and software.
The compiler compiles source programs and library functions into binary executable programs based on the APIS provided by the operating system for two reasons:
<1> the formats of executable files are different: PE for windows and ELF for linux;
<2> the APIS provided by different systems are different, and only some functions are the same. (wine uses API conversion to generate the corresponding dll functions corresponding to windows in linux)

Programs that can run directly on windows platforms are compiled and have been associated with system APIs or hardware, therefore, applications cannot be transplanted between different systems.

However, the Java source program can still be transplanted after compilation.
----------------------------------------------------------------------------------
[How is java cross-platform]
First, what is cross-platform? cross-platform is not source code cross-platform. The software compiled by a specific compiler can only be executed on the corresponding operating system.
Therefore, cross-platform platforms must intercept language encoding and operating system barriers and cannot be directly compiled into machine languages. Otherwise, they are related to the platform.
Therefore, Java uses the [Java compiler] to compile the source program into a [General intermediate code (bytecode)]. The intermediate code is irrelevant to the platform.
Configure different [Interpreters] for different systems, and call different APIs through intermediate code conversion through Java virtual machines in different operating systems.
Thus, one compilation is implemented and runs everywhere!
----------------------------------------------------------------------------------

[How to Implement Android project release]
After using an IDE to import an android project, you can import some officially provided support-v7 class libraries in the class library folder, this class library can be run on multiple systems with the knowledge we have. The reason is very simple: the Java compiler compiles it into an intermediate code, which is irrelevant to the platform, the interpreter can be used across platforms. Let's change a compiler, such as gcc. The final assembly step in the compilation process is naturally related to machines. Therefore, the class libraries compiled by gcc cannot be used on machines different from those of the original gcc system, it cannot be linked on a non-gcc compiler.

Let's talk about the android project, we can imagine the process of writing the project to the mobile phone: After we click run as android application, this project and its class library (including the support-v7) compile the file into the target file (not linked) on the IDE and then send it to the mobile phone for running. During runtime, the source code target file first calls the imported class library file to perform the corresponding operations, links the system API (Dynamic Link), and loads the library file to the virtual memory, after the library files on the current page are created, the ing between the virtual memory and the physical memory is established. The page is displayed.
----------------------------------------------------------------------------------
[Programming language execution method]
<1> C, C ++, and Visual Basic are directly compiled. Disadvantage: Only compilers applicable to windows systems can be used. Advantage: Fast execution speed.
<2> Html, JavaScript, and explanation. Disadvantage: it cannot be compiled. It can only be explained and is easy to expose. Advantage: cross-platform.
<3> Java,. Net. One compilation, explained everywhere. Disadvantages: slow execution speed; advantages: security, cross-platform, and garbage collection.
----------------------------------------------------------------------------------
Attached:
32 keywords in C Language
Keywords are words that have been used by the C language itself and cannot be used for other purposes. For example, keywords cannot be used as variable names or function names.
A total of 32 C language keywords defined by ANSI:
Auto double int struct break else long switch
Case enum register typedef char extern return union
Const float short unsigned continue for signed void
Default goto sizeof volatile do if while static
Keywords can be divided into two categories: Data Type keywords and process control keywords.
 

Related Article

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.