gcc的學習在C接觸到linux以后就開始不斷的學習,也知道了一些基本的用法,但是關于gcc的使用還是有很多值得我們加深的地方。gcc 只是一個編譯工具而已。也就相當于我們在windows環(huán)境下的visual c++等一樣,區(qū)別是visual c++是基于IDE的,而gcc是這些IDE的基礎。學習linux程序設計必然會學習gcc。
gcc實質是完成程序的編譯和鏈接,程序的編譯是指從一種文件類型轉換到另一種文件類型的過程。一個C語言程序轉換為可執(zhí)行程序的基本步驟如下:
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=<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
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.
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>.
<http://bugzilla.redhat.com/bugzilla>.
從上面的結果可以知道基本的用法。
但是還是有幾個需要注意的地方,這也是我們學習gcc時不經常使用,但又非常有用的幾個用法。
1、采用gcc實現預編譯,預編譯可以實現代碼的檢查,特別是宏定義的檢查,通過預編譯檢查實際的代碼是否出錯,這是非常有用的檢查方式。
由于預編譯以后宏定義被擴展了,這時對源碼的分析就能找出代碼宏定義等是否存在錯誤,特別時一些不容易發(fā)現的錯誤。
基本的實現形式為: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]$ gcc -E main.c > main.pre.c
[gong@Gong-Computer Example]$ vi main.pre.c
從上面的結果可以發(fā)現我們的宏已經實現了擴展。通過分析宏的擴展可以分析代碼是否正確。
比如我將宏定義max(x,y)改寫為max (x,y)就會出現下面的結果。如下圖所示。
比如我將宏定義max(x,y)改寫為max (x,y)就會出現下面的結果。如下圖所示。
從856行的結果我們可以知道,上面的代碼并不是我們需要的情況,這說明我們的代碼存在問題,從而實現了宏定義的檢測。這是非常有用的一種檢測方式。
2、產生鏡像文件
基本的實現方法是:注意Wl逗號后面跟著需要傳遞的參數,逗號后面不能存在空格,否則出現錯誤。
gcc -Wl,-Map=file.map file.c -o target
關于選項-Wl的使用可以參考help,這是我的一個截圖
從上面說明可以知道-Wl用于傳遞參數給鏈接器。當然也有傳遞給匯編器和預編譯的選項。
通過上面的選項可以得到一個鏡像文件,通過打開鏡像文件來程序的結構。
[gong@Gong-Computer Example]$ gcc -Wl,-Map=main.map main.c -o main.exe
[gong@Gong-Computer Example]$ vi main.map
[gong@Gong-Computer Example]$ vi main.map
上面只是其中的一部分,還有很多的內容。其中這些內容指出了程序的基本分布情況。
3、匯編程序
匯編語言是不可避免要學習的設計語言,但是很多時候并不需要完全手動的編寫匯編語言,我們可以采用gcc實現一段程序的匯編形式,只需要選擇正確的選項即可。
gcc -S file.c
實現如下:
[gong@Gong-Computer Example]$ gcc -S main.c
[gong@Gong-Computer Example]$ vi main.s
[gong@Gong-Computer Example]$ vi main.s
從上面的代碼就知道了基本的匯編形式,當然也可以自己設計,但是該選項簡化了匯編語言的設計。
4、在gcc中函數庫,鏈接庫的調用,這是比較難以掌握和容易出錯的地方。
在靜態(tài)編譯的情況下:
gcc file.c -o file -Llibpath -llibname
gcc中-L主要是指明函數庫的查找目錄,-L后緊跟著目錄而不是文件。-l后面緊跟著需要連接的庫名,需要主要的是靜態(tài)庫通常是以 libfile.a命名,這時-l后的庫名只能是file,而不是libfile.a。這是需要注意的。一般情況下總是將-l放在最后。但是需要注意的是各個庫之間的依賴關系。依賴關系沒有搞清楚也會導致編譯出現錯誤。
下面的代碼如下:
- foo.c
- 1 #include<stdio.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<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)編譯是指將一些庫函數編譯到程序中,這樣會增加程序的大小。動態(tài)庫則是在運行過程中添加到程序中,這樣可以減小程序的大小。兩種方式都有各自的優(yōu)勢。
靜態(tài)庫的創(chuàng)建:
gcc -c foo.c -o foo.o
gcc -c bar.c -o bar.o
創(chuàng)建的基本過程就是采用歸檔函數實現。
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
./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.表示庫函數的搜索目錄為當前目錄。也可以換成其他的目錄。
以上的兩個編譯過程只是存在一個差異就是庫的擺放順序存在差別,第一種情況下由于foo依賴bar,而bar庫不能被foo調用,因此出錯。而第二種則滿足foo依賴bar,main依賴foo的關系。其中的-L.表示庫函數的搜索目錄為當前目錄。也可以換成其他的目錄。
因此在gcc中添加庫時,需要注意庫名和庫的順序,最好采用一定的依賴關系圖分析實現。具體的就要我們在設計程序時自己的考慮各個庫函數之間的關系。
至于動態(tài)庫的創(chuàng)建可以采用gcc實現。其中的-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下,然后在函數中即可實現調用。
動態(tài)庫的使用可以將創(chuàng)建好的動態(tài)庫放在/usr/lib下,然后在函數中即可實現調用。
gcc的其他一些用法:
查找系統(tǒng)文件路徑:gcc -v main.c
獲得程序的依賴關系:gcc -M main.c ,其中包括了所有的依賴關系,在寫makefile過程中寫依賴關系通常不需要系統(tǒng)頭文件,這時可以采用gcc -MM main.c去掉系統(tǒng)頭文件的依賴關系。
- [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的使用還是要多實踐才有效果,才能準確的運用。