GCC使用指南
使用语法:6 S* o, U4 c, D1 H
gcc [ option | filename ]...; ~' A6 W# [. U$ x7 e
g++ [ option | filename ]...
+ |2 q' `: |( ~$ `% G3 _# c# o! G- }2 H1 ^) O
其中 option 为 gcc 使用时的选项(后面会再详述),
/ x. m$ ~ x# V- V( j5 ~! a 而 filename 为欲以 gcc 处理的文件! |1 y& @0 s) R2 e
说明:
j, }, m+ |( F, y5 z1 d% b: u2 w; ^ 这 C 与 C++ 的 compiler 已将产生新程序的相关程序整合起来。产+ U1 M3 h% c! @8 I) j7 Z5 X4 u L6 f! G
生一个新的程序需要经过四个阶段:预处理、编译、汇编,连结,
$ v# a, c7 Z1 W, q$ L 而这两个编译器都能将输入的文件做不同阶段的处理。虽然原始程序
& \# ~! T* f0 K! x; J 的扩展名可用来分辨编写原始程序码所用的语言,但不同的 compiler k; a' D) l$ I) O5 O5 {; J$ J
,其预设的处理程序却各不相同:' S) r8 z* j, u, E1 P
y2 A: S& a- f8 _7 @( S& z gcc 预设经由预处理过(扩展名为.i)的文件为 C 语言,并於程" o! @' ~3 ^/ h. D# ]$ v: W" b
式连结阶段以 C 的连结方式处理。
8 g( |/ l$ A5 `5 W1 t1 o; |/ g, C7 V9 Q! n, Q% H4 @
g++ 预设经由预处理过(扩展名为.i)的文件为 C++ 语言,并於程
$ V- P. A7 N2 Z. i) E" q; T& |& t" `$ _3 ?3 f. C
序连结阶段以 C++ 的连结方式处理。9 c; j" f% x+ X4 {+ R
- ~7 w3 d% M# r0 N9 c; X
1 d7 f# W3 ~# N6 N- q. i7 Q7 u5 C
原始程序码的扩展名指出所用编写程序所用的语言,以及相对应的处4 }" Q3 R. q4 S# g# G
理方法:* y& w f! j% Y# H( Z0 {# v* _% W
& m0 V+ g( j( V* s .c C 原始程序 ; 预处理、编译、汇编
/ i5 } _9 K) y. x: i" H- e .C C++ 原始程序 ; 预处理、编译、汇编
: w) j. }3 G3 b/ q8 s6 [ .cc C++ 原始程序 ; 预处理、编译、汇编
$ c+ {. t) J$ A .cxx C++ 原始程序 ; 预处理、编译、汇编- f/ y% s X( o2 N6 M* Y1 V! ?/ C
.m Objective-C 原始程序 ; 预处理、编译、汇编
# J8 p* c$ F5 i6 C .i 已经过预处理之 C 原始程序 ; 编译、汇编) i: a1 X& @: J0 @3 w* x" ^
.ii 已经过预处理之 C++ 原始程序 ; 编译、汇编/ @& |* G' Y# t, |
.s 组合语言原始程序 ; 汇编
" F2 w, |% l! ]0 V8 [, u9 n* ]; j .S 组合语言原始程序 ; 预处理、汇编
& J5 C* \2 g8 X0 N .h 预处理文件(标头文件) ; (不常出现在指令行)
3 }) |5 U0 A/ H3 E- p6 p/ a* d H' B) S. ?0 j- I7 U
, l. G" {( ]: W+ T8 p 其他扩展名的文件是由连结程序来处理,通常有:
2 r8 J3 I: t1 n. [5 F% s2 w
6 |: n( V. l b .o Object file
1 G' ?& ~% J; [3 f6 C U7 N .a Archive file+ C- b/ f' v2 i: z
; g, J3 G$ c4 ^/ p
8 I8 M1 A) ^) f 除非编译过程出现错误,否则 "连结" 一定是产生一个新程序的最1 v6 O( a" T+ M$ f" ]! b# I
後阶段。然而你也可以以 -c、-s 或 -E 等选项,将整个过程自四+ a7 l M. S6 t R
个阶段中的其中一个停止。在连结阶段,所有与原始码相对应的) y6 I7 e% V! `& w( u
.o 文件、程序库、和其他无法自文件名辨明属性的文件(包括不以 .o
; O, G* m2 V0 D 为扩展名的 object file 以及扩展名为 .a 的 archive file)都会( g+ |* e! F4 V, {+ V: q
交由连结程序来处理(在指令行将那些文件当作连结程序的参数传给
1 ~, l+ S3 l& Z- @8 Z' D" k 连结程序)。* _* b7 d% N: G# D' A. V
. p. r c' N! ~) R0 K& ?9 j3 z8 N$ K( z8 x( k* `9 _
选项:
1 T1 m/ y9 X" }- L9 p 不同的选项必须分开来下:例如 `-dr' 这个选项就与 `-d -r' 大# T1 S! h; m; S' D* |# i
不相同。
$ w5 c O" U( l
: t7 a) e" l5 r+ V! i/ d 绝大部份的 `-f' 及 `-W' 选项都有正反两种形式:-fname 及; t H( M" Q1 t# F+ N; Y& K# Y
-fno-name (或 -Wname 及 -Wno-name)。以下只列出非预设的那个& C$ @+ i2 \7 G5 E0 G# Z# i' x- @
形式。
# k% K, g. w" u0 A5 ]' R# A/ Q0 d# o
以下是所有选项的摘要。以形式来分类。选项的意义将另辟小节说8 p l* ~' C0 S" w4 z# F( ~
明。; D7 ^* ~9 l( E3 K9 y! u+ |
( k4 o0 n6 d, W* o; ^ 一般性(概略、常用的)选项
3 N3 w9 ~6 j, V* R1 R -c -S -E -o file -pipe -v -x language
7 [3 I( n+ O& X0 ~6 i* j: `# N( E- f, }0 X! Z9 C6 Z; n
程序语言选项
) j9 O% `# P6 A/ n4 e' o -ansi -fall-virtual -fcond-mismatch
# ~1 z+ ~/ F9 P/ P6 K) J. x5 u -fdollars-in-identifiers -fenum-int-equiv- p+ _! A |0 n/ b
-fexternal-templates -fno-asm -fno-builtin
4 l% y9 G8 w w% L* s% z -fno-strict-prototype -fsigned-bitfields4 A* h* A, M$ g, V
-fsigned-char -fthis-is-variable5 q2 T( ^4 s% D7 @: u$ H) X
-funsigned-bitfields -funsigned-char
* b2 P% h& [: V -fwritable-strings -traditional -traditional-cpp& q" C( K7 J7 N! U, o; c* N
-trigraphs% p/ `3 H* j' z# a" i1 @8 |) ]2 L
, ?% [2 L6 V. S. A" M8 R" e7 n 编译时的警告选项
2 n- o1 w$ w4 W( u# K o2 y l! z -fsyntax-only -pedantic -pedantic-errors -w -W
) n1 Q% h. _3 L6 z1 p0 U- Z -Wall -Waggregate-return -Wcast-align -Wcast-qual
, H/ w7 D: e' g9 v* o -Wchar-subscript -Wcomment -Wconversion8 d4 T& X5 p! s- p+ m4 l
-Wenum-clash -Werror -Wformat -Wid-clash-len
) P$ T# @, j3 N -Wimplicit -Winline -Wmissing-prototypes
( m* J# u* V! ?( r4 d/ V# y; C -Wmissing-declarations -Wnested-externs -Wno-import2 c* k" a- ?4 i" u- r, N
-Wparentheses -Wpointer-arith -Wredundant-decls; d9 b# C; {# `: N& l! p& q1 Y
-Wreturn-type -Wshadow -Wstrict-prototypes -Wswitch
J* [8 k' F* N; f, a -Wtemplate-debugging -Wtraditional -Wtrigraphs& T4 Q9 v4 {$ M( f
-Wuninitialized -Wunused -Wwrite-strings
; A- r6 S: x) O: D' w2 @
) W' g3 d/ K: o1 w 除错选项
- w% o8 G1 k4 d$ N1 ^ -a -dletters -fpretend-float -g -glevel -gcoff
7 `$ d7 r1 _6 F -gxcoff -gxcoff+ -gdwarf -gdwarf+ -gstabs -gstabs+
3 R" u/ ]: \6 O% ]1 b -ggdb -p -pg -save-temps -print-file-name=library! J. ]$ o1 D0 |$ |. v& ?# E. x' I
-print-libgcc-file-name -print-prog-name=program( f8 G" {$ V& H! y$ e7 ^
5 a* Z1 G& ]! r1 m
最佳化选项
( L$ C! n! P% |$ P -fcaller-saves -fcse-follow-jumps -fcse-skip-blocks$ A/ _5 u3 {' R
-fdelayed-branch -felide-constructors7 N( ^- H7 o9 S, T/ ^( _
-fexpensive-optimizations -ffast-math -ffloat-store6 K! S, \7 g: g5 ]
-fforce-addr -fforce-mem -finline-functions* }3 |! D- y+ I3 c0 X$ h |
-fkeep-inline-functions -fmemoize-lookups. r% G& }* m* ^# B! c
-fno-default-inline -fno-defer-pop
+ h" M' u" V& r- f -fno-function-cse -fno-inline -fno-peephole
" T% o( n. [5 p& [/ I -fomit-frame-pointer -frerun-cse-after-loop& v. F: d. d- r: V
-fschedule-insns -fschedule-insns2
' @, w1 k- x8 R" _8 x -fstrength-reduce -fthread-jumps -funroll-all-loops' \: k' c2 L; H) t, b4 O1 |% ^
-funroll-loops -O -O2
, M3 b, N4 X9 D. a3 M
L1 |8 R( {% i" z8 k 预处理选项
! x9 F4 x3 ^, M7 z5 Z- [ -Aassertion -C -dD -dM -dN -Dmacro[=defn] -E -H
0 ]+ G" B: A! m% f9 r y" m9 P -idirafter dir -include file -imacros file -iprefix* d, J' `# w: {# d
file -iwithprefix dir -M -MD -MM -MMD -nostdinc -P/ k7 t+ R9 I# M. ]; b8 \& t
-Umacro -undef
2 ]- H9 {+ W' C0 _ ~1 a' I; g# W& W1 @; b
汇编程序选项+ Q' O0 w2 \) V; S8 t# I7 ?
-Wa,option
+ [) e/ Z9 k7 ^; l% F
2 p! C- t) t5 P- Z& s 连结程序选项
# v$ z; }* b6 F1 G -llibrary -nostartfiles -nostdlib -static -shared
) ^* h1 `! P! Y8 Z% ]( J- e" ` -symbolic -Xlinker option -Wl,option -u symbol9 H W5 B1 `! B) q$ T
2 }& X0 T* K- N3 R6 u; m4 c 目录选项
1 S8 E, k) M2 V. [& s -Bprefix -Idir -I- -Ldir- `1 l1 p6 a' ?, Q1 m$ |' y
3 ^, Q. y# P# v3 P) K, V+ E$ Y Target Options% f# O( O) I0 V6 E! t' p0 N3 o
-b machine -V version& |9 Q6 c7 ~* c: L$ y6 ~. ?
7 t. }1 p F* l5 B/ ?( u+ G' s 与机器(平台)相关的选项
0 W* @! I/ X# [5 o1 k) ?# T M680x0 Options. r1 i* J7 U' |
-m68000 -m68020 -m68020-40 -m68030 -m68040 -m688817 c" k0 D. b% T: e2 x* C
-mbitfield -mc68000 -mc68020 -mfpa -mnobitfield) r7 @' U% a& w+ X% R
-mrtd -mshort -msoft-float+ o/ k- A/ x B$ d# A, o6 b
* Y# p) k& G9 p) L$ I6 ]
VAX Options- x" f F5 Y) |% [' I; }4 T
-mg -mgnu -munix3 t" u& D: c% j5 j( b
& o5 u6 C" d+ c0 m6 o' n
SPARC Options' `& v9 P7 L( x2 Z
-mepilogue -mfpu -mhard-float -mno-fpu8 ], d }* l( W# f
-mno-epilogue -msoft-float -msparclite -mv8
) O- _: r% x4 x) G$ Q" a -msupersparc -mcypress
0 `& }, W4 x9 K% r& |/ R7 c
+ L4 o6 A# u! l$ _- J) N: {8 a Convex Options) \2 t" T+ M) l9 o6 C4 h
-margcount -mc1 -mc2 -mnoargcount
3 W" m4 N, u! \9 }
3 n g) b; z: E7 g0 o% S AMD29K Options
2 t/ d' p: [$ C9 s; e) Y8 u: s' q' Z -m29000 -m29050 -mbw -mdw -mkernel-registers! }; ^# i; O8 l# O2 y( P N3 P, y
-mlarge -mnbw -mnodw -msmall -mstack-check
5 ~8 W. I( D- a7 U -muser-registers3 D% b1 N5 n2 z! W! K
( H: g3 p- i. E" e M88K Options
0 R9 P# B6 W1 ~! O' |8 d -m88000 -m88100 -m88110 -mbig-pic
/ n9 l* Q; L( V1 k" x; Z6 Z -mcheck-zero-division -mhandle-large-shift
0 d ]9 Z9 ]" J4 L' P' q -midentify-revision -mno-check-zero-division. c0 _7 U7 k$ F% {* L
-mno-ocs-debug-info -mno-ocs-frame-position
; n M% r0 R# ?" [9 _ -mno-optimize-arg-area -mno-serialize-volatile! ^# {# y1 U; T3 ~$ i% [
-mno-underscores -mocs-debug-info& G, \ }* w+ A1 S8 {* g; x, q* y
-mocs-frame-position -moptimize-arg-area) F% S- n) G7 H4 R0 ~- q8 L4 Y- B& Z2 z
-mserialize-volatile -mshort-data-num -msvr3 -msvr4
( f& u7 m( j4 N7 h7 d- o K5 N3 d9 d -mtrap-large-shift -muse-div-instruction
9 ?9 k5 W @6 R( Q -mversion-03.00 -mwarn-passed-structs+ D. h! b( B' }! h! c
. d, e) X+ u# B" a6 a
RS6000 Options
; C% t" V4 B% h. Q% `; q& D -mfp-in-toc -mno-fop-in-toc
6 I6 e& U D! u; c
6 _, x* v' a+ t0 n- |7 Z# i) p$ a1 F RT Options+ k! O. o. u! D2 g q
-mcall-lib-mul -mfp-arg-in-fpregs -mfp-arg-in-gregs
+ S( X' ?5 Q9 U -mfull-fp-blocks -mhc-struct-return -min-line-mul
* Y& g2 M- M; Q* }. p2 n -mminimum-fp-blocks -mnohc-struct-return3 b5 {. t l" T3 ^$ c) n- R) W' @! W
+ ]5 a I; x1 ^
MIPS Options+ A+ H; Z' y c: ]
-mcpu=cpu type -mips2 -mips3 -mint64 -mlong64
. G' l) `% w! G -mlonglong128 -mmips-as -mgas -mrnames -mno-rnames0 V; P) M4 ^8 {# o
-mgpopt -mno-gpopt -mstats -mno-stats -mmemcpy
8 J* s3 W' z" @; J! t/ F1 Q -mno-memcpy -mno-mips-tfile -mmips-tfile
5 H6 T7 U7 a% A4 D- w# g3 X% B -msoft-float -mhard-float -mabicalls -mno-abicalls9 A# y2 t' ?" [9 P" C4 j. V b6 k
-mhalf-pic -mno-half-pic -G num -nocpp" d: X8 d" Y6 j2 }- k1 r# x
) p) B$ j! n9 f
i386 Options
* `7 N1 R" G: Y8 g -m486 -mno-486 -msoft-float -mno-fp-ret-in-387
+ ?0 m5 W0 G' {1 ?" Y
6 d* N8 @/ B/ b5 n: p HPPA Options
0 `% Z$ ^* t7 I$ Q -mpa-risc-1-0 -mpa-risc-1-1 -mkernel -mshared-libs9 P+ H2 g( |5 o- c# Y ]
-mno-shared-libs -mlong-calls -mdisable-fpregs0 z1 L# X4 [: m* F* R
-mdisable-indexing -mtrailing-colon
1 a$ U- G3 w% U7 b* U! K
% ^- T& p0 a8 L, e8 m5 m+ K i960 Options
/ g3 v$ ?* p2 ], f. r/ h$ G }, R# s- y -mcpu-type -mnumerics -msoft-float
' d7 ^. z/ [. f7 n; x -mleaf-procedures -mno-leaf-procedures -mtail-call
% J1 T: P3 G' E3 x# b -mno-tail-call -mcomplex-addr -mno-complex-addr- N* X. L4 m! o; a8 e
-mcode-align -mno-code-align -mic-compat% V. f3 |) b4 y
-mic2.0-compat -mic3.0-compat -masm-compat' Z( M( C9 m# G) R
-mintel-asm -mstrict-align -mno-strict-align
4 U4 m" {4 [9 R5 g' [5 R6 j, j -mold-align -mno-old-align
1 r4 Y/ a8 N* r- U0 t* x; d2 y3 M7 k. v
DEC Alpha Options
, V, r. c/ u, {7 J -mfp-regs -mno-fp-regs -mno-soft-float -msoft-float- Z+ T; _7 d' K4 v
; l, m s) k! [5 p. h/ I) U System V Options: @4 _2 n6 V+ D9 K
-G -Qy -Qn -YP,paths -Ym,dir' g0 A" C6 `3 J+ ]' \
( O9 _1 u% d" M' h4 t
Code Generation Options0 m7 j9 C2 D! u6 l( A8 v0 @
-fcall-saved-reg -fcall-used-reg -ffixed-reg' |4 X% G. d& i1 s. l
-finhibit-size-directive -fnonnull-objects/ K9 Y8 r0 {( w7 O. A
-fno-common -fno-ident -fno-gnu-linker' j6 j3 v; T: k# ~; o
-fpcc-struct-return -fpic -fPIC
1 G. P- r( j5 J6 k+ H -freg-struct-returno -fshared-data -fshort-enums4 z! m( B' ]8 Q* a, i! P6 b
-fshort-double -fvolatile -fvolatile-global7 x- z# B) e3 z
-fverbose-asm: l' p! ~& y# w& f3 H; X+ [
8 g% ?' g5 t. G% p" E9 B
PRAGMAS
' }' s) Q: ~6 n! {. Y- G Two `#pragma' directives are supported for GNU C++, to9 ]) o1 I# b6 h5 n5 ?3 n5 i' @
permit using the same header file for two purposes: as a; ~% f# L! O$ K: T0 I5 ^
definition of interfaces to a given object class, and as
, L7 k$ c( M9 d! t# |4 w the full definition of the contents of that object class.
; |: y1 I v) e! i7 `
0 z0 U* n V/ t, ]/ e #pragma interface2 R7 s5 ?, r y
(C++ only.) Use this directive in header files
& O$ a% O0 V% i: v7 c) L" N* \ that define object classes, to save space in most. V1 B2 W/ I- l! e) K/ ?3 ^
of the object files that use those classes. Nor-
# s3 [' W/ D: ~1 B7 d1 Y2 \ l6 j6 A mally, local copies of certain information (backup
9 H# Q x) P: @$ m; ?% ]4 S copies of inline member functions, debugging infor-' s( g3 m* y/ Z3 a$ i; p
mation, and the internal tables that implement vir-
! x; c) N+ N/ r tual functions) must be kept in each object file
3 p8 R8 H: X. r4 G0 w that includes class definitions. You can use this& I! n8 Q2 Q3 U) u1 S7 E7 r5 S
pragma to avoid such duplication. When a header- C+ g( F1 V) x3 ~7 g* v# `6 ^( z
file containing `#pragma interface' is included in: C# F" V1 o0 Q. z3 M3 Y+ l" Q
a compilation, this auxiliary information will not0 O2 i4 F5 `* t: d" [* _
be generated (unless the main input source file it-
, N* {, U' \$ T6 l! P! Y0 v* @ self uses `#pragma implementation'). Instead, the
- @0 o" j4 \- }8 z; }; Z. |& [5 R object files will contain references to be resolved0 M: I- `% d v5 X' V3 C3 q$ T
at link time.9 }$ a% }, R! K) T: E; {: L1 j# t' [
~6 X8 q0 H# x* e6 S7 a& S+ X: f
#pragma implementation
7 H+ z) c+ C2 Q6 ?* L# e
9 }* Z8 @% u7 l3 h9 D) D+ Z #pragma implementation "objects.h"! {5 ~ S8 |. |3 d
(C++ only.) Use this pragma in a main input file,% Q' o% G# D; x
when you want full output from included header
4 v/ i3 L; E3 N3 F3 v0 s files to be generated (and made globally visible).
; [) z. F! S+ r, a! M The included header file, in turn, should use" T7 X/ Q# i+ ]' m4 ^+ K. I& h' @3 }
`#pragma interface'. Backup copies of inline mem-
+ o) y/ `+ G% g; g c, n ber functions, debugging information, and the in-
1 H+ t. m" E' T8 _+ y5 \/ ~ ternal tables used to implement virtual functions
W7 k2 x+ Z8 o0 A$ r are all generated in implementation files.1 P8 Z' Q9 U" S* t; I. \- W
9 U$ ~# V( \5 Q, ?8 Z. |( f; d
If you use `#pragma implementation' with no argu-! d* E, ^8 X/ I* U
ment, it applies to an include file with the same+ g5 y- @. D+ J. b- j
basename as your source file; for example, in
9 t ^6 M' U5 y$ D% J8 \' ` `allclass.cc', `#pragma implementation' by itself( j" E3 ]( z- i, C5 ^
is equivalent to `#pragma implementation
( e4 U$ U4 |9 b% P "allclass.h"'. Use the string argument if you want
/ V: Z7 L) F- ~1 x9 E+ a. d a single implementation file to include code from
3 p* r+ x# n n6 ^" }- K8 L5 F- t multiple header files.
5 p6 k: l, o1 X- B& C4 O1 J$ j& m5 _9 o# t5 B/ A
There is no way to split up the contents of a sin-* [, t- J, c9 y; d, C7 W
gle header file into multiple implementation files.8 z9 j; @" g# k" {. Z
n2 j5 \ B4 S3 ^, S文件说明9 N1 z$ S9 k* g; e. A
file.c C source file
# D3 H- j1 h! \* { file.h C header (preprocessor) file
! K" [4 m2 v8 i4 s' C8 x9 V. Q. c file.i 经预处理过的 C source file
! l2 e3 z. |6 E8 t file.C C++ source file
# b2 z2 {' t9 L8 u$ T: {3 T file.cc C++ source file2 F/ G+ K, q) D6 R/ `* V2 B
file.cxx C++ source file
0 t! P m: x- E b2 F2 L6 x file.m Objective-C source file: j! o" H$ I8 l- F
file.s assembly language file$ n. p! S# ^4 l5 C5 C3 I. _1 W
file.o object file
+ @. x# _- W7 p/ ?) y a.out link edited output
Y( W5 {& I. p6 ~9 c: n$ ] TMPDIR/cc* temporary files
8 U- t( h2 a# n: E& L8 C3 E8 P LIBDIR/cpp preprocessor3 z, R3 N, d% k- i$ Q2 e8 I
LIBDIR/cc1 compiler for C
6 w ~; I4 C8 d! T) e9 ^+ c) `2 @& e LIBDIR/cc1plus compiler for C++
& D% Z. F N0 S9 e4 h, c9 Q' ], K LIBDIR/collect linker front end needed on some machines
6 x6 U8 {& |: y; J/ I: ~2 k LIBDIR/libgcc.a GCC subroutine library
/ s: U; N: a0 H7 t$ t( F% U /lib/crt[01n].o start-up routine& R2 x- H! C+ `) v, o ?
LIBDIR/ccrt0 additional start-up routine for C++3 y7 c6 \& W, @) [3 ? j( r2 H) m
/lib/libc.a standard C library, 参阅 man page intro(3)- `8 i9 n& B1 [( K/ H$ M, ]6 V
/usr/include standard directory for #include files
0 I1 k8 N3 L5 t8 M LIBDIR/include standard gcc directory for #include files
7 h% f1 d7 k9 }, r. U LIBDIR/g++-include additional g++ directory for #include
1 q/ Z7 z8 @9 q& Q( t
4 u+ e, b: M c2 g9 w+ S% \ LIBDIR is usually /usr/local/lib/machine/version.
+ b: U. X; a; f6 C& g$ j! ~ TMPDIR comes from the environment variable TMPDIR (default+ O5 T$ J% f ?$ t3 ~+ M* `
/usr/tmp if available, else /tmp).
With your idea, Carry out together.