<> introduction

hollo.c Is the first program written by most programmers , This blog will introduce compiling and running hello.c, What the operating system will do .

<> Compiling system

hello.c It's an advanced C Language code , The format is as follows
#include <stdio.h> int main() { printf("hello world\n"); return 0; }
Run the program in the system , You have to C Conversion of language code into low-level machine language instructions , These instructions follow the executable target file
Well arranged , And stored on disk as binary files , Form executable target file .
For example visual studio and devc++ Etc , Did it all for programmers , If we use gcc The compiler can see step by step , Changes in procedures .

ad locum GCC Drive file read hello.c, And the executable file is generated hello, This can be divided into four steps , Pretreatment , compile , assembly , link , It looks messy , We will explain them separately below

<> Pretreatment

command :gcc -E hello.c -o hello.i

Preprocessing is reading c source program , Pseudo instructions in it ( with # Instructions at the beginning , It's a macro ) And special symbols “ replace ” handle ; After this treatment , Generate a macro without a definition , No conditional compilation instructions , Output files without special symbols . The meaning of this file is the same as that of the source file without preprocessing , Still C file . But the content is different .

Pseudo instructions mainly include the following three aspects :

(1) Macro definition instruction , as #define Name TokenString,#undef And some macros built in the compiler , as __DATE__, FILE, LINE,
TIME, FUNCTION etc .

(2) Conditional compilation instructions , as #ifdef,#ifndef,#else,#elif,#endif etc .
(3) Header file contains instructions , as #include "FileName" perhaps #include etc .

Execute the command to get hello.i, Open as follows

take #include<stdio.h> Replace with 800 Multiline code , And the comments are removed , One thing to note is that , In the pretreatment stage , Does not check for syntax errors .

<> compile

command :gcc -S hello.i -o hello.o
Compile time , Compiler will text file hello.i Convert to text file hello.s

At this stage, the compiler mainly does lexical analysis , Grammatical analysis , Semantic analysis, etc , After checking that there are no errors , Translate code into assembly language [2]. available gcc Parameters of -S See .
compiler (ccl) Text file hello.i Translate to text file hello.s, It contains an assembly language program .
This is very useful , It provides the same output file format for different compilers in different high-level languages .
open hello.s

<> assembly

command :gcc -c hello.s -o hello.o

Assembler will hello.s Translate into machine language instructions , Then follow these instructions to redirect the target program
Format storage of , Results saved in hello.o In binary file , If we open it hello.o I'll see a lot of garbled code , as follows .

<> link

printf The function exists in a printf.o In a separate precompiled target file . It must be incorporated into hello.o In the process of , Linker is responsible for the merging of the two , The result is hello file , It is an executable target file

Randall E. Bryant : In depth understanding of computer operating system ( Third Edition ): Mechanical Industry Press ,2015.

Technology
©2019-2020 Toolsou All rights reserved,
Python Basic knowledge and notes Programmer Tanabata Valentine's Day confession code NOI2019 travels China's longest high speed rail officially opened ! The fastest way to finish the race 30.5 hour C Language programming to find a student's grade Software testing BUG describe ESP8266/ESP32 System : Optimize system startup time Unity Scene loading asynchronously ( Implementation of loading interface ) Baidu , Ali , Tencent's internal position level and salary structure , With job suggestions !PYTHON Summary of final review