GCC 命令行选项( -nodefaultlibs -nostartfiles -nostdlib)



GCC 技术参考大全 附录D 命令行选项 目录 前言  

Linker

-nodefaultlibs

标准系统库例程不应该作为被链接的可执行程序的一部分包含进来。用到的库仅仅是在命令行中所列出的那些库。

编 译程序可能会产生对系统函数 memcpy() , memcmp() , memset() (对标准 C 和 System V 而言), bcopy() 和 bzero() (对 BSD 而言)的调用。这些引用通常都可在 libc.a 中进行解析,因此需要在命令行中亲自提供这些例程。

库 libgcc.a 包含了一系列目标平台相关的特殊例程,可以将它们视为编译程序的一部分,因此在没有标准库时应该指定 -lgcc 。

参见选项 -nostartfiles 和 -nostdlib 。

Linker

-nostartfiles

标准启动目标文件不应该作为链接的可执行程序的一部分被包含。参见选项 -nostdlib 和 -nodefaultlibs 。

Pre Ada

-nostdinc

禁止编译程序在标准系统目录中搜索头文件。搜索的仅仅是当前目录和选项 -I 中指定的目录。

对 Ada 来说,该选项指定了源文件不使用系统库。

该选项也可以写作 –no-standard-includes 。

Pre C++

-nostdinc++

禁止编译程序在标准 C++ 目录中搜索头文件,但可以在其他标准目录中搜索。该选项专供编译 C++ 库使用。

Linker

-nostdlib

只有命令行指定的项才传递给链接器。标准启动文件和库都不会传递给链接器。该选项隐式打开选项 -nostartfiles 和 -nodefaultlibs 。

该选项也可以写作 –no-standard-libraries 。

-O level

指定用于编译程序所生成的代码的优化级别。注意,在代码尺寸和执行速度间永远都需要权衡。默认选项是 -O0 ,即不进行优化。

如 果没有指定优化级别,编译程序产生与输入源结构匹配的代码。优化不仅需要更多的处理,还需要更多的内存。没有优化的编译具有两个好处,可以缩短编译时间 (优化则需要很长的时间),而且可以在调试器中轻松跟踪所产生的代码。对软件开发过程而言,所有这些都是理想的条件。也可以使用调试器调试已优化的代码, 但由于输出的代码可能被重组,因此很难进行跟踪。

该选项也可以写作 –optimize 。

优化级别参见表 D-4 。

表 D-4 优化的 6 种级别

级别

描述

-O

编 译程序尝试减少代码尺寸和执行时间 , 但不会进行造成调试困难的修改。该级别会打开选项 -fno_optimize_size , -fdefer_pop , -fthread_jumps , -jguess_branch_prob , -cprop-registers 和 -fdelayed_branch 。当调试器在某平台上无需栈指针就可以正常工作时才会设置选项 -fomit-frame-pointer

-O0

这是默认选项。关闭所有优化。关闭所有尺寸优化并设置选项 -fno-merge-constants

-O1

与选项 -O 相同

-O2

该 优化级打开不涉及代码尺寸和运行速度权衡的所有优化。除了选项 -O 所打开的选项外,该优化级还打开选项 -foptimize-sibling-calls , -fcse-follow-jumps , -fcse-skip-blocks , -fgcse , -fexpensive-optimizations , -fstrength-reduce , -frerun-cse-after-loop , -frerun-loop-opt , -fcaller-saves , -fforce-mem , -fpeephole2 , -fschedule-insns , -fschedule-insn-after-reload , -fregmove , -fstruct-aliasing , -fdelete-null-pointer-checks 和 -freorder-blocks 。该优化级不进行循环展开、函数内嵌或是寄存器重命名

-O3

除了 -O2 所打开的选项外,该优化级还打开选项 -finline-functions 和 -frename-registers

-Os

优化代码尺寸。打开 -O2 中打开的所有选项。将选项 -falign-loops , -falign-jumps , -falign-labels 和 -falign-functions 设置为 1 ,它将禁止任何为对齐而插入的空间

-o filename

将输出写入指定的文件中。该选项对各种输出均有效,可以是预处理过的源代码、汇编语言、目标文件或是已链接的可执行程序。由于只能指定一个输出文件,因此如果可能产生多个文件,就不要使用 -o 选项。

如果编译程序生成一个要链接的可执行程序,而且没有用 -o 选项指定其名字,默认名是 a.out 。

该选项也可以写作 –output 。

–optimize level

与选项 -O 相同。

–output filename

与选项 -o 相同。

Java

–output-class-directory= directory

与选项 -foutput-class-dir 相同。

-p

包含额外的代码,这些代码将输出适合性能分析程序 prof 分析的信息。该选项必须在编译源文件和链接目标文件的同时使用。

参见选项 -pg 。该选项也可以写作 –profile 。

Pre

-P

当同选项 -E 一起使用时,预处理程序不产生 #line 指示字。

该选项也可以写作 –no-line-commands 。

–param name = value

GCC 内部存在一些优化代码程度的限制,因此调整这些值就是调整整个优化。表 D-5 给出了参数的名字和可用的数值。

该选项也可以写作 –param 。

表 D-5 –params 选项可接受的名字

名字

max-delay-slot-insn-search

填充一个延迟段时所选择的最大数目的指令。较大的数值可以改进所生成的代码,但会降低编译速度。默认值是 100

max-delay-slot-live-search

当查找具有合法寄存器信息的代码块时的最大代码块搜索数目。较大的数值可以改进生成的代码,但会降低编译速度。默认值是 333

max-gcse-memory

可用来分配执行 GCSE (全局命令子表达式消除)的最大的内存量。如果没有足够的内存执行该操作,优化将无法进行。默认值是 50MB ( 52428800 )

max-gcse-passes

GCSE (全局命令子表达式消除)的最大迭代次数。默认值是 1

max-inline-insns

在可以被扩展内嵌的方法中的最大指令数目。默认值是 600

max-pending-list-length

在跟踪机制重置槽位列表并重新开始前,可以由槽位调度器在一个等待依赖列表中排序的最大的分支元素数目。一个大函数可能产生成千上万的依赖关系。默认值是 32

    上一页 下一页 回专题首页

       -Wl,option
           Pass option as an option to the linker. If option contains commas,
           it is split into multiple options at the commas.

       -u symbol
           Pretend the symbol symbol is undefined, to force linking of library
           modules to define it. You can use -u multiple times with different
           symbols to force loading of additional library modules.

       -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 pro-
           gram 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 16k on the m88k,
           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.

       -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.

       -MD -MD is equivalent to -M -MF file, except that -E is not implied.
           The driver determines file based on whether an -o option is given.
           If it is, the driver uses its argument but with a suffix of .d,
           otherwise it take the basename of the input file and applies a .d
           suffix.

           If -MD is used in conjunction with -E, any -o switch is understood
           to specify the dependency output file (but @pxref{-MF}), but if
           used without -E, each -o is understood to specify a target object
           file.

           Since -E is not implied, -MD can be used to generate a dependency
           output file as a side-effect of the compilation process.



       -fno-builtin
       -fno-builtin-function
           Don’t recognize built-in functions that do not begin with builtin_ as prefix.

           GCC normally generates special code to handle certain built-in functions more efficiently; for instance, calls to
           "alloca" may become single instructions that adjust the stack directly, and calls to "memcpy" may become inline
           copy loops. The resulting code is often both smaller and faster, but since the function calls no longer appear as
           such, you cannot set a breakpoint on those calls, nor can you change the behavior of the functions by linking with
           a different library. In addition, when a function is recognized as a built-in function, GCC may use information
           about that function to warn about problems with calls to that function, or to generate more efficient code, even if
           the resulting code still contains calls to that function. For example, warnings are given with -Wformat for bad
           calls to "printf", when "printf" is built in, and "strlen" is known not to modify global memory.

           With the -fno-builtin-function option only the built-in function function is disabled. function must not begin
           with
builtin_. If a function is named this is not built-in in this version of GCC, this option is ignored.
           There is no corresponding -fbuiltin-function option; if you wish to enable built-in functions selectively when
           using -fno-builtin or -ffreestanding, you may define macros such as:
          
                   #define abs(n)          builtin_abs ((n))
                   #define strcpy(d, s)   
builtin_strcpy ((d), (s))

       -ffreestanding
           Assert that compilation takes place in a freestanding environment. This implies -fno-builtin. A freestanding
           environment is one in which the standard library may not exist, and program startup may not necessarily be at
           "main". The most obvious example is an OS kernel. This is equivalent to -fno-hosted.

       -fstrict-aliasing
           Allows the compiler to assume the strictest aliasing rules applicable to the language being compiled. For C (and
           C++), this activates optimizations based on the type of expressions. In particular, an object of one type is
           assumed never to reside at the same address as an object of a different type, unless the types are almost the same.
           For example, an "unsigned int" can alias an "int", but not a "void" or a "double". A character type may alias any
           other type.

           Pay special attention to code like this:

                   union a_union {
                     int i;
                     double d;
                   };

                   int f() {
                     a_union t;
                     t.d = 3.0;
                     return t.i;
                   }

           The practice of reading from a different union member than the one most recently written to (called "type-punning")
           is common. Even with -fstrict-aliasing, type-punning is allowed, provided the memory is accessed through the union
           type. So, the code above will work as expected. However, this code might not:

                   int f() {
                     a_union t;
                     int
ip;
                     t.d = 3.0;
                     ip = &t.i;
                     return *ip;
                   }

          Enabled at levels -O2, -O3, -Os.

       -mkernel
           Enable kernel development mode. The -mkernel option sets -static, -fno-common, -fno-cxa-atexit, -fno-exceptions,
           -fno-non-call-exceptions, -fapple-kext, -fno-weak and -fno-rtti where applicable. This mode also sets
           -mno-altivec, -msoft-float, -fno-builtin and -mlong-branch for PowerPC targets.


=========================>>>> Options Controlling CPP <<<<=============================

       -include file
           Process file as if "#include "file"" appeared as the first line of the primary source file. However, the first
           directory searched for file is the preprocessor’s working directory instead of the directory containing the main
           source file. If not found there, it is searched for in the remainder of the "#include "…"" search chain as
           normal.
          
           If multiple -include options are given, the files are included in the order they appear on the command line.
          
       -imacros file
           Exactly like -include, except that any output produced by scanning file is thrown away. Macros it defines remain
           defined. This allows you to acquire all the macros from a header without also processing its declarations.

           All files specified by -imacros are processed before all files specified by -include.

       -idirafter dir
           Search dir for header files, but do it after all directories specified with -I and the standard system directories
           have been exhausted. dir is treated as a system include directory. If dir begins with "=", then the "=" will be
           replaced by the sysroot prefix; see –sysroot and -isysroot.

       -iprefix prefix
           Specify prefix as the prefix for subsequent -iwithprefix options. If the prefix represents a directory, you should
           include the final /.

       -iwithprefix dir
       -iwithprefixbefore dir
           Append dir to the prefix specified previously with -iprefix, and add the resulting directory to the include search
           path. -iwithprefixbefore puts it in the same place -I would; -iwithprefix puts it where -idirafter would.


=========================>>>> Options Controlling C++ Dialect <<<<=============================

      

       -fabi-version=n
           Use version n of the C++ ABI. Version 2 is the version of the C++ ABI that first appeared in G++ 3.4. Version 1
           is the version of the C++ ABI that first appeared in G++ 3.2. Version 0 will always be the version that conforms
           most closely to the C++ ABI specification. Therefore, the ABI obtained using version 0 will change as ABI bugs are
           fixed.

           The default is version 2.

       -fno-enforce-eh-specs
           Don’t generate code to check for violation of exception specifications at runtime. This option violates the C++
           standard, but may be useful for reducing code size in production builds, much like defining NDEBUG. This does not
           give user code permission to throw exceptions in violation of the exception specifications; the compiler will still
           optimize based on the specifications, so throwing an unexpected exception will result in undefined behavior.

       -ffor-scope
       -fno-for-scope
           If -ffor-scope is specified, the scope of variables declared in a for-init-statement is limited to the for loop
           itself, as specified by the C++ standard. If -fno-for-scope is specified, the scope of variables declared in a
           for-init-statement extends to the end of the enclosing scope, as was the case in old versions of G++, and other
           (traditional) implementations of C++.

           The default if neither flag is given to follow the standard, but to allow and give a warning for old-style code
           that would otherwise be invalid, or have different behavior.

       -fno-weak
           Do not use weak symbol support, even if it is provided by the linker. By default, G++ will use weak symbols if
           they are available. This option exists only for testing, and should not be used by end-users; it will result in
           inferior code and has no benefits. This option may be removed in a future release of G++.

       -nostdinc++
           Do not search for header files in the standard directories specific to C++, but do still search the other standard
           directories. (This option is used when building the C++ library.)

       -pedantic
           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.

           Valid ISO C and ISO C++ programs should compile properly with or without this option (though a rare few will
           require -ansi or a -std option specifying the required version of ISO C). However, without this option, certain
           GNU extensions and traditional C and C++ features are supported as well. With this option, they are rejected.

       -Wformat
           Check calls to "printf" and "scanf", etc., to make sure that the arguments supplied have types appropriate to the
           format string specified, and that the conversions specified in the format string make sense. This includes
           standard functions, and others specified by format attributes, in the "printf", "scanf", "strftime" and "strfmon"
           (an X/Open extension, not in the C standard) families (or other target-specific families). Which functions are
           checked without format attributes having been specified depends on the standard version selected, and such checks
           of functions without the attribute specified are disabled by -ffreestanding or -fno-builtin.