gccノート基本オプション

6095 ワード

gcc処理プロセス:
1.前処理-E(.c->.i)
gcc -E test.c -o test.i 2. コンパイル-S(.i->.s)
gcc -S test.i
3.アセンブリ-c(.s->.o)
gcc -c test.s
4.実行可能ファイルをリンクして生成する
gcc test.o -o test
 
 
5.ターゲットファイル依存関係を生成するには:
gcc -M test.c
出力:
test.o: test.c/usr/include/stdio.h/usr/include/features.h\ /usr/include/bits/predefs.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-linux-gnu/4.4.5/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-linux-gnu/4.4.5/include/stdarg.h\ /usr/include/bits/stdio_lim.h/usr/include/bits/sys_errlist.h
6.コンパイラ関連情報の印刷
-v  Print (on standard error output) the commands executed to run the stages of compilation.  Also print
           the version number of the compiler driver program and of the preprocessor and the compiler proper.
ヘッダファイルの検索パスなど、詳細を印刷するには、次の方法を使用します.
空のcppファイルを新規作成します.cpp,その後,g++-E-v-P test.cpp、出力は以下の通りです.
     specs。
COLLECT_GCC=/usr/bin/g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/i686-redhat-linux/4.6.2/lto-wrapper
  :i686-redhat-linux
   :../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch=i686 --build=i686-redhat-linux
    :posix
gcc    4.6.2 20111027 (Red Hat 4.6.2-1) (GCC)
COLLECT_GCC_OPTIONS='-E' '-v' '-P' '-shared-libgcc' '-mtune=generic' '-march=i686'
 /usr/libexec/gcc/i686-redhat-linux/4.6.2/cc1plus -E -quiet -v -P -D_GNU_SOURCE test1.cpp -mtune=generic -march=i686
        “/usr/lib/gcc/i686-redhat-linux/4.6.2/include-fixed”
        “/usr/lib/gcc/i686-redhat-linux/4.6.2/../../../../i686-redhat-linux/include”
#include "..."        :
#include <...>        :
 /usr/lib/gcc/i686-redhat-linux/4.6.2/../../../../include/c++/4.6.2
 /usr/lib/gcc/i686-redhat-linux/4.6.2/../../../../include/c++/4.6.2/i686-redhat-linux
 /usr/lib/gcc/i686-redhat-linux/4.6.2/../../../../include/c++/4.6.2/backward
 /usr/lib/gcc/i686-redhat-linux/4.6.2/include
 /usr/local/include
 /usr/include
      。
COMPILER_PATH=/usr/libexec/gcc/i686-redhat-linux/4.6.2/:/usr/libexec/gcc/i686-redhat-linux/4.6.2/:/usr/libexec/gcc/i686-redhat-linux/:/usr/lib/gcc/i686-redhat-linux/4.6.2/:/usr/lib/gcc/i686-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/i686-redhat-linux/4.6.2/:/usr/lib/gcc/i686-redhat-linux/4.6.2/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-E' '-v' '-P' '-shared-libgcc' '-mtune=generic' '-march=i686'

7.前処理禁止(-E)の出力に行番号を含める
-P  Inhibit generation oflinemarkers in the output from the preprocessor.  This might be useful when            running the preprocessor on something that is not C code, and will be sent to a program which might            be confused by the linemarkers.
8.最適化
-O 0非最適化
-O1
-O 2デフォルト最適化レベル
  :http://bbs.nankai.edu.cn/cgi-bin/bbs/bbsanc?path=/groups/GROUP_2/Linux/D4CC04B15/M.1211877692.A
    : 
-O0         (    )
 -O , -O1                  
 -O2      -O1       (    don’t increase over speed (     )
  )
 -Os                 (            –O2)
 -O3          (-O2,     )

-O0   
     -O0    (               ) ,         
                       (  GNU Debugger,gdb)   
  。               ,              。

-O1   (-O)
            ,                          
     。 -O1    , -O0                      
       ,        。
 defer-pop                       
 thread-jumps        (jump)  (threading)  (       )
 branch-probabilities       (branch) profiling(  )     
 cprop-registers                    
 guess-branch-probability             
 Omit-frame-pointer            (    )

-O2   
                  (      -o1      ),    
                (    )
align-loops             
 align-jumps                   
 align-labels          
 align-functions            
 optimize-sibling-calls        sibling(  )    (tail)     
 ces-follow-jumps       CSE ,            
 ces-skip-blocks        CSE ,        
 gcse                  
 expensive-optimizations               
 strength-reduce              
 rerun-cse-after-loop             CSE
 caller-saves            around    
 force-mem                     
 peephole2    sched2      rtl peephole
 regmove                
 strict-aliasing                  
 delete-null-pointer-checks                
 reorder-blocks                    
 schedule-insns                    
 schedule-insns2                   
 rerun-loop-opt                

-Os             -O2       ,              
        。         -O2      :-falign-labels, 
-falign-jumps, -falign-labels  -falign-functions。            
             ,                       ? 

-O3  
 -O3      GCC          。       -O2       
 -finline-functions                      
 -frename-registers              (         )       
    

volatileキーワードで変数を設定すると、最適化を防ぐことができます.
セグメントコードに最適化レベルを設定します.
1 #pragma GCC push_options

2 #pragma GCC optimize ("O0")

3  

4 your code

5  

6 #pragma GCC pop_options 

  :http://blog.csdn.net/fyfhust/article/details/5793482

もう一つの関連概念:memeory barierは、memory barrierにあるコードを順番に実行させる.
9.-WL,optionsリンクにoptionsを渡す
例えば、gcc-Wl、--verbose a.c