“C語言” 讀書劄記(三)之[編譯執行]“C語言” 讀書劄記之[程式和程式設計語言]

來源:互聯網
上載者:User
 介紹   再續前緣“C語言” 讀書劄記之[程式和程式設計語言]。   現在C語言跨的領域非常之多,如遊戲、嵌入式、智能電器等。為什麼不直接用彙編或機器語言直接寫呢?原因是彙編和機器語言受到電腦體繫結構的影響。 直接用某種體繫結構的彙編或機器指令寫出來的程式只能在這種體繫結構的電腦上運行。   C語言的好處是各種體繫結構的電腦都有各自的C編譯器,可以把C程式編譯成各種不同體繫結構的機器指令,這意味著用C語言寫的程式 只需稍加修改甚至不用修改就可以在各種不同的電腦上編譯運行。 

  hello,world   我們從簡單hello,world開始——有人說了,又來了,這個“hello,world”,好多博友都寫這種東西煩不煩?呵呵,那我寫的也許跟他們說的不一樣呢?或者理解的跟別人有不一樣的地方。   先寫個程式我們看看。  
來源程式(或者源檔案):是程式員通過文字編輯器建立並儲存的文字檔。來源程式實際就是由0和1組成的位序列,這些位8個一組,成為位元組,每個位元組通過ASCII字元集(大部分是)對應一個文本字元。
                                 下面是上面程式對應的16進位數字表示

                                  在終端輸入man ascii看看ascii表

操作步驟:

             

             查看16進位格式

             回複到文字格式設定

 

                                 我們看看對應的二進位是什麼樣的?——這個也是在磁碟中的儲存狀態。

 

 總結:  系統中所有的資訊——包括磁碟檔案、儲存空間中的程式,儲存空間中存放的使用者資料以及網路上傳送的資料,都是由一串位元表示的。 思考:  同樣的一套位元組序列可能在不同的上下文中表示一個整數、浮點數、字串或者機器指令。——這取決於資料對象的上下文。 我們該做什嗎?  做為程式員,我們需要瞭解數位機器表示方式,因為它們與常見的整數和實數是不同的。 使用參考:  在Linux下使用vim配合xxd查看並編輯二進位檔案 

 翻譯      來源程式是能夠被人讀懂的,為了能在系統上運行,我們需要把C語句通過其他程式轉化為一系列的低級機器語言指令。然後這些指令按照一種稱為“可執行目標程式“的格式打包好,並以 二進位磁碟檔案的形式存放起來。   在Linux系統上,從源檔案到目標檔案(可執行目標程式檔案)的轉化是由編譯器驅動程式完成的。如:

  我們用GCC工具觀察每個階段。( GCCGNU Compiler CollectionGNU編譯器套裝) 先贈送一張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編譯器的編譯過程,對整個過程有所熟悉。如果有好的知識點,望大家賜教。   要知後事如何,且聽下回分解。 

推薦  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"  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 modeset backspace=indent,eol,startif has("vms")  set nobackup      " do not keep a backup file, use versions insteadelse  set nobackup      " keep [NO] backup fileendifset history=250     " keep 250 lines of command line historyset ruler       " show the cursor position all the timeset showcmd     " display incomplete commandsset incsearch      " do incremental searchingset 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 formattingmap 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 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 '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 ENDelse  set autoindent        " always set autoindenting onendif " 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 easilyset softtabstop=4set shiftwidth=4set tabstop=4set noexpandtabset smarttabset showmatchset foldenableset autoindentset smartindentset cindentset foldmethod=markerset background=darkset nobackupset encoding=utf-8language en_US.utf8set fileencodings=ucs-bom,utf-8,gbk,latin1set fileformats=unix,dos,macset fileencoding=utf-8set fileformat=unixset fileformat=unixset matchtime=15" Make shift-insert work like in Xtermmap <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=30nmap  wm :WMToggle

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.