Friday, March 14, 2008

gcc常用参数和环境变量

Command-Line Options

命令行参数

-c

Preprocess, compile, and assemble only (i.e., don't link).
预处理(生成.i,用完删除)、编译、(生成.s,用完删除),汇编(生成.o),不连接(不生成可执行文件)。Btw,貌似很多地方可以用-pipe直接通过管道,不生成临时文件,加快编译

-C
Leave comments in when preprocessing.
预处理时不去除注释(结合其他会留下预处理输出的参数使用,如-E)

-D name[= definition]
Defines the symbol name.
定义一个标识符(相当于程序里面写#define name[= definition],可以结合#ifndef name实现控制功能)

-e name
Start program execution at name.
(对一个Freestanding的程序,比如自己写的OS)自定义入口函数(常和-ffreestanding -nostartfiles –nostdlib等一起使用)

-E
Preprocess only; output to stdout, unless used with -o.
只预处理,结果输出到标准输出,除非用-o指定输出文件(一般为*.i)

-ffast-math
Permit faster floating-point arithmetic methods at the cost of accuracy or precision.
浮点数运算的时候以精度换速度(第一个f是flag的意思。C99使用比以前更好数值精确度,当然我们也可以不用。。这个参数定义_ _FAST_MATH_ _这个宏,相当于后面要说的6个浮点相关运算参数的并集,which对我来说长得相当古怪!)

-ffinite-math-only
Disregard infinities and NaN ("not a number") values.浮点相关运算参数。我没看懂“Disregard”到底是指丢弃,还是说不检查就接收。。

-ffreestanding
Compile as a freestanding (not hosted) program.
编译一个独立的程序(比如不会去连接crt0.o,“which contains the actual entry point of the executable program”,crt = C Runtime)

-finline-functions, -fno-inline-functions
Enable/disable inline functions.
允许/不允许内联函数

-fno-math-errno
Disable the errno variable for simple math functions.
浮点相关运算参数。浮点数学函数中出错了不使用errno这个全局变量?

-fno-trapping-math
Generate "nonstop" floating-point code.
浮 点相关运算参数。“Generates "nonstop" code, on the assumption that no math exceptions will be raised that can be handled by the user program.”假设没有数学操作产生的异常是用户能处理的,非常拗口。。用国语说就是浮点运算就不产生该有的异常了,反正用户也处理不了

-frounding-math
D
on't disregard the rounding-mode features of the floating-point environment (experimental).
浮点相关运算参数-fno-rounding-math的opposite。

-fsignaling-nans
Allow all exceptions raised by signaling NaNs (experimental).
浮点相关运算参数-fno-signaling-nans的opposite。

-fsyntax-only
Don't compile or link; just test input for syntax.
只测试输入语法有效性

-funroll-loops, -fno-unroll-loops
Enable/disable loop optimization.
对循环做优化(比如把一些小循环的跳转改成线性的代码,貌似会使文件增大?)

-funsafe-math-optimizations
Permit optimizations that don't conform to standards and/or don't verify values.
浮点相关运算参数。看名字吧。。。

-fverbose-asm
Include C variable names as comments in assembly language.
在生成的的汇编语言里面将C变量名加到注释里面(结合会留下汇编输出的参数使用,如-S)

-g[ format]
Compile for debugging.
生成包含特定format调试信息的文件(比如-ggdb,结合GDB来调试。当然debug的会比release的大很多)

-I directory[: directory[...]]
Search for "include" files in the specified path.
指定#include时的查找路径,多个路径用冒号隔开
Quote:“The usual search order for include directories is:
1. The directory containing the given source file (for filenames in given in quotation marks in an #include directive).
2. Directories specified by -I options, in command-line order.
3. Directories specified in the environment variables C_INCLUDE_PATH and CPATH.
4. The system's default include directories.”

-I-
Distinguish between -Ipath for #include and -Ipath for #include "file".
Quote:“This option divides any -Idirectory options on the command line into two groups. All directories appended to an -I option to the left of -I- are not searched for header files named in angle brackets in an #include directive, such as this one:#include

Instead, they are searched only for header files named in quotation marks in the #include directive, thus:
#include "myheader.h"

The second group consists of any directories named in an -I option to the right of -I-. These directories are searched for header files named in any #include directive.

Furthermore, if -I- appears on the command line, then the directory containing the source file is no longer automatically searched first for header files.”

-lbasename
Link with library libbasename.so or libbasename.a.
连接以libbasename为名的shared object或者archive

-L directory[: directory[...]]
Search for library files in the specified path.
指定连接时的查找路径,多个路径用冒号隔开

-march= cpu
Intel x86: Generate model-specific code.
为某个平台优化,第一个m意思是machine或者model。对于x86体系来说,-mcpu和-mtune是一样,-march可以为不同cpu产生优化的代码,比如-march=athlon-4(具体的cpu abbr.看gcc manual)

-mcpu= cpu
Sparc, ARM, and RS/6000-PowerPC: Generate model-specific code.
Intel x86: Optimize scheduling for the specified CPU model.
为某个平台优化,对于x86和非x86体系来说,-m系列参数意义有点不一样,见前面-march
Quote:“For several processor types, such as the Sparc, ARM, and RS/6000-PowerPC series, the option -mcpu=cpu generates machine code for the specific CPU type's register set, instruction set, and scheduling behavior. Programs compiled with this option may not run at all on a different model in the same CPU family.”

-mtune= cpu
Optimize scheduling for the specified CPU model.
为某个平台优化,这个比-mcpu要“温和”一点。
Quote:“The option -mtune=cpu is more tolerant. Code generated with -mtune=cpu uses optimized scheduling parameters for the given CPU model, but adheres to the family's common instructions and registers, so that it should still run on a related model.”

-nostartfiles
Don't link startup code.
Quote:“On most systems, GCC also links programs by default with initialization routines in object files named crtbegin.o and crtend.o.”

-nostdlib
Don't link with the standard library.
不连接C Stanndard Library

-o file
Direct output to the specified file.
指定输出文件名。

-O0
T
urn off all optimization options.

不优化。


-O, -O1

Perform some optimization without taking much time.
优化

-O2
Perform more optimization, including data flow analysis.
再优化

-O3
Perform still more optimization, including inline function compilation.
优化到底

-Os
Optimize for size.
只在文件大小方面做优化

-p
Link in code to output profiling information.
“The -p option adds special functions to your program to output profiling information when you run it. The profiling output is saved in a file called mon.out.”

-pedantic
Output warnings on nonstandard usage.
为非标准用法产生Warning(比如ANSI C不包括但是GUN C扩展的地方,见-std)

-pedantic-errors
Fail on nonstandard usage.
这个比较狠,编写portable代码应该用上

-pg
Link in code to output profiling information for gprof.
为GUN Profiler,gprof输出profiling information,这次输出文件叫gmon.out

-s
Strip symbol tables from executable file.
优化参数。在执行文件中去除符号表
Quote:“This makes the finished program file significantly smaller, and is often used in building a production version.

-S
Preprocess and translate into assembly language only
预处理,编译,到生成*.s为止,不汇编。

-save-temps
Save intermediate output files.
留下所有编译过程中产生的文件:.i、.s、.o

-shared
Create a shared object file for dynamic linking.
生成.so文件

-static
Don't link to shared object files.
只静态连接,就是说生成的文件不会去连接.so文件

-std=iso9899:1990
-std=c89
-ansi
Support ISO/IEC 9899:1990.
都是一样的。支持ANSI C标准,与标准冲突的扩展被disable(比如GUN的tyepeof操作符,好东西啊。。)

-std=iso9899:199409
Support ISO/IEC 9899:1989 and AMD1.
另一个标准

-std=c99
Support ISO/IEC 9899:1999.

-std=gnu89
Like -ansi, plus GNU extensions (default).
默认-std选项

-std=gnu99
Like -std=c99, plus GNU extensions.

-traditional
Support old-style C. Deprecated; supported only with -E.
注意:“supported only with –E”,只在预处理的时候有用

-trigraphs
Support ISO C trigraphs.

Trigraph

Equivalent

??(

[

??)

]

??<

{

??>

}

??=

#

??/

\

??!

|

??'

^

??-

~


-U name
"Undefine" the symbol name.
和-D对应的

-v
Be verbose: print the options applied at each step of compiling.

--version
Output GCC version and license information.

-w
Disable all warnings.

-Wa, option[, option[...]]
Pass options to assembler command line.

-Wall
Output warnings about a broad range of problems in source code.

-Wl, option[, option[...]]
Pass options to linker command line.
给连接器ld传命令行参数

-Werror
Fail on all warnings.
这个比-pedantic-errors更狠

-Wextra
Output warnings about legal but questionable usage.
这个貌似是最verbose的warning了

-Wtraditional
Warn about differences to old-style C.

-fmerge-constants
Put identical constants in a single location.
优化参数,再补一句:“even across different source files”

-x filetype
Treat subsequent files as being of the specified type.
这 里filetype的选项有“c, c-header, cpp-output, assembler (meaning that the file contains assembly language), assembler-with-cpp, or none.”所有出现在-x之前的文件被当作-x指定的filetype,常用在没用标准扩展名的时候

假设当前目录下有这些源文件:[main.c func.c func.h],其中main.c要调用func.c中的函数。

【1】生成静态库:
$ gcc -c func.c -o func.o
$ ar r libfunc.a func.o
$ gcc main.c -o main -static -L. -lfunc
$ ./main

【2】生成动态库:
$ gcc -fpic -c func.c -o func.o
$ gcc -shared -o libfunc.so.1.0.0 func.o
$ ln -s libfunc.so.1.0.0 libfunc.so
$ gcc main.c -o main -L. -lfunc
$ export LD_LIBRARY_PATH=$(pwd)
$ ./main
如果将so文件copy到系统lib目录(如/usr/lib),则最后2步就不用了。

3个小知识:
【1】nm命令:列出目标文件或2进制文件的所有符号。
【2】ldd命令:列出为了使程序正常运行所需要的所有共享库。
【3】/etc/ld.so.conf文件:除了标准目录(/lib和/usr/lib)之外,链接器和加载器搜索共享库时要检查的其他目录,和这个文件相关的一个命令是:ldconfig 。
【4】Gcc选项 -aux-info可以导出被编译源文件所包含的所有函数(或方法)的原型。如:gcc component.c -aux-info prototype.h




Environment Variables
环境变量

CPATH, C_INCLUDE_PATH
Colon-separated list of directories to search for header files, after those indicated by -Idirectory on the command line.
和-I一起,#include的时候用,冒号分隔

COMPILER_PATH
Colon-separated list of directories to search for GCC's own subprogram files.

GCC_EXEC_PREFIX
A prefix for GCC to add to the names of its subprograms when invoking them. May end with a slash.

LIBRARY_PATH
Colon-separated list of directories to search for linker and library files, after directories specified by -Ldirectory on the command line.
和-L一起,连接library的时候用

LD_LIBRARY_PATH
Colon-separated list of directories to search for shared library files. Read not by GCC, but by executables dynamically linked against shared libraries.
找.so的路径,比如/usr/lib/、%SystemRoot%\system32\之类的

TMPDIR
Directory to use for temporary files.
临时文件路径,比如/tmp

No comments:

Post a Comment