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

gcc的幾個(gè)妙用

作者:龔平   來(lái)源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年03月14日   【字體:
gcc的學(xué)習(xí)在C接觸到linux以后就開始不斷的學(xué)習(xí),也知道了一些基本的用法,但是關(guān)于gcc的使用還是有很多值得我們加深的地方。gcc 只是一個(gè)編譯工具而已。也就相當(dāng)于我們?cè)趙indows環(huán)境下的visual c++等一樣,區(qū)別是visual c++是基于IDE的,而gcc是這些IDE的基礎(chǔ)。學(xué)習(xí)linux程序設(shè)計(jì)必然會(huì)學(xué)習(xí)gcc。
 
gcc實(shí)質(zhì)是完成程序的編譯和鏈接,程序的編譯是指從一種文件類型轉(zhuǎn)換到另一種文件類型的過(guò)程。一個(gè)C語(yǔ)言程序轉(zhuǎn)換為可執(zhí)行程序的基本步驟如下:
1、編寫程序(vi,emacs等軟件)
2、程序預(yù)編譯(cpp)
3、編譯成匯編程序(cc)
4、匯編程序(as)
5、鏈接程序(ld)
 
其中的這些過(guò)程都已經(jīng)被gcc包含,我們?cè)趯?shí)際的編譯過(guò)程中采用了gcc main.c -o main.exe即可實(shí)現(xiàn)一個(gè)程序的編譯和鏈接。并不需要一步一步的實(shí)現(xiàn),但是我們?cè)诜治龅倪^(guò)程中又必須注意一個(gè)C語(yǔ)言文件的處理過(guò)程以及相應(yīng)的處理程序。
 
關(guān)于gcc的基本含義用法就不再詳細(xì)的說(shuō)明了,我覺得最簡(jiǎn)單的使用方法是通過(guò)軟件的help學(xué)習(xí)軟件。
[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é)果可以知道基本的用法。
但是還是有幾個(gè)需要注意的地方,這也是我們學(xué)習(xí)gcc時(shí)不經(jīng)常使用,但又非常有用的幾個(gè)用法。
1、采用gcc實(shí)現(xiàn)預(yù)編譯,預(yù)編譯可以實(shí)現(xiàn)代碼的檢查,特別是宏定義的檢查,通過(guò)預(yù)編譯檢查實(shí)際的代碼是否出錯(cuò),這是非常有用的檢查方式。
由于預(yù)編譯以后宏定義被擴(kuò)展了,這時(shí)對(duì)源碼的分析就能找出代碼宏定義等是否存在錯(cuò)誤,特別時(shí)一些不容易發(fā)現(xiàn)的錯(cuò)誤。
基本的實(shí)現(xiàn)形式為:gcc -E file.c > file.pre.c
[gong@Gong-Computer Example]$ vi main.c
采用重定向的方式改變輸出流,便于檢查錯(cuò)誤所在。
[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)實(shí)現(xiàn)了擴(kuò)展。通過(guò)分析宏的擴(kuò)展可以分析代碼是否正確。
比如我將宏定義max(x,y)改寫為max (x,y)就會(huì)出現(xiàn)下面的結(jié)果。如下圖所示。
從856行的結(jié)果我們可以知道,上面的代碼并不是我們需要的情況,這說(shuō)明我們的代碼存在問(wèn)題,從而實(shí)現(xiàn)了宏定義的檢測(cè)。這是非常有用的一種檢測(cè)方式。
 
2、產(chǎn)生鏡像文件
基本的實(shí)現(xiàn)方法是:注意Wl逗號(hào)后面跟著需要傳遞的參數(shù),逗號(hào)后面不能存在空格,否則出現(xiàn)錯(cuò)誤。
gcc -Wl,-Map=file.map file.c -o target
關(guān)于選項(xiàng)-Wl的使用可以參考help,這是我的一個(gè)截圖
從上面說(shuō)明可以知道-Wl用于傳遞參數(shù)給鏈接器。當(dāng)然也有傳遞給匯編器和預(yù)編譯的選項(xiàng)。
通過(guò)上面的選項(xiàng)可以得到一個(gè)鏡像文件,通過(guò)打開鏡像文件來(lái)程序的結(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、匯編程序
匯編語(yǔ)言是不可避免要學(xué)習(xí)的設(shè)計(jì)語(yǔ)言,但是很多時(shí)候并不需要完全手動(dòng)的編寫匯編語(yǔ)言,我們可以采用gcc實(shí)現(xiàn)一段程序的匯編形式,只需要選擇正確的選項(xiàng)即可。
gcc -S file.c
實(shí)現(xiàn)如下:
[gong@Gong-Computer Example]$ gcc -S main.c
[gong@Gong-Computer Example]$ vi main.s
從上面的代碼就知道了基本的匯編形式,當(dāng)然也可以自己設(shè)計(jì),但是該選項(xiàng)簡(jiǎn)化了匯編語(yǔ)言的設(shè)計(jì)。

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

簡(jiǎn)要的介紹一些靜態(tài)庫(kù)的創(chuàng)建方式。
首先需要注意的時(shí)靜態(tài)編譯是指將一些庫(kù)函數(shù)編譯到程序中,這樣會(huì)增加程序的大小。動(dòng)態(tài)庫(kù)則是在運(yùn)行過(guò)程中添加到程序中,這樣可以減小程序的大小。兩種方式都有各自的優(yōu)勢(shì)。
靜態(tài)庫(kù)的創(chuàng)建:
gcc -c foo.c -o foo.o
gcc -c bar.c -o bar.o
創(chuàng)建的基本過(guò)程就是采用歸檔函數(shù)實(shí)現(xiàn)。
ar csr libfoo.a foo.o
ar csr libbar.a bar.o
從上面的程序我們可以知道foo程序依賴bar程序,而main程序則依賴foo程序,所以這樣就形成了一定的關(guān)系,一般來(lái)說(shuō)只有將依賴的庫(kù)函數(shù)寫在最右邊才能保證其他的庫(kù)函數(shù)依賴該庫(kù)函數(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
以上的兩個(gè)編譯過(guò)程只是存在一個(gè)差異就是庫(kù)的擺放順序存在差別,第一種情況下由于foo依賴bar,而bar庫(kù)不能被foo調(diào)用,因此出錯(cuò)。而第二種則滿足foo依賴bar,main依賴foo的關(guān)系。其中的-L.表示庫(kù)函數(shù)的搜索目錄為當(dāng)前目錄。也可以換成其他的目錄。
 
因此在gcc中添加庫(kù)時(shí),需要注意庫(kù)名和庫(kù)的順序,最好采用一定的依賴關(guān)系圖分析實(shí)現(xiàn)。具體的就要我們?cè)谠O(shè)計(jì)程序時(shí)自己的考慮各個(gè)庫(kù)函數(shù)之間的關(guān)系。
 
至于動(dòng)態(tài)庫(kù)的創(chuàng)建可以采用gcc實(shí)現(xiàn)。其中的-shared就是表明了該庫(kù)是動(dòng)態(tài)庫(kù),-fPCI是指支持PCI,file.o是指需要加載到庫(kù)中的二進(jìn)制文件。庫(kù)名就是libname.so
gcc -shared -fPCI -o libname.so file.o
動(dòng)態(tài)庫(kù)的使用可以將創(chuàng)建好的動(dòng)態(tài)庫(kù)放在/usr/lib下,然后在函數(shù)中即可實(shí)現(xiàn)調(diào)用。
 
gcc的其他一些用法:
查找系統(tǒng)文件路徑:gcc -v main.c
獲得程序的依賴關(guān)系:gcc -M main.c ,其中包括了所有的依賴關(guān)系,在寫makefile過(guò)程中寫依賴關(guān)系通常不需要系統(tǒng)頭文件,這時(shí)可以采用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]$

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

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

關(guān)閉窗口

相關(guān)文章