Reprint from http://blogold.chinaunix.net/u3/94700/showart.php? Id = 2340792
GccA large number of warning options are provided to warn against possible problems in the code. You can use-wall to enable the following warning:
-Waddress-warray-bounds (only with-O2)-WC ++ 0x-compat
-Wchar-subscripts-wimplicit-int-wimplicit-function-Declaration
-Wcomment-wformat-wmain (only for C/objc and unless
-Ffreestanding)-wmissing-braces-wnonnull-wparentheses
-Wpointer-sign-wreorder-wreturn-type-wsequence-point
-Wsign-compare (only in C ++)-wstrict-aliasing-wstrict-overflow = 1
-Wswitch-wtrigraphs-wuninitialized (only with-O1 and above)
-Wunknown-pragmas-wunused-function-wunused-label-wunused-Value
-Wunused-Variable
Unused-function: a static function that is declared but not defined;
Unused-label: declared but not used label;
Unused-parameter: Warning unused function parameters;
Unused-variable: declared but not used local variable;
Unused-value: calculated but not used value;
Format: incorrect use of format character strings in functions such as printf and scanf;
Implicit-int: the type is not specified;
Implicit-function: used before Declaration;
Char-subscripts: Use the char class as the array subscript (because Char may be a signed number );
Missingbraces: the braces do not match;
Parentheses: parentheses do not match;
Return-type: the return value does not match the type of the returned value;
Sequence-point: code that violates the sequence point, such as a [I] = C [I ++];
Switch: If the switch statement lacks default or the switch uses an enumeration variable as the index, the case of a variable is missing;
Strict-aliasing = N: Use n to set the limit on the type of the object to which the pointer variable points. The default value is n = 3. It is valid only when-fstrict-aliasing is set;
Unknow-pragmas: Use the unknown # pragma command;
Uninitialized: The used variable is initialized and only valid when-O2 is used;
The following are warning options that are not activated in-wall:
Cast-align: When the pointer type conversion has more strict memory alignment requirements, a warning is issued;
Sign-compare: When the signed and unsigned types are used for comparison;
Missing-prototypes: When a function does not have a function prototype before use;
Packed: packed is an extension of GCC. It is required to keep no memory alignment between the members of the struct.Empty Room, Sometimes cause memory alignment problems;
Padded: it is also an extension of GCC, so that memory alignment is filled between struct members, which will increase the size of the struct.
Unreachable-code: when no code is executed.
Inline: When the inline function no longer maintains inline (for example, the inline function obtains the address );
Disable-optimization: when the specified optimization cannot be executed (it takes too much time orSystemResources ).
You can use-werror to change all warnings to errors so that compilation is stopped when a warning is generated. It must be used with the specified warning parameter.
Optimization:
By default, GCC provides a set of 5-level optimization options:
-O0: No optimization (default)
-O and-O1: use can reduce the targetTextSize and execution time, but does not significantly increase the Compilation Time. The memory usage during compilation is significantly increased when large programs are compiled.
-O2: including-O1 optimization and added the optimization that does not require compromise between the target file size and execution speed. the compiler does not execute loop expansion and function inline. this option will increase the Compilation Time and execution performance of the target file.
-OS: optimize the target file size, execute all-O2 optimization options that do not increase the size of the target file, and execute optimization options that specifically reduce the size of the target file.
-O3: Enable all-O2 Optimization Options and add-Finline-functions,-funswitch-loops,-fpredictive-commoning, -fgcse-after-Reload and-ftree-vectorize optimization options.
-Option-O1 included in O1 can be safely used with debugging options:
-Fauto-inc-Dec-fcprop-registers-fdce-fdefer-pop-fdelayed-branch
-FDSE-fguess-branch-probability-fif-conversion2-fif-Conversion
-Finline-small-functions-FIPA-pure-const-FIPA-Reference
-Fmerge-constants-fsplit-wide-types-ftree-CCP-ftree-CH
-Ftree-copyrename-ftree-DCE-ftree-Dominator-opts-ftree-DSE
-Ftree-fre-ftree-SRA-ftree-ter-funit-at-a-time
All of the following optimization options need to be added before the name. If you do not need this option, you can use the-fno-prefix.
Defer-Pop: delay to the pop parameter from the function parameter stack only when necessary;
Thread-jumps: Use jump thread optimization to avoid jump to another jump;
Branch-probabilities: branch optimization;
Cprop-registers: Use copy-propagation between registers to pass values;
Guess-branch-Probability: branch prediction;
Omit-frame-pointer: no stack frame is generated when possible;
-O2: The following are the Optimization Options added by-O2 based on-O1:
-Falign-functions-falign-Jumps-falign-loops-falign-labels
-Fcaller-saves-fcrossjumping-fcse-Follow-Jumps-fcse-Skip-Blocks
-Fdelete-Null-pointer-checks-fexpensive-optimizations-fgcse
-Fgcse-lm-foptimize-sibling-CILS-fpeephole2-fregmove
-Freorder-blocks-freorder-functions-frerun-CSE-after-Loop
-Fsched-interblock-fsched-spec-fschedule-insns
-Fschedule-insns2-fstrict-aliasing-fstrict-overflow-ftree-pre
-Ftree-VRP
CPU architecture optimization options, usually-mcpu (will be canceled);-March,-mtune
Debug options:
You can specify the-G option when compiling source code in GCC to generate the target code with debugging information. GCC can provide debugging information for different debuggers on different platforms, by default, the debugging information generated by GCC is used by GDB. You can use-gformat to specify the format of the debugging information to be generated for other debuggers on other platforms. common formats include:
-Ggdb: Generate debugging information dedicated to GDB. The most suitable format (dwarf 2, stabs, etc.) will have some GDB-specific extensions, which may cause other debuggers to fail to run.
-Stabs: it is in stabs format and does not contain GDB extensions. stabs is often used in DBX debugger of BSD system.
-Gcoff: Generate debugging information in coff format, which is commonly used in SDB debuggers under system v;
-Gxcoff: Generate debugging information in xcoff format, which is used for DBX debugger under ibm rs/6000;
-Gdwarf-2: generates debugging information in the dwarf version2 format. It is often used in the DBX debugger on irixx6. GCC uses some features of dwarf version3.
You can add the following levels after the specified debugging format:
For example,-ggdb2, etc., 0 indicates that no debugging information is generated. when using-gdwarf-2, because the earliest format is-gdwarf2 can cause confusion, you need to use an additional-glevel to specify the level of debugging information, other format options can also specify a level.
GCC can use the-P option to specify the generated information for porf.
Common GCC options
Option
Description
-- Help
-- Target-help
The GCC help description is displayed. 'Target-help' is the command line option for displaying the target machine.
-- Version
The GCC version number and copyright information are displayed.
-OOUTFILE
Output to the specified file.
-XLanguage
Specifies the programming language used. Supported languages include: C ++ javaser none. 'None' indicates that the default behavior is restored, that is, the source file language is guessed Based on the file extension.
-V
Print more information and display the program called by the compiler.
-###
Similar to-V, the option is enclosed in quotation marks and the command is not executed.
-E
Only for preprocessing, without compilation, compilation, and linking. As shown in.
-S
Only compiled to the assembly language, without assembly and link. As shown in.
-C
Compile and assemble the code to the target without link. As shown in.
-Pipe
Use MPs queues instead of temporary files.
-Combine
Multiple source files are passed to the assembler at a time.
3. Other GCC options
More useful GCC options:
Command
Description
-LLibrary
-LLibrary
Search for the database named library when linking.
Example: $ GCC test. C-lm-o Test
-IDir
SetDirAdd to the path list of the search header file.
Example: $ GCC test. C-I ../INC-o Test
-LDir
SetDirAdd to the path list of the Search Library file.
Example: $ gcc-I/home/foo-L/home/foo-ltest test. C-o Test
-DName
Predefine a nameNameMacro. The value is 1.
Example: $ gcc-dtest_config test. C-o Test
-DName=Definition
Predefined nameName, Value:Definition.
-Ggdb
-GgdbLevel
Generate debugging information for the Debugger GDB.LevelIt can be 1, 2, 3, and the default value is 2.
-G
-GLevel
Generate debugging information in the local operating system format. -G is not the same as-ggdb.-G generates information other than GDB.LevelThe value is the same as the preceding one.
-S
Removes the symbol table and relocation information from the executable file. Used to reduce the size of executable files.
-M
Tells the Preprocessor to output a rule suitable for make to describe the dependencies of each target file. For each source file, the pre-processor outputs a make rule. The target item (target) of the rule is the target file name corresponding to the source file, and the dependency item (dependency) is all files referenced by '# include' in the source file. The generated rule can be a single row, but if it is too long, use '\'-linefeed to continue multiple rows. The rules are displayed in standard output and no pre-processed C Programs are generated.
-C
Tell the Preprocessor not to discard comments. Use it with the '-e' option.
-P
Tell the Preprocessor not to generate the '# line' command. Use it with the '-e' option.
-Static
In a system that supports dynamic connections, the shared library cannot be connected. This option is invalid on other systems.
-Nostdlib
The system standard Startup File and standard library file are not connected. Only the specified file is passed to the connector.
Warnings
-Wall
Some useful warning options are opened. We recommend that you add this option during compilation.
-W
-Wextra
Print some additional warning information.
-W
Do Not Display All warning information.
-Wshadow
A warning is given when a local variable overrides another local variable or global variable. It is recommended to enable this option. -Wall does not open this option.
-Wpointer-Arith
A warning is given when arithmetic operations are performed on function pointers or void * pointers. It is also useful. -Wall does not open this option.
-Wcast-Qual
A warning is given when the type modifier is discarded during forced conversion. -Wall does not open this option.
-Waggregate-return
If a function that defines or calls a function that returns a struct or consortium, the compiler sends a warning.
-Winline
Whether Declared as inline or specified-Finline-functions options, the compiler will issue a warning if a function cannot be inline. This is a useful option if your code contains many inline functions.
-Werror
The warning is treated as an error. If any warning occurs, the compilation will be abandoned.
-Wunreachable-code
If the compiler detects code that will never be executed, a warning is given. It is also a useful option.
-Wcast-align
Once a forced conversion of pointer types leads to an increase in the address alignment required by the target, the compiler issues a warning.
-Wundef
When an undefined symbol appears in # If, a warning is given.
-Wredundant-decls
If a definition is declared multiple times in the same visible domain, the compiler sends a warning even if these repeated declarations are valid and there is no difference.
Optimization
-O0
The compiler is prohibited from optimization. This item is used by default.
-O
-O1
Try to optimize the Compilation Time and executable file size.
-O2
More optimizations will try almost all of the optimization functions, but will not be optimized by "space for time.
-O3
On the basis of-O2, open some optimization options:-Finline-functions,-funswitch-loops and-fgcse-after-reload.
-OS
Optimize the size of the generated file. It will open all options opened by-O2, except for those that increase the file size.
-Finline-Functions
Concatenates all simple functions into the caller. The compiler will explore to determine which functions are simple enough and it is worth doing this inline.
-Fstrict-aliasing
Apply the most powerful alias rule (aliasing rules ).
Standard
-ANSI
Supports ANSI-compliant C Programs. In this way, some features that are not compatible with ansi c in gnu c will be disabled.
-STD = c89
-Iso9899: 1990
Indicates that the program is compiled using the standard ISO C90 as the standard.
-STD = c99
-STD = iso9899: 1999
Indicates that the program is compiled using standard ISO c99 as the standard.
-STD = C ++ 98
Indicates that the program is compiled using standard C ++ 98 as the standard.
-STD = gnu9x
-STD = gnu99
Use ISO c99 with GNU extensions.
-Fno-ASM
Do not use ASM, inline, or typeof as keywords, so these words can be used as identifiers. Replace them with _ ASM __, _ inline _, and _ typeof. '-ANSI' implicitly declares '-fno-ASM '.
Fgnu89-inline
Tells the compiler to use the traditional GNU syntax when seeing the inline function in c99 mode.
C options
-Fsigned-Char
-Funsigned-Char
Define char as A/non-Signed type, just like signed Char/unsigned char.
-Traditional
Try to support some aspects of the traditional C compiler. For details, see the gnu c manual.
-Fno-builtin
-Fno-builtin-Function
Functions without the prefix of _ builtin _ are not accepted as built-in functions.
-Trigraphs
Supports the trigraphs of ansi c ). The '-ANSI' option implicitly declares this option.
-Fsigned-bitfields
-Funsigned-bitfields
If the 'signe' or 'unsigne' modifier is not explicitly declared, these options are used to define a signed or non-Signed domain. By default, bit fields are signed because they inherit the basic integer type, such as int, which is a signed number.
-Wstrict-prototypes
If the parameter type is not specified in the declaration or definition of a function, the compiler issues a warning. A useful warning.
-Wmissing-prototypes
If a global function is defined without prior declaration, the compiler issues a warning. This warning is generated even if the function definition itself provides the function prototype. The purpose of this option is to check the global functions that are not declared in the header file.
-Wnested-Externs
If an extern declaration appears inside the function, the compiler issues a warning.
C ++ options
-Ffor-Scope
You can also execute programs from the beginning and redirect them.
-Fno-rtti
Disable the support for dynamic_cast and typeid. If you do not need these functions, disabling them will save some space.
-Wctor-dtor-Privacy
A warning is given when a class is unavailable. The constructor and destructor are considered private.
-Wnon-Virtual-dtor
When a class has polymorphism and no virtual destructor, a warning is issued. -Wall will enable this option.
-Wreorder
If the initialization sequence of the member variables in the Code is inconsistent with the actual execution sequence, a warning is given.
-Wno-deprecated
Do not give a warning when using outdated features.
-Woverloaded-virtual
If the function declaration hides the virtual function of the base class, a warning is given.
Machine dependent options (Intel)
-Mtune =CPU-type
Generates code for the specified type of CPU.CPU-typeIt can be i386, i133, i586, Pentium, i686, pentium4, and so on.
-MSSE
-Msse2
-Mmmx
-MnO-SSE
Mno-sse2
-MnO-MMX
Use or not use the MMX, SSE, and sse2 commands.
-M32
-M64
Generate code on 32-bit/64-bit machines.
-Mpush-ARGs
-MnO-push-ARGs
(No) use the push command to store parameters. It is used by default.
-Mregparm =Num
When an integer parameter is passed, the number of registers used is controlled.