新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > gcc的幾個妙用

        gcc的幾個妙用

        作者: 時間:2016-12-01 來源:網絡 收藏
        gcc的學習在C接觸到linux以后就開始不斷的學習,也知道了一些基本的用法,但是關于gcc的使用還是有很多值得我們加深的地方。gcc 只是一個編譯工具而已。也就相當于我們在windows環境下的visual c++等一樣,區別是visual c++是基于IDE的,而gcc是這些IDE的基礎。學習linux程序設計必然會學習gcc。
        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 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:
        .
        從上面的結果可以知道基本的用法。
        但是還是有幾個需要注意的地方,這也是我們學習gcc時不經常使用,但又非常有用的幾個用法。
        1、采用gcc實現預編譯,預編譯可以實現代碼的檢查,特別是宏定義的檢查,通過預編譯檢查實際的代碼是否出錯,這是非常有用的檢查方式。
        由于預編譯以后宏定義被擴展了,這時對源碼的分析就能找出代碼宏定義等是否存在錯誤,特別時一些不容易發現的錯誤。
        基本的實現形式為: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
        從上面的結果可以發現我們的宏已經實現了擴展。通過分析宏的擴展可以分析代碼是否正確。
        上一頁 1 2 3 下一頁

        關鍵詞: gcclinux編譯工

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 施秉县| 宁晋县| 陆河县| 天等县| 华坪县| 平阴县| 张家界市| 保定市| 抚州市| 来宾市| 仁布县| 瓦房店市| 凤阳县| 龙口市| 兴业县| 甘孜县| 温宿县| 三穗县| 茶陵县| 马鞍山市| 兴安县| 云霄县| 泽州县| 简阳市| 农安县| 边坝县| 金乡县| 定兴县| 蒙山县| 克东县| 夹江县| 奉贤区| 资兴市| 镇坪县| 平阳县| 龙山县| 扶绥县| 四会市| 富源县| 蓬安县| 永州市|