久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

專注電子技術(shù)學習與研究
當前位置:單片機教程網(wǎng) >> MCU設(shè)計實例 >> 瀏覽文章

gcc的幾個妙用

作者:龔平   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2014年03月14日   【字體:
gcc的學習在C接觸到linux以后就開始不斷的學習,也知道了一些基本的用法,但是關(guān)于gcc的使用還是有很多值得我們加深的地方。gcc 只是一個編譯工具而已。也就相當于我們在windows環(huán)境下的visual c++等一樣,區(qū)別是visual c++是基于IDE的,而gcc是這些IDE的基礎(chǔ)。學習linux程序設(shè)計必然會學習gcc。
 
gcc實質(zhì)是完成程序的編譯和鏈接,程序的編譯是指從一種文件類型轉(zhuǎn)換到另一種文件類型的過程。一個C語言程序轉(zhuǎn)換為可執(zhí)行程序的基本步驟如下:
1、編寫程序(vi,emacs等軟件)
2、程序預編譯(cpp)
3、編譯成匯編程序(cc)
4、匯編程序(as)
5、鏈接程序(ld)
 
其中的這些過程都已經(jīng)被gcc包含,我們在實際的編譯過程中采用了gcc main.c -o main.exe即可實現(xiàn)一個程序的編譯和鏈接。并不需要一步一步的實現(xiàn),但是我們在分析的過程中又必須注意一個C語言文件的處理過程以及相應(yīng)的處理程序。
 
關(guān)于gcc的基本含義用法就不再詳細的說明了,我覺得最簡單的使用方法是通過軟件的help學習軟件。
[gong@Gong-Computer test]$ gcc --help
Usage: gcc [options] file...
Options:
  -pass-exit-codes         Exit with highest error code from a phase
  --help                   Display this information
  --target-help            Display target specific command line options
  --help={target|optimizers|warnings|params|[^]{joined|separate|undocumented}}[,...]
                           Display specific types of command line options
  (Use '-v --help' to display command line options of sub-processes)
  --version                Display compiler version information
  -dumpspecs               Display all of the built in spec strings
  -dumpversion             Display the version of the compiler
  -dumpmachine             Display the compiler's target processor
  -print-search-dirs       Display the directories in the compiler's search path
  -print-libgcc-file-name  Display the name of the compiler's companion library
  -print-file-name=<lib>   Display the full path to library <lib>
  -print-prog-name=<prog>  Display the full path to compiler component <prog>
  -print-multi-directory   Display the root directory for versions of libgcc
  -print-multi-lib         Display the mapping between command line options and
                           multiple library search directories
  -print-multi-os-directory Display the relative path to OS libraries
  -print-sysroot           Display the target libraries directory
  -print-sysroot-headers-suffix Display the sysroot suffix used to find headers
  -Wa,<options>            Pass comma-separated <options> on to the assembler
  -Wp,<options>            Pass comma-separated <options> on to the preprocessor
  -Wl,<options>            Pass comma-separated <options> on to the linker
  -Xassembler <arg>        Pass <arg> on to the assembler
  -Xpreprocessor <arg>     Pass <arg> on to the preprocessor
  -Xlinker <arg>           Pass <arg> on to the linker
  -combine                 Pass multiple source files to compiler at once
  -save-temps              Do not delete intermediate files
  -save-temps=<arg>        Do not delete intermediate files
  -no-canonical-prefixes   Do not canonicalize paths when building relative
                           prefixes to other gcc components
  -pipe                    Use pipes rather than intermediate files
  -time                    Time the execution of each subprocess
  -specs=<file>            Override built-in specs with the contents of <file>
  -std=<standard>          Assume that the input sources are for <standard>
  --sysroot=<directory>    Use <directory> as the root directory for headers
                           and libraries
  -B <directory>           Add <directory> to the compiler's search paths
  -b <machine>             Run gcc for target <machine>, if installed
  -V <version>             Run gcc version number <version>, if installed
  -v                       Display the programs invoked by the compiler
  -###                     Like -v but options quoted and commands not executed
  -E                       Preprocess only; do not compile, assemble or link
  -S                       Compile only; do not assemble or link
  -c                       Compile and assemble, but do not link
  -o <file>                Place the output into <file>
  -x <language>            Specify the language of the following input files
                           Permissible languages include: c c++ assembler none
                           'none' means revert to the default behavior of
                           guessing the language based on the file's extension
Options starting with -g, -f, -m, -O, -W, or --param are automatically
 passed on to the various sub-processes invoked by gcc.  In order to pass
 other options on to these processes the -W<letter> options must be used.
For bug reporting instructions, please see:
<http://bugzilla.redhat.com/bugzilla>.
 
從上面的結(jié)果可以知道基本的用法。
但是還是有幾個需要注意的地方,這也是我們學習gcc時不經(jīng)常使用,但又非常有用的幾個用法。
1、采用gcc實現(xiàn)預編譯,預編譯可以實現(xiàn)代碼的檢查,特別是宏定義的檢查,通過預編譯檢查實際的代碼是否出錯,這是非常有用的檢查方式。
由于預編譯以后宏定義被擴展了,這時對源碼的分析就能找出代碼宏定義等是否存在錯誤,特別時一些不容易發(fā)現(xiàn)的錯誤。
基本的實現(xiàn)形式為:gcc -E file.c > file.pre.c
[gong@Gong-Computer Example]$ vi main.c
采用重定向的方式改變輸出流,便于檢查錯誤所在。
[gong@Gong-Computer Example]$ gcc -E main.c > main.pre.c
[gong@Gong-Computer Example]$ vi main.pre.c
從上面的結(jié)果可以發(fā)現(xiàn)我們的宏已經(jīng)實現(xiàn)了擴展。通過分析宏的擴展可以分析代碼是否正確。
比如我將宏定義max(x,y)改寫為max (x,y)就會出現(xiàn)下面的結(jié)果。如下圖所示。
從856行的結(jié)果我們可以知道,上面的代碼并不是我們需要的情況,這說明我們的代碼存在問題,從而實現(xiàn)了宏定義的檢測。這是非常有用的一種檢測方式。
 
2、產(chǎn)生鏡像文件
基本的實現(xiàn)方法是:注意Wl逗號后面跟著需要傳遞的參數(shù),逗號后面不能存在空格,否則出現(xiàn)錯誤。
gcc -Wl,-Map=file.map file.c -o target
關(guān)于選項-Wl的使用可以參考help,這是我的一個截圖
從上面說明可以知道-Wl用于傳遞參數(shù)給鏈接器。當然也有傳遞給匯編器和預編譯的選項。
通過上面的選項可以得到一個鏡像文件,通過打開鏡像文件來程序的結(jié)構(gòu)。
[gong@Gong-Computer Example]$ gcc -Wl,-Map=main.map main.c -o main.exe
[gong@Gong-Computer Example]$ vi main.map
上面只是其中的一部分,還有很多的內(nèi)容。其中這些內(nèi)容指出了程序的基本分布情況。
 
3、匯編程序
匯編語言是不可避免要學習的設(shè)計語言,但是很多時候并不需要完全手動的編寫匯編語言,我們可以采用gcc實現(xiàn)一段程序的匯編形式,只需要選擇正確的選項即可。
gcc -S file.c
實現(xiàn)如下:
[gong@Gong-Computer Example]$ gcc -S main.c
[gong@Gong-Computer Example]$ vi main.s
從上面的代碼就知道了基本的匯編形式,當然也可以自己設(shè)計,但是該選項簡化了匯編語言的設(shè)計。

4、在gcc中函數(shù)庫,鏈接庫的調(diào)用,這是比較難以掌握和容易出錯的地方。
在靜態(tài)編譯的情況下:
gcc file.c -o file -Llibpath -llibname 
 
gcc中-L主要是指明函數(shù)庫的查找目錄,-L后緊跟著目錄而不是文件。-l后面緊跟著需要連接的庫名,需要主要的是靜態(tài)庫通常是以 libfile.a命名,這時-l后的庫名只能是file,而不是libfile.a。這是需要注意的。一般情況下總是將-l放在最后。但是需要注意的是各個庫之間的依賴關(guān)系。依賴關(guān)系沒有搞清楚也會導致編譯出現(xiàn)錯誤。
下面的代碼如下:
  1. foo.c

  2.  
  3.   1 #include<stdio.h>
     
  4.   2
     
  5.   3
     
  6.   4 extern void bar();
     
  7.   5
     
  8.   6 void foo()
     
  9.   7 {
     
  10.   8 printf("This is foo ().\n");
     
  11.   9
     
  12.  10 bar ();
     
  13.  11 }

  bar.c

   1 #include<stdio.h>
   2
   3 void bar()
   4 {
   5         printf( " This is bar (). \n");
   6 }
   7

  main.c

   1 extern void foo();
   2
   3 int main()
   4 {
   5         foo();
   6
   7         return  0;
   8 }
~                     

簡要的介紹一些靜態(tài)庫的創(chuàng)建方式。
首先需要注意的時靜態(tài)編譯是指將一些庫函數(shù)編譯到程序中,這樣會增加程序的大小。動態(tài)庫則是在運行過程中添加到程序中,這樣可以減小程序的大小。兩種方式都有各自的優(yōu)勢。
靜態(tài)庫的創(chuàng)建:
gcc -c foo.c -o foo.o
gcc -c bar.c -o bar.o
創(chuàng)建的基本過程就是采用歸檔函數(shù)實現(xiàn)。
ar csr libfoo.a foo.o
ar csr libbar.a bar.o
從上面的程序我們可以知道foo程序依賴bar程序,而main程序則依賴foo程序,所以這樣就形成了一定的關(guān)系,一般來說只有將依賴的庫函數(shù)寫在最右邊才能保證其他的庫函數(shù)依賴該庫函數(shù)。
[gong@Gong-Computer test]$ gcc -o main  main.c -L. -lbar -lfoo
./libfoo.a(foo.o): In function `foo':
foo.c:(.text+0x13): undefined reference to `bar'
collect2: ld returned 1 exit status
 
[gong@Gong-Computer test]$ gcc -o main  main.c -L. -lfoo -lbar
以上的兩個編譯過程只是存在一個差異就是庫的擺放順序存在差別,第一種情況下由于foo依賴bar,而bar庫不能被foo調(diào)用,因此出錯。而第二種則滿足foo依賴bar,main依賴foo的關(guān)系。其中的-L.表示庫函數(shù)的搜索目錄為當前目錄。也可以換成其他的目錄。
 
因此在gcc中添加庫時,需要注意庫名和庫的順序,最好采用一定的依賴關(guān)系圖分析實現(xiàn)。具體的就要我們在設(shè)計程序時自己的考慮各個庫函數(shù)之間的關(guān)系。
 
至于動態(tài)庫的創(chuàng)建可以采用gcc實現(xiàn)。其中的-shared就是表明了該庫是動態(tài)庫,-fPCI是指支持PCI,file.o是指需要加載到庫中的二進制文件。庫名就是libname.so
gcc -shared -fPCI -o libname.so file.o
動態(tài)庫的使用可以將創(chuàng)建好的動態(tài)庫放在/usr/lib下,然后在函數(shù)中即可實現(xiàn)調(diào)用。
 
gcc的其他一些用法:
查找系統(tǒng)文件路徑:gcc -v main.c
獲得程序的依賴關(guān)系:gcc -M main.c ,其中包括了所有的依賴關(guān)系,在寫makefile過程中寫依賴關(guān)系通常不需要系統(tǒng)頭文件,這時可以采用gcc -MM main.c去掉系統(tǒng)頭文件的依賴關(guān)系。
  1. [gong@Gong-Computer test]$ gcc -M main.c
  2. main.o: main.c /usr/include/stdio.h /usr/include/features.h \
     
  3.  /usr/include/sys/cdefs.h /usr/include/bits/wordsize.h \
     
  4.  /usr/include/gnu/stubs.h /usr/include/gnu/stubs-32.h \
     
  5.  /usr/lib/gcc/i686-redhat-linux/4.5.1/include/stddef.h \
     
  6.  /usr/include/bits/types.h /usr/include/bits/typesizes.h \
     
  7.  /usr/include/libio.h /usr/include/_G_config.h /usr/include/wchar.h \
     
  8.  /usr/lib/gcc/i686-redhat-linux/4.5.1/include/stdarg.h \
     
  9.  /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h
     
  10. [gong@Gong-Computer test]$ gcc -MM main.c
     
  11. main.o: main.c
     
  12. [gong@Gong-Computer test]$

從上面的兩個結(jié)果就可以知道兩個選項的差別,這種差別在編寫Makefile中的依賴關(guān)系時非常的有用。特別是第二種形式是比較重要的方式。

關(guān)于gcc的使用還是要多實踐才有效果,才能準確的運用。

關(guān)閉窗口

相關(guān)文章

主站蜘蛛池模板: 亚洲91 | 男人天堂社区 | 日韩欧美精品在线播放 | 天堂av资源 | 日韩精品一区二区三区在线播放 | 亚洲色图50p | 成人在线免费看 | av激情在线 | 国产一区二区成人 | 亚洲高清在线 | 日韩视频一区二区 | 国产精品久久久久久久久久东京 | 亚洲一区二区在线电影 | 久久久久久国产精品 | 日韩欧美在线播放 | 久久不卡 | 欧美一区视频 | 一区二区三区免费在线观看 | 国产成人精品视频在线观看 | 色综合久久久 | 91精品国产91久久久久久吃药 | 成人精品一区亚洲午夜久久久 | 亚州影院 | 一级特黄在线 | 在线黄色网| av高清| 国产成人精品午夜视频免费 | 欧美精品乱码久久久久久按摩 | 女女百合av大片一区二区三区九县 | 欧美高清视频在线观看 | 青娱乐av| 欧美精品一区二区三区在线播放 | 国产精品99久久久久久宅男 | 免费在线黄色av | 国产 欧美 日韩 一区 | 亚洲a网| 国产成人jvid在线播放 | 久久99精品久久久久婷婷 | 一级无毛片 | 四虎午夜剧场 | 国产97碰免费视频 |