Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782132
  • 博文数量: 413
  • 博客积分: 8399
  • 博客等级: 中将
  • 技术积分: 4325
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-09 10:44
文章分类

全部博文(413)

文章存档

2015年(1)

2014年(18)

2013年(39)

2012年(163)

2011年(192)

分类: C/C++

2012-02-23 10:59:32

..1.. man gcc:

NAME
       gcc - GNU project C and C++ compiler

SYNOPSIS
       gcc [-c|-S|-E] [-std=standard]
           [-g] [-pg] [-Olevel]
           [-Wwarn...] [-pedantic]
           [-Idir...] [-Ldir...]
           [-Dmacro[=defn]...] [-Umacro]
           [-foption...] [-mmachine-option...]
           [-o outfile] [@file] infile...

Only the most useful options are listed here; see below for the  remainder. g++ accepts mostly the same options as gcc.

DESCRIPTION
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage.  For example, the -c option says not to run the linker. Then the output consists of object files output by the assembler. Other options are passed on to one stage of processing. 

1)If you only want some of the stages of compilation, you can use -x (or filename suffixes) to tell gcc where to startand one of the options -c, -S, or -E to say where gcc is to stop. Note that some combinations (for example, -x cpp-output -E) instruct gcc to do nothing at all.

-c  
Compile or assemble the source files, but do not link.  The linking stage simply is not done. The ultimate output is in the form of an object file for each source file. By default, the object file name for a source file is made by replacing the suffix .c, .i, .s, etc., with .o. Unrecognized input files, not requiring compilation or assembly, are ignored.

-S  
Stop after the stage of compilation proper; do not assemble. The  output is in the form of an assembler code file for each non-assembler input file specified. By default, the assembler file name for a source file is made by replacing the suffix .c, .i, etc., with .s. Input files that don't require compilation are ignored.

-E  
Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output. Input files which don't require preprocessing are ignored.

-o file 
Place output in file file. This applies regardless to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code. If -o is not specified, the default is to put an executable file in a.out, the object file for source.suffix in source.o, its assembler file in source.s, a precompiled header file insource.suffix.gch, and all preprocessed C source on standard output.
记忆方法:-E -S -c 三个字母合起来是“ESc”正好是我们键盘最左上角的按键。三个字母对应的阶段正好是按照编译的顺序来的:预处理、编译、汇编。很好记忆。)

2)Most of the command line options that you can use with GCC are useful for C programs; when an option is only useful with another language(usually C++), the explanation says so explicitly. If the description for a particular option does not mention a source language, you can use that option with all supported languages.

3)The gcc program accepts options and file names as operands.  Many options have multi-letter names; therefore multiple single-letter options may not be grouped: -dv is very different from -d -v.
(gcc会将编译选项和文件名,当作操作数来处理。许多选项是多个字母组成的名字;因此多个单字母选项不能够组合成一个:-dv 和 -d -v是截然不同的!

4)You can mix options and other arguments.  For the most part, the order you use doesn't matterOrder does matterwhen you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified. Also, the placement of the -l option is significant.
(我们不需要区分编译选项和其它的参数。它们的前后顺序没有区别。但是当一个选项出现多次时,它们的顺序才重要。)

5)Many options have long names starting with -f or with -W---for example, -fmove-loop-invariants, -Wformat and so on. Most of these have both positive and negative forms; the negative form of -ffoo would be -fno-foo. This manual documents only one of these two forms, whichever one is not the default.
许多由多个字母组成的选项以 -f 或者 -W 开头。比如 -ffoo是打开foo选项,而-fno-foo是关闭foo选项;-Wformat是打开format选项,而Wno-format是关闭format选项。)

6)gcc [-c|-S|-E] [-std=standard]
           [-g] [-pg] [-Olevel]
           [-Wwarn...] [-pedantic]
           [-Idir...] [-Ldir...]
           [-Dmacro[=defn]...] [-Umacro]
           [-foption...] [-mmachine-option...]
           [-o outfile] [@file] infile...

1>   -c -S -E我们已经了解;
2>   -std 表示按照那个标准来编译;
比如 -std=-c98, -std=-c90, -std=-c99, -std=gnu90, -std=gnu99, -std=gnu++0x -std=c++0x, -std=c++98
3>   -g  Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF 2).
            GDB can work with this debugging information.
            On most systems that use stabs format, -g enables use of extra debugging information that only GDB can use;
            this extra information makes debugging work better in GDB but will probably make other debuggers crash or
            refuse to read the program. If you want to control for certain whether to generate the extra information, 
            use -gstabs+, -gstabs, -gxcoff+, -gxcoff, or -gvms (see below). 
            (我们一般使用 -gstabs,然后再也gdb.  -g 字母g表示generate 生成,产生调试信息。)
  -ggdb Produce debugging information for use by GDB.  This means to use the most expressive format available
             (DWARF 2, stabs, or the native format if neither of those are supported), including GDB extensions if at all possible.
4>  -pg Generate extra code to write profile information suitable for the analysis program gprof. You must use this
            option when compiling the source files you want data about, and you must also use it when linking.
            (产生信息用于 gprof 程序来分析程序。)

5> -Olevel  (-O后面的数字是优化的水平)
    -O1 Optimize.  Optimizing compilation takes somewhat more time, and a lot more memory for a large function.

            With -O, the compiler tries to reduce code size and execution time, without performing any optimizations that
            take a great deal of compilation time.
    -O2 Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed
           tradeoff. As compared to -O, this option increases both compilation time and the performance of the generated code.
    -O3 Optimize yet more.  -O3 turns on all optimizations specified by -O2 and also turns on the -finline-functions,
          -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload, -ftree-vectorize and -fipa-cp-clone options.

    -O0 Reduce compilation time and make debugging produce the expected results.  This is the default.

    -Os Optimize for size.  -Os enables all -O2 optimizations that do not typically increase code size.  It also 
          performs further optimizations designed to reduce code size.

   -Ofast
           Disregard strict standards compliance.  -Ofast enables all -O3 optimizations.  It also enables optimizations 
           that are not valid for all standard compliant programs.  It turns on -ffast-math.
           If you use multiple -O options, with or without level numbers, the last such option is the one that is effective.
5> -W 用于产生各种警告信息。最常用的就是 -Wall

6> -pedantic [pi'dæntik] adj.迂腐的, 卖弄学问的, 学究式的 
           Issue all the warnings demanded by strict ISO C and ISO C++reject all programs that use forbidden extensions,
           and some other programs that do not follow ISO C and ISO C++.  For ISO C, follows the version of the 
           ISO C standard specified by any -std option used.
7> -I dir (大写的 i 表示 include )
           Add the directory dir to the list of directories to be searched for header files. Directories named by -I are
           searched before the standard system include directories.  If the directory dir is a standard system include
           directory, the option is ignored to ensure that the default search order for system directories and the
           special treatment of system headers are not defeated . If dir begins with "=", then the "=" will be replaced by
           the sysroot prefix; see --sysroot and -isysroot.
8> -Ldir 
           Add directory dir to the list of directories to be searched for -l.
9> -llibrary  (小写的 L 表示 link )
       -l library
           Search the library named library when linking. (The second alternative with the library as a separate argument 
           is only for POSIX compliance and is not recommended.)

           It makes a difference where in the command you write this option; the linker searches and processes libraries and
           object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before
           bar.o. If bar.o refers to functions in z, those functions may not be loaded.

           The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a.  
           The linker then uses this file as if it had been specified precisely by name.

           The directories searched include several standard system directories plus any that you specify with -L.

           Normally the files found this way are library files---archive files whose members are object files. The linker
           handles an archive file by scanning through it for members which define symbols that have so far been
           referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual
           fashion.  The only difference between using an -l option and specifying a file name is that -l surrounds library
           with lib and .a and searches several directories.
注:一般 -L 和 -l 一起配合使用, gcc ... -L/home/digdeep/mylib/  -lmath 表示到目录 /home/digdeep/mylib/目录下面,去链接名字为libmath.a的库文件。

10>   -D name  (D 表示 define )
           Predefine name as a macro, with definition 1.
一般用于debug,在源文件中有类似的宏定义:
#define DEBUG printf(...) 
和类似的代码:
#ifdefine DEGUB 
....
#endif
然后我们在编译的时候指定: gcc ... - D DEBUG ... 的话,可以让 #ifdefine DEGUB 为真!
    -U name  (表示 undef undefine )
           Cancel any previous definition of name, either built in or provided with a -D option.

11> -foption 前面已经说过了,-f用于多字母的选项,比如:
      1'  -fsigned-char  -funsigned-char
      2'  -fpic
           Generate position-independent code (PIC) suitable for use in a shared library, if supported for the target
           machine.  Such code accesses all constant addresses through a global offset table (GOT).  The dynamic loader
           resolves the GOT entries when the program starts (the dynamic loader is not part of GCC; it is part of the
           operating system).  If the GOT size for the linked executable exceeds a machine-specific maximum size, you get
           an error message from the linker indicating that -fpic does not work; in that case, recompile with -fPIC instead.
           (These maximums are 8k on the SPARC and 32k on the m68k and RS/6000.  The 386 has no such limit.)

           Position-independent code requires special support, and therefore works only on certain machines.  For the 386,
           GCC supports PIC for System V but not for the Sun 386i.  Code generated for the IBM RS/6000 is always
           position-independent.

           When this flag is set, the macros "__pic__" and "__PIC__" are defined to 1.

     3' -fPIC
           If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding
           any limit on the size of the global offset table. This option makes a difference on the m68k, PowerPC and SPARC.

           Position-independent code requires special support, and therefore  works only on certain machines.

           When this flag is set, the macros "__pic__" and "__PIC__" are   defined to 2.

          When generating code for shared libraries, -fpic implies  -msmall-data and -fPIC implies -mlarge-data.

12>  -shared
           Produce a shared object which can then be linked with other objects to form an executable.  Not all systems
           support this option.  For predictable results, you must also specify the same set of options that were used to
           generate code (-fpic, -fPIC, or model suboptions) when you specify this option.

=======================================================================================

..2.. man ar:
NAME
       ar - create, modify, and extract from archives.

DESCRIPTION
       The GNU ar program creates, modifies, and extracts from archives.  An archive is a single file holding a collection
       of other files in a structure that makes it possible to retrieve the original individual files (called members of the archive).

       The original files' contents, mode (permissions), timestamp, owner, and group are preserved in the archive, and can
       be restored on extraction.
       ar is considered a binary utility because archives of this sort are most often used as libraries holding commonly
       needed subroutines.

       ar creates an index to the symbols defined in relocatable object modules in the archive when you specify the
       modifier s.  Once created, this index is updated in the archive whenever ar makes a change to its contents (save for
       the q update operation).  An archive with such an index speeds up linking to the library, and allows routines in the
       library to call each other without regard to their placement in the archive.

我们一般用: ar -rsv obj.o obj2.o libobj.a 命令来将 obj.o obj2.o 生成一个静态库 libobj.a 
然后在使用: gcc -L/path/  lobj 来连接静态库文件 libobj.a

动态库的生成:gcc -O -fpic -shared -o obj.so obj.c 命令来将生成一个动态库 obj.so 
然后的链接使用:gcc -o main main.c ./obj.so 来连接动态库文件 obj.so 

=======================================================================================

..3.. man as:
NAME
       AS - the portable GNU assembler.
编译:as -gstabs -o helloworld.o helloworld.s
链接:ld -o helloworld helloworld.o
执行:./helloworld

=======================================================================================

..4.. man objdump:
NAME
       objdump - display information from object files.

反汇编: objdump -S -d helloworld 
-S 表示 source ,即反汇编的同时显示源码;-d 表示 disassemble 即反汇编。

阅读(10005) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~