Tuesday, December 20, 2011

Introduction to GNU C Compiler (GCC)

In this post i will show some of the useful built-in functions and command line options, Not all of them but the most important to me and may be to the reader.

Agenda
  • Command line options
    • C dialect options
    • Warning options
    • Debugging options
    • Optimize options
    • Pre-processor options
    • Link options
    • Environment variables
  •  Built-in functions
  • References
Command line options

C dialect options


-ansi
In C mode, this is equivalent to `-std=c90'. In C++ mode, it is equivalent to `-std=c++98'.
This turns off certain features of GCC that are incompatible with ISO C90 (when compiling C code), or of standard C++ (when compiling C++ code), such as the asm and typeof keywords, and predefined macros such as unix and vax that identify the type of system you are using. It also enables the undesirable and rarely used ISO trigraph feature. For the C compiler, it disables recognition of C++ style `//' comments as well as the inline keyword.
The alternate keywords __asm__, __extension__, __inline__ and __typeof__ continue to work despite -ansi. You would not want to use them in an ISO C program, of course, but it is useful to put them in header files that might be included in compilations done with -ansi. Alternate predefined macros such as __unix__ and __vax__ are also available, with or without -ansi.

-fno-asm 
Do not recognize asm, inline or typeof as a keyword, so that code can use these words as identifiers. You can use the keywords __asm__, __inline__ and __typeof__ instead. -ansi implies -fno-asm.
In C++, this switch only affects the typeof keyword, since asm and inline are standard keywords. You may want to use the -fno-gnu-keywords flag instead, which has the same effect.
-fopenmp
Enable handling of OpenMP directives #pragma omp in C/C++ and !$omp in Fortran. When -fopenmp is specified, the compiler generates parallel code according to the OpenMP Application Program Interface v3.0 http://www.openmp.org/.

Warning options

-fsyntax-only
Check the code for syntax errors, but don't do anything beyond that.
-fmax-errors=n
Limits the maximum number of error messages to n, at which point GCC bails out rather than attempting to continue processing the source code. If n is 0 (the default), there is no limit on the number of error messages produced. If -Wfatal-errors is also specified, then -Wfatal-errors takes precedence over this option.
-w
Inhibit all warning messages.
-Werror
Make all warnings into errors. 
-Wfatal-errors 
 This option causes the compiler to abort compilation on the first error occurred rather than trying to keep going and printing further error messages. 
-Wall 
This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros.
-Wunused-function 
Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall.
-Wunused-parameter 
Warn whenever a function parameter is unused aside from its declaration.
-Wunused-label 
Warn whenever a label is declared but not used. This warning is enabled by -Wall.
-Wunused-parameter 
Warn whenever a function parameter is unused aside from its declaration
-Wunused-variable
Warn whenever a local variable or non-constant static variable is unused aside from its declaration. This warning is enabled by -Wall.
-Wunused-value 
Warn whenever a statement computes a result that is explicitly not used. To suppress this warning cast the unused expression to `void'. This includes an expression-statement or the left-hand side of a comma expression that contains no side effects. For example, an expression such as `x[i,j]' will cause a warning, while `x[(void)i,j]' will not. This warning is enabled by -Wall.
-Wunused 
All the above -Wunused options combined.
-Wno-div-by-zero 
Do not warn about compile-time integer division by zero. Floating point division by zero is not warned about, as it can be a legitimate way of obtaining infinities and NaNs.
-Wfloat-equal 
 Warn if floating point values are used in equality comparisons.
-Wtype-limits 
Warn if a comparison is always true or always false due to the limited range of the data type, but do not warn for constant expressions. For example, warn if an unsigned variable is compared against zero with `<' or `>='. This warning is also enabled by -Wextra 
-Wsign-compare 
 Warn when a comparison between signed and unsigned values could produce an incorrect result when the signed value is converted to unsigned. This warning is also enabled by -Wextra; to get the other warnings of -Wextra without this warning, use `-Wextra -Wno-sign-compare'.
-Winline 

Warn if a function can not be inlined and it was declared as inline. Even with this option, the compiler will not warn about failures to inline functions declared in system headers. 
The compiler uses a variety of heuristics to determine whether or not to inline a function. For example, the compiler takes into account the size of the function being inlined and the amount of inlining that has already been done in the current function. Therefore, seemingly insignificant changes in the source program can cause the warnings produced by -Winline to appear or disappear. 

-Wstack-protector 
This option is only active when -fstack-protector is active. It warns about functions that will not be protected against stack smashing.
-Woverlength-strings 
Warn about string constants which are longer than the “minimum maximum” length specified in the C standard. Modern compilers generally allow string constants which are much longer than the standard's minimum limit, but very portable programs should avoid using longer strings.

Debugging options
-g 
Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF 2). GDB can work with this debugging information.
-ggdb
Produce debugging information for use by GDB.
-Q 
Makes the compiler print out each function name as it is compiled, and print some statistics about each pass when it finishes.
-ftime-report 
Makes the compiler print some statistics about the time consumed by each pass when it finishes.
Optimize options

-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.
-fstack-protector 
Emit extra code to check for buffer overflows, such as stack smashing attacks. This is done by adding a guard variable to functions with vulnerable objects. This includes functions that call alloca, and functions with buffers larger than 8 bytes. The guards are initialized when a function is entered and then checked when the function exits. If a guard check fails, an error message is printed and the program exits.
-fstack-protector-all 
Like -fstack-protector except that all functions are protected.

Pre-processor options


-D name 
Predefine name as a macro, with definition 1.
-D name=definition 
The contents of definition are tokenized and processed as if they appeared during translation phase three in a `#define' directive. In particular, the definition will be truncated by embedded newline characters.
-U name 
Cancel any previous definition of name, either built in or provided with a -D option.
-undef 
Do not predefine any system-specific or GCC-specific macros. The standard predefined macros remain defined.
-I dir 
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.
-o file 
Write output to file. This is the same as specifying file as the second non-option argument to cpp. gcc has a different interpretation of a second non-option argument, so you must use -o to specify the output file.
-v 
Verbose mode. Print out GNU CPP's version number at the beginning of execution, and report the final form of the include path.
-version
--version 
Print out GNU CPP's version number. With one dash, proceed to preprocess as normal. With two dashes, exit immediately.
Link options

-llibrary
-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.)
-static 
On systems that support dynamic linking, this prevents linking with the shared libraries. On other systems, this option has no effect.
-shared 
Produce a shared object which can then be linked with other objects to form an executable. Not all systems support this option.
Directory options

COMPILER_PATH 
The value of COMPILER_PATH is a colon-separated list of directories, much like PATH. GCC tries the directories thus specified when searching for subprograms, if it can't find the subprograms using GCC_EXEC_PREFIX.
LIBRARY_PATH 
The value of LIBRARY_PATH is a colon-separated list of directories, much like PATH. When configured as a native compiler, GCC tries the directories thus specified when searching for special linker files, if it can't find them using GCC_EXEC_PREFIX. Linking using GCC also uses these directories when searching for ordinary libraries for the -l option (but directories specified with -Lcome first).
Built-in functions

__builtin_types_compatible_p (type1, type2)
You can use the built-in function __builtin_types_compatible_p to determine whether two types are the same.
This built-in function returns 1 if the unqualified versions of the types type1 and type2 (which are types, not expressions) are compatible, 0 otherwise. The result of this built-in function can be used in integer constant expressions.
double __builtin_huge_val (void)
Returns a positive infinity, if supported by the floating-point format, else DBL_MAX. This function is suitable for implementing the ISO C macro HUGE_VAL.
float __builtin_huge_valf (void)
Similar to __builtin_huge_val, except the return type is float.
long double __builtin_huge_vall (void)
Similar to __builtin_huge_val, except the return type is long double.
 double __builtin_inf (void)
Similar to __builtin_huge_val, except a warning is generated if the target floating-point format does not support infinities.
int __builtin_ctz (unsigned int x)
Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined.
int __builtin_popcount (unsigned int x)
Returns the number of 1-bits in x.

References






No comments:

Post a Comment