新聞中心

gcc的幾個(gè)妙用

作者: 時(shí)間:2016-12-01 來源:網(wǎng)絡(luò) 收藏
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)換到另一種文件類型的過程。一個(gè)C語言程序轉(zhuǎn)換為可執(zhí)行程序的基本步驟如下:
1、編寫程序(vi,emacs等軟件)
2、程序預(yù)編譯(cpp)
3、編譯成匯編程序(cc)
4、匯編程序(as)
5、鏈接程序(ld)
其中的這些過程都已經(jīng)被gcc包含,我們?cè)趯?shí)際的編譯過程中采用了gcc main.c -o main.exe即可實(shí)現(xiàn)一個(gè)程序的編譯和鏈接。并不需要一步一步的實(shí)現(xiàn),但是我們?cè)诜治龅倪^程中又必須注意一個(gè)C語言文件的處理過程以及相應(yīng)的處理程序。
關(guān)于gcc的基本含義用法就不再詳細(xì)的說明了,我覺得最簡單的使用方法是通過軟件的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 compilers target processor
-print-search-dirs Display the directories in the compilers search path
-print-libgcc-file-name Display the name of the compilers 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 compilers 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 files 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:
.
從上面的結(jié)果可以知道基本的用法。
但是還是有幾個(gè)需要注意的地方,這也是我們學(xué)習(xí)gcc時(shí)不經(jīng)常使用,但又非常有用的幾個(gè)用法。
1、采用gcc實(shí)現(xiàn)預(yù)編譯,預(yù)編譯可以實(shí)現(xiàn)代碼的檢查,特別是宏定義的檢查,通過預(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ò)展。通過分析宏的擴(kuò)展可以分析代碼是否正確。
上一頁 1 2 3 下一頁

關(guān)鍵詞: gcclinux編譯工

評(píng)論


技術(shù)專區(qū)

關(guān)閉