GCC parameter Interpretation (finishing)

Source: Internet
Author: User

gcc parameter explanation

GCC and g++ are GNU's C &c++ compilers respectively.

A total of 4 steps are required for gcc/g++ to perform compilation work:

1, preprocessing, generate. i file [preprocessor CPP]

2, the preprocessed file will not be converted into assembly language, generate files. s[compiler Egcs]

3, have the assembly into the target Code (machine code) to generate. o file [assembler as]

4, connect the target code, generate executable program [linker LD]


[Detailed parameters]
-X language filename
Sets the language in which the file is used, invalidates the suffix name, and is valid for more than one later. That is, according to the Convention C language suffix name is. c, and C + + suffix name is. C or. cpp, if you decide that the suffix name of your C code file is. Pig, you use this parameter, which works on the file name that follows him, only to the use of the next parameter.
The parameters that can be used are the following
' C ', ' objective-c ', ' c-header ', ' C + + ', ' cpp-output ', ' assembler ', ' assembler-with-cpp '.
See English, you should be able to understand.
Example usage:
Gcc-x Chello.pig

-X None filename
Turn off the last option, which means that GCC automatically recognizes the file type according to the filename suffix
Example usage:
Gcc-x c Hello.pig-x None hello2.c

-C
Only activate preprocessing, compilation, and assembly, that is, he only makes the program into obj files.
Example usage:
Gcc-chello.c
He will generate. O's obj file


-S
Activating preprocessing and compiling only means compiling the file into assembly code.
Example usage:
Gcc-shello.c
He will generate the assembly code for. S, and you can use a text editor to see


-E
Activate preprocessing only, do not generate file, if you want to get output file you need to redirect it to an output file.

Example usage:
GCC-EHELLO.C > Pianoapan.txt
gcc-ehello.c | More


-O
Specifies the name of the generated executable file, by default, GCC generates the a.out executable file.
Example usage:
Gcc-ohello.exe hello.c (Oh, Windows used to)
Gcc-ohello.asm-s hello.c


-pipe
Using pipelines instead of compiled temporary files may be problematic when using non-GNU assembler tools.

Example usage:
Gcc-pipe-o Hello hello.c


-ansi
Turn off the features in GNU C that are incompatible with ANSI C, and activate the proprietary features of ANSI C (including the prohibition of some ASM, inline, typeof keywords, and Unix,vax, and other preprocessing macros.)


-fno-asm
This option implements part of the functionality of the ANSI option, which prohibits the use of ASM, inline, and typeof as keywords.

-fno-strict-prototype
Only works on g++, using this option, g++ the function with no arguments is not an explicit description of the number and type of arguments, not without arguments. and GCC, regardless of whether or not this argument is used, a function with no parameters will be a type that is not explicitly described

-fthis-is-varialble
is to the traditional C + +, you can use this when the general variable use.

-fcond-mismatch
Allows the second and third parameter types of the conditional expression to not match, and the value of the expression will be a void type.

-funsigned-char
-fno-signed-char
-fsigned-char
-fno-unsigned-char
These four parameters are set to the char type, which determines that the char type is set to unsigned char (the first two parameters
Number) or signed char (the latter two parameters)

-include File
Contains a code, that is, a compiled file when you need to rely on another file, you can use it to include it in together to compile, the function is equivalent to #include<filename>
Example usage:
GCC Hello.c-include/root/pianopan.h

-imacros File
Expands the file's macro to the gcc/g++ input file, and the macro definition itself does not appear in the input file.

-dmacro
Equivalent to the #define macro in C language

-dmacro=defn
Equivalent to the #define MACRO=DEFN in C language

-umacro
Equivalent to the #undef macro in C language
-undef
Cancel the definition of any nonstandard macro

-idir
When you use the #include "file", gcc/g++ will first find your header file in the current directory, if not found, he returned to the default header file directory to find, if use-I developed the directory, he will first in the directory you specify, and then in the usual order to look.
For #include<file>,gcc/g++ directory lookup to-I, if not found, it will go to the system's default header file directory to find.

-i-
is to cancel the function of the previous parameter, so it is usually used after-idir.

-idirafter dir
Find in the directory of-I after the failure is found in the directory.

-iprefix Prefix
-iwithprefix dir

Generally used together, when-I directory lookup fails, it will be found under Prefix+dir

-nostdinc
Causes the compiler to not be in the system default header file directory to change the file, general and-I joint use, explicitly qualifying the location of the header file.

-nostdin C + +
The rule does not search in the standard path specified by g++, but still searches in other paths, which is used when creating the libg++ library

- C
In the preprocessing, do not delete the annotation information, general and e-use, and sometimes analytical procedures, with this very convenient.

- M
Generate file associated information that contains all the source code that the target file relies on, and you can test it with gcc-m hello.c.

-mm
Same as the one above, but it ignores dependencies caused by #include<file>.

-md
Is the same as-m, but the output is imported into the. d file.

-mmd
Same as-mm, but the output is imported into the. d file.

-wa,option
This option passes option to the assembler and, if there is a comma between option, the option is divided into multiple options and then passed to the assembly.

-wl.option
This option passes option to the connector and, if there is a comma between option, divides option into multiple options and then passes it to the connecting program.

-llibrary
Specify the libraries to use when compiling
Example usage:
Gcc-lcurses hello.c
Compiling programs using the Ncurses library

-ldir
Define the path to the library on which the compile is dependent, and Dir is the directory name. such as your own library, you can use it to create a directory, otherwise the compiler will only be in the standard library directory search.

-o0
-o1
-o2
-o3
The 4 levels of the compiler's tuning options,-o0 means no optimizations,-o1 defaults,-o2 further optimizations,-o3 the highest level of optimization, and further optimizations than-O2, including inline functions.

- G

Just the compiler, when compiling, produces debugging information.

-gstabs
This option claims debugging information in the stabs format, but does not include GDB debugging information.

-gstabs+
This option claims debugging information in the stabs format and contains additional debugging information that is available only to GDB.

-ggdb
This option will generate the debug information that GDB can use as much as possible.


-static
This option disables the use of dynamic libraries, so the things that are compiled are generally large because they compile the libraries on which they depend, so that no dynamic connection libraries are required to run.


-share
This option will use dynamic libraries as much as possible, so the resulting files are relatively small, but require the system to be run by dynamic libraries.


-traditional
Try to get the compiler to support the traditional C language features.

- W

Does not generate any warning messages.


-wall

Generate all warning messages.

GCC is the GNU C and C + + compiler. In fact, GCC supports compilation in three languages: C, C + +, and OBJECTC (an object-oriented extension of the C language). Use the GCC command to compile and connect both C and C + + source programs at the same time.
If you have two or a few C source files, you can also easily compile, connect, and build executables using GCC. For example, suppose you have two source files Main.c and factorial.c two source files, now you want to compile a program that generates a factorial calculation.
The code is as follows:
-----------------------
Factorial.c
-----------------------
int factorial (int n)
{
if (n <= 1)
return 1;
Else
return factorial (n-1) * n;
}
-----------------------
List MAIN.C
-----------------------
#include <stdio.h>
#include <unistd.h>
int factorial (int n);
int main (int argc, char **argv)
{
int n;
if (ARGC < 2)
{
printf ("Usage:%s n\n", argv [0]);
return-1;
}
Else
{
n = atoi (argv[1]);
printf ("Factorial of%d is%d.\n", N,factorial (n));
}
return 0;
}
-----------------------
Use the following command to compile the build executable file and execute the program:
$ gcc-o factorial main.c factorial.c
$/factorial 5
Factorial of 5 is 120.
GCC can also be used to compile C programs and C + + programs. Generally speaking, C compiler by the source file suffix name to determine whether the C program or C + + program. In Linux, the C source file has a suffix named. C, and the C + + source file has a suffix named. C or. cpp. However, the GCC command compiles only the C + + source files, not the library links that are used by the C + + program. Therefore, the g++ command is typically used to compile and connect C + + programs, which automatically invoke GCC implementation compilation. Let's say we have a C + + source file like the following (hello. C):

#include <iostream>
void Main (void)
{
cout << "Hello, world!" <<endl;
}
You can compile, connect, and build an executable file by calling the g++ command as follows:
$ g++-o Hello hello. C
$./hello
Hello, world!.
Main options for Gcc/egcs *********

Resources
1, "Linux/unix Advanced Programming"
----Red Flag Software Technology Co., Ltd., Tsinghua University Press published.
2, "Gcc man page".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.