gcc實質是完成程式的編譯和連結,程式的編譯是指從一種檔案類型轉換到另一種檔案類型的過程。一個C語言程式轉換為可執行程式的基本步驟如下:
1、編寫程式(vi,emacs等軟體)
2、程式預編譯(cpp)
3、編譯成彙程式設計式(cc)
4、彙程式設計式(as)
5、連結程式(ld)
其中的這些過程都已經被gcc包含,我們在實際的編譯過程中采用了gcc main.c -o main.exe即可實作一個程式的編譯和連結。并不需要一步一步的實作,但是我們在分析的過程中又必須注意一個C語言檔案的處理過程以及相應的處理程式。
關于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= Display the full path to library
-print-prog-name= Display the full path to compiler component
-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, Pass comma-separated on to the assembler
-Wp, Pass comma-separated on to the preprocessor
-Wl, Pass comma-separated on to the linker
-Xassembler Pass on to the assembler
-Xpreprocessor Pass on to the preprocessor
-Xlinker Pass on to the linker
-combine Pass multiple source files to compiler at once
-save-temps Do not delete intermediate files
-save-temps= 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= Override built-in specs with the contents of
-std= Assume that the input sources are for
--sysroot= Use as the root directory for headers
and libraries
-B Add to the compiler's search paths
-b Run gcc for target , if installed
-V Run gcc version number , 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 Place the output into
-x 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 options must be used.
For bug reporting instructions, please see:
http://bugzilla.redhat.com/bugzilla>.
從上面的結果可以知道基本的用法。
但是還是有幾個需要注意的地方,這也是我們學習gcc時不經常使用,但又非常有用的幾個用法。
1、采用gcc實作預編譯,預編譯可以實作代碼的檢查,特别是宏定義的檢查,通過預編譯檢查實際的代碼是否出錯,這是非常有用的檢查方式。
由于預編譯以後宏定義被擴充了,這時對源碼的分析就能找出代碼宏定義等是否存在錯誤,特别時一些不容易發現的錯誤。
基本的實作形式為:gcc -E file.c > file.pre.c
[gong@Gong-Computer Example]$ vi main.c
gcc的幾個妙用 采用重定向的方式改變輸出流,便于檢查錯誤所在。
[gong@Gong-Computer Example]$ gcc -E main.c > main.pre.c
[gong@Gong-Computer Example]$ vi main.pre.c
gcc的幾個妙用 從上面的結果可以發現我們的宏已經實作了擴充。通過分析宏的擴充可以分析代碼是否正确。
比如我将宏定義max(x,y)改寫為max (x,y)就會出現下面的結果。如下圖所示。
gcc的幾個妙用 從856行的結果我們可以知道,上面的代碼并不是我們需要的情況,這說明我們的代碼存在問題,進而實作了宏定義的檢測。這是非常有用的一種檢測方式。
2、産生鏡像檔案
基本的實作方法是:注意Wl逗号後面跟着需要傳遞的參數,逗号後面不能存在空格,否則出現錯誤。
gcc -Wl,-Map=file.map file.c -o target
關于選項-Wl的使用可以參考help,這是我的一個截圖
gcc的幾個妙用 從上面說明可以知道-Wl用于傳遞參數給連結器。當然也有傳遞給彙編器和預編譯的選項。
通過上面的選項可以得到一個鏡像檔案,通過打開鏡像檔案來程式的結構。
[gong@Gong-Computer Example]$ gcc -Wl,-Map=main.map main.c -o main.exe
[gong@Gong-Computer Example]$ vi main.map
gcc的幾個妙用 上面隻是其中的一部分,還有很多的内容。其中這些内容指出了程式的基本分布情況。
3、彙程式設計式
彙編語言是不可避免要學習的設計語言,但是很多時候并不需要完全手動的編寫彙編語言,我們可以采用gcc實作一段程式的彙編形式,隻需要選擇正确的選項即可。
gcc -S file.c
實作如下:
[gong@Gong-Computer Example]$ gcc -S main.c
[gong@Gong-Computer Example]$ vi main.s
gcc的幾個妙用 從上面的代碼就知道了基本的彙編形式,當然也可以自己設計,但是該選項簡化了彙編語言的設計。
4、在gcc中函數庫,連結庫的調用,這是比較難以掌握和容易出錯的地方。
在靜态編譯的情況下:
gcc file.c -o file -Llibpath -llibname
gcc中-L主要是指明函數庫的查找目錄,-L後緊跟着目錄而不是檔案。-l後面緊跟着需要連接配接的庫名,需要主要的是靜态庫通常是以libfile.a命名,這時-l後的庫名隻能是file,而不是libfile.a。這是需要注意的。一般情況下總是将-l放在最後。但是需要注意的是各個庫之間的依賴關系。依賴關系沒有搞清楚也會導緻編譯出現錯誤。
下面的代碼如下:
foo.c
1 #includestdio.h>
2
3
4 extern void bar();
5
6 void foo()
7 {
8 printf("This is foo ().\n");
9
10 bar ();
11 }
bar.c
1 #include
2
3 void bar()
4 {
5 printf( " This is bar (). \n");
6 }
7
main.c
1 extern void foo();
3 int main()
5 foo();
6
7 return 0;
8 }
~
簡要的介紹一些靜态庫的建立方式。
首先需要注意的時靜态編譯是指将一些庫函數編譯到程式中,這樣會增加程式的大小。動态庫則是在運作過程中添加到程式中,這樣可以減小程式的大小。兩種方式都有各自的優勢。
靜态庫的建立:
gcc -c foo.c -o foo.o
gcc -c bar.c -o bar.o
建立的基本過程就是采用歸檔函數實作。
ar csr libfoo.a foo.o
ar csr libbar.a bar.o
從上面的程式我們可以知道foo程式依賴bar程式,而main程式則依賴foo程式,是以這樣就形成了一定的關系,一般來說隻有将依賴的庫函數寫在最右邊才能保證其他的庫函數依賴該庫函數。
[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調用,是以出錯。而第二種則滿足foo依賴bar,main依賴foo的關系。其中的-L.表示庫函數的搜尋目錄為目前目錄。也可以換成其他的目錄。
是以在gcc中添加庫時,需要注意庫名和庫的順序,最好采用一定的依賴關系圖分析實作。具體的就要我們在設計程式時自己的考慮各個庫函數之間的關系。
至于動态庫的建立可以采用gcc實作。其中的-shared就是表明了該庫是動态庫,-fPCI是指支援PCI,file.o是指需要加載到庫中的二進制檔案。庫名就是libname.so
gcc -shared -fPCI -o libname.so file.o
動态庫的使用可以将建立好的動态庫放在/usr/lib下,然後在函數中即可實作調用。
gcc的其他一些用法:
查找系統檔案路徑:gcc -v main.c
獲得程式的依賴關系:gcc -M main.c ,其中包括了所有的依賴關系,在寫makefile過程中寫依賴關系通常不需要系統頭檔案,這時可以采用gcc -MM main.c去掉系統頭檔案的依賴關系。
[gong@Gong-Computer test]$ gcc -M main.c
main.o: main.c /usr/include/stdio.h /usr/include/features.h \
/usr/include/sys/cdefs.h /usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h /usr/include/gnu/stubs-32.h \
/usr/lib/gcc/i686-redhat-linux/4.5.1/include/stddef.h \
/usr/include/bits/types.h /usr/include/bits/typesizes.h \
/usr/include/libio.h /usr/include/_G_config.h /usr/include/wchar.h \
/usr/lib/gcc/i686-redhat-linux/4.5.1/include/stdarg.h \
/usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h
[gong@Gong-Computer test]$ gcc -MM main.c
main.o: main.c
[gong@Gong-Computer test]$
從上面的兩個結果就可以知道兩個選項的差别,這種差别在編寫Makefile中的依賴關系時非常的有用。特别是第二種形式是比較重要的方式。
關于gcc的使用還是要多實踐才有效果,才能準确的運用。