"C Language" Reading Notes (3) [compilation and execution] "C Language" Reading Notes [programs and programming languages]

Source: Internet
Author: User
This section describes [programming languages] In the Reading Notes of C language. Currently, C language is widely used in many fields, such as games, embedded systems, and Smart appliances. Why not write data directly in assembly or machine language? The reason is that assembly and machine language are affected by the computer architecture. Written directly by means of an architectural assembly or machine instruction.ProgramIt can only run on a computer with this architecture.   The advantage of C language is that Computers of various architectures have their own C compilers, which can compile C programs into machine commands of different architectures, which means programs written in C Language You can compile and run the program on different computers with only a few modifications or even no modifications. .

Hello, world we started from simple hello, world -- some people said, and again, this "Hello, world", many bloggers are not bothered to write such a thing? Well, maybe I wrote something different from what they said? Or you may understand something different from others. First, write a program.
Source program (or source file): a text file created and saved by a programmer in a text editor. The source program is actually a bit sequence consisting of 0 and 1. These 8 digits are in a group and become bytes. Each byte corresponds to a text character through the ASCII character set (most of which are.
The following is the hexadecimal number representation corresponding to the above program

Enter man ASCII on the terminal to view the ASCII table

Procedure:

View hexadecimal format

Reply to text format

 

Let's see what the corresponding binary is like? -- This is also the storage status on the disk.

 

Summary: all the information in the system, including disk files, stored programs, user data stored in the memory, and data transmitted over the network, is represented by a string of bits. Thinking: the same set of byte sequences may represent an integer, floating point number, string, or machine instruction in different contexts. -- This depends on the context of the data object. What should we do? As a programmer, we need to know the machine representation of numbers, because they are different from common integers and real numbers. Usage reference: Use Vim in Linux with xxd to view and edit binary files

The Source translation program can be understood by humans. In order to run on the system, we need to convert the C statement into a series of low-level machine language commands through other programs. Then these commands are packaged in a format called "executable Target Program" and Binary Disk Files . In Linux, the conversion from the source file to the target file (executable target program file) is completed by the compiler driver. For example:

We use the GCC tool to observe each stage. ( Gcc : GNU Compiler Collection , GNU Compiler set )   First, a GCC Command Option diagram is provided.  

  Preprocessing phase

View some source code of Main. I

Conclusion: The pre-processor (CPP) modifies the original C program based on the command starting with "#" And returns another C program (or C program), which usually uses "I" as the file extension.

Compilation phase

 

View the source code of Main. S.

 

Summary: translate a C language text file into an assembly language text file. -- Assembly language is very useful because it provides a general output language for different compilers in different advanced languages.

 

Assembly stage  

Open the source code. Note that it is a binary disk file, and pay attention to the operation steps.

This is garbled and requires: %! Xxd operation

View commands

 

Summary: The assembler (AS) translates main. s into machine language instructions and packs these commandsRelocationFormat of the Target Program and save the result to main. o file, Main. O is a binary file, and its Byte encoding is a machine language instruction rather than a character (So garbled characters may appear in the text compiler ).

 

Link stage  

Perform operations in the assembly phase to view some script codes.

 

Summary: The printf function is called in the program. It is a function of the C standard library. The printf function exists in a separate pre-compiled target file named printf. O. The connector (LD) is responsible for merging the pre-compiled target file of printf. O, and the result is the. out file.

Note: A. out is an executable target file, while Main. O is a relocated target program file. -- The system is responsible for executing the executable target file after it is loaded into the system memory. The target program file that can be relocated is provided to the linker for merging.

To sum up this article, I learned the compilation process of the GCC compiler and became familiar with the entire process. If you have good knowledge, I hope you can give me some advice. You need to know what is going on, and listen to the next decomposition.

Recommended Vim configuration sharing
"  An example for a vimrc file.  ""  Maintainer: Bram moolenaar <Bram@vim.org>  "  Last change: 2006 Nov 16  ""  To use it, copy it  "  For UNIX and OS/2 :~ /. Vimrc  "  For Amiga: S:. vimrc  "  For MS-DOS and Win32: $ Vim \ _ vimrc  " For OpenVMS: sys $ login:. vimrc  "  When started  " Evim "  , Evim. Vim will already have done these settings.  If V: progname = ~? "  Evim  "  Finishendif  "  Use Vim settings, rather then VI settings (much better !).  " This must be first, because it changes other options as a side effect.  Set  Nocompatible  "  Allow backspacing over everything in insert mode  Set Backspace = Indent, EOL, start  If Has ( "  VMS  "  )  Set Nobackup " Do not keep a backup file, use versions instead  Else    Set Nobackup "  Keep [No] backup file  Endif  Set History = 250       "  Keep 250 lines of command line history  Set Ruler "  Show the cursor position all the time  Set Showcmd"  Display incomplete commands  Set Incsearch "  Do incremental searching  Set  Nu  "  For Win32 Gui: Remove 't'flag from 'guioptions': No tearoff menu entries  "  Let & guioptions = substitute (& guioptions,  " T "  , ""  ,  " G "  )  "  Don't use ex mode, Use Q for formatting  Map Q GQ  "  In your terminal emulators the mouse works just fine, thus enable it.  Set Mouse = A  "  Switch syntax highlighting on, when the terminal has colors  " Also switch on highlighting the last used search pattern.  If & T_co> 2 | Has ( "  Gui_running  "  ) Syntax on  Set  Hlsearchendif  "  Only do this part when compiled with support for autocommands.  If Has ( "  Autocmd "  )  "  Enable file type detection.    "  Use the default filetype settings, so that mail gets 'tw 'set to 72,    "  'Cindent 'is on in C files, etc.    "  Also load indent files, to automatically do language-dependent indenting.  Filetype plugin indent on  "  Put these in an autocmd group, so that we can delete them easily. Augroup vimrcex Au ! "  For all text files set 'textidth' to 78 characters. Autocmd filetype text setlocal textwidth = 78    "  When editing a file, always jump to the last known cursor position.    "  Don't do it when the position is invalid or when inside an event handler    "  (Happens when dropping a file on gvim ). Autocmd bufreadpost * \  If Line ( "  '\"  " )> 0 & Line ( "  '\"  " ) <= Line ( "  $  " ) | \ Exe  "  Normal! G '\"  " | \ Endif autocmd vimleave *\  If Has ( "  Mksession  " ) & Exists ( "  V: this_session  " ) & V: this_session! = "" | \ Exec  "  Mksession!  " V: this_session | \ Endif augroup end Else    Set Autoindent "  Always set autoindenting on  Endif  "  Has (  " Autocmd "  )  "  Convenient command to see the difference between the current buffer and  "  File it was loaded from, thus the changes you made. Command difforig vertNew | Set Bt = nofile | r # | 0d _ | Diffthis \ | Wincmd p | Diffthis  "  Add By journeylee to make things more easily  Set Softtabstop = 4  Set Shiftwidth = 4  Set Tabstop = 4  Set  Noexpandtab Set  Smarttab  Set  Showmatch  Set  Foldenable  Set  Autoindent  Set  Smartindent  Set  Cindent  Set Foldmethod = Marker  Set Background = Dark Set  Nobackup  Set Encoding = UTF- 8  Language en_us.utf8  Set Fileencodings = ucs-bom, UTF- 8  , GBK, Latin1  Set Fileformats = UNIX, DOS, Mac  Set Fileencoding = UTF- 8  Set Fileformat = UNIX Set Fileformat = UNIX  Set Matchtime = 15  "  Make shift-insert work like in xterm Map <s-Insert> <middlemouse> Map ! <S-Insert> <middlemouse> "  Let xterm16_brightness = 'default'  " Change If  Needed  " Let xterm16_colormap = 'allblue'  " Change If  Needed  "  Colo xterm16  "  Do not use swap files  Map <F5> :! Php-l % <CR> "  Set runtimepath + =/home/zhoubc/opt/Vim/doc  "  Autocmd bufnewfile, bufread *. Ros, *. Inc, *. php set keywordprg =  " Help"  Let G: winmanagerwindowlayout = '  Taglist | fileexplorer  '  Let G: winmanagerwidth = 30  Nmap wm: wmtoggle 

 

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.