“C語言” 讀書札記之[編譯執行]
介紹
現在C語言跨的領域非常之多,如游戲、嵌入式、智能電器等。為什么不直接用匯編或機器語言直接寫呢?原因是匯編和機器語言受到計算機體系結構的影響。直接用某種體系結構的匯編或機器指令寫出來的程序只能在這種體系結構的計算機上運行。
C語言的好處是各種體系結構的計算機都有各自的C編譯器,可以把C程序編譯成各種不同體系結構的機器指令,這意味著用C語言寫的程序只需稍加修改甚至不用修改就可以在各種不同的計算機上編譯運行。
hello,world
我們從簡單hello,world開始——有人說了,又來了,這個“hello,world”,好多博友都寫這種東西煩不煩?呵呵,那我寫的也許跟他們說的不一樣呢?或者理解的跟別人有不一樣的地方。
先寫個程序我們看看。
源程序(或者源文件):是程序員通過文本編輯器創建并保存的文本文件。源程序實際就是由0和1組成的位序列,這些位8個一組,成為字節,每個字節通過ASCII字符集(大部分是)對應一個文本字符。
下面是上面程序對應的16進制數字表示
在終端輸入man ascii看看ascii表
#p#
操作步驟:
查看16進制格式
回復到文本格式
我們看看對應的二進制是什么樣的?——這個也是在磁盤中的存儲狀態。
總結:
系統中所有的信息——包括磁盤文件、存儲器中的程序,存儲器中存放的用戶數據以及網絡上傳送的數據,都是由一串比特表示的。
思考:
同樣的一套字節序列可能在不同的上下文中表示一個整數、浮點數、字符串或者機器指令。——這取決于數據對象的上下文。
我們該做什么?
做為程序員,我們需要了解數字的機器表示方式,因為它們與常見的整數和實數是不同的。
使用參考:在Linux下使用vim配合xxd查看并編輯二進制文件
#p#
翻譯
源程序是能夠被人讀懂的,為了能在系統上運行,我們需要把C語句通過其他程序轉化為一系列的低級機器語言指令。然后這些指令按照一種稱為“可執行目標程序“的格式打包好,并以二進制磁盤文件的形式存放起來。
在Linux系統上,從源文件到目標文件(可執行目標程序文件)的轉化是由編譯器驅動程序完成的。如下圖:
我們用GCC工具觀察每個階段。(GCC:GNU Compiler Collection,GNU編譯器套裝)
先贈送一張gcc命令選項圖
預處理階段
查看main.i的部分源碼
總結:預處理器(cpp)根據以字符#開頭的命令,修改原始C程序,結果得到了另一個C程序(還是C程序),通常以i做為文件擴展名。
編譯階段
查看main.s的源碼
總結:將C語言文本文件翻譯成匯編語言文本文件。——匯編語言是非常有用的,因為它為不同高級語言的不同編譯器提供了通用的輸出語言。
匯編階段
打開源代碼,注意此時為二進制磁盤文件,還有注意操作步驟
此時為亂碼,需要進行:%!xxd操作
查看指令
總結:匯編器(as)將main.s翻譯成機器語言指令,把這些指令打包成為一種“可重定位目標程序“的格式,并將結果保存到main.o文件中,main.o是二進制文件,它的字節編碼是機器語言指令而不是字符(所以用文本編譯器看會出現亂碼)。
鏈接階段
按照匯編階段操作,查看部分指令碼
總結:程序中調用了printf函數,它是C標準庫的一個函數,printf函數存在于一個名為printf.o的單獨的預編譯目標文件中。連接器(ld)負責把printf.o的預編譯目標文件進行并入,結果就得到a.out文件。
NOTE:a.out是可執行的目標文件,而main.o是可重定位目標程序文件。——可執行目標文件加載到系統存儲器后,由系統負責執行。而可重定位目標程序文件是提供給鏈接器使用,執行并入操作。
總結
這一篇主要是學習gcc編譯器的編譯過程,對整個過程有所熟悉。如果有好的知識點,望大家賜教。
#p#
vim配置分享
- " An example for a vimrc file.
- "
- " Maintainer: Bram Moolenaar <Bram@vim.org>
- " Last change: 2006 Nov 16
- "
- " To use it, copy it to
- " for Unix and OS/2: ~/.vimrc
- " for Amiga: s:.vimrc
- " for MS-DOS and Win32: $VIM\_vimrc
- " for OpenVMS: sys$login:.vimrc
- " When started as "evim", evim.vim will already have done these settings.
- if v:progname =~? "evim"
- finish
- endif
- " 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 many 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 hlsearch
- endif
- " 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 'textwidth' 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 the
- " file it was loaded from, thus the changes you made.
- command DiffOrig vert new | 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
- "不使用swap文件
- 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
【編輯推薦】