VI usage Daquan!

Source: Internet
Author: User

This document is my note on GCC parameters. I miss the DOS age and I used a notebook to record all the DOS commands.
Haha, the following things may not be very comprehensive. I have referenced a lot of books and GCC help.
Because it is possible that I have not seen this parameter, and another reason is that I may not use it yet, I will slowly
Complete. Haha if you want to transfer in this article please keep my email ( and article comprehensive
GCC and G ++ are the gnu c & C ++ compiler gcc/g ++. A total of four steps are required during compilation.

1. pre-process and generate the. I file [pre-processor CPP]
2. Do not convert the pre-processed files into assembly languages to generate the file. s [compiler egcs]
3. Generate. O files from assembly to target code (machine code) [assembler as]
4. Connect the target code to generate an executable program [linker ld]
[Parameter Details]
-X language filename
Set the language used by the file to make the suffix invalid and valid for multiple later versions.
The suffix name is. C, and C ++ suffix is. C or. cpp. If you are very personalized, decide the suffix of your C code file.
The name is. Pig Haha. You need to use this parameter. This parameter applies to all file names after it, unless
Use of the next parameter.
Can I use the following parameters?
'C', 'Objective-C', 'c-header', 'c ++ ', 'cpp-output', 'Explorer', and 'a
Ssembler-with-CPP '.
You can understand English.
Example usage:
Gcc-x C hello. Pig
-X none filename
Turn off the previous option, that is, let GCC automatically identify the file type based on the file name suffix
Example usage:
Gcc-x C hello. Pig-x none hello2.c
Only activate preprocessing, compilation, and assembly, that is, he only makes the program into an OBJ file.
Example usage:
Gcc-C hello. c
It will generate the. o obj file
Only activating preprocessing and compilation means compiling a file into assembly code.
Example usage
Gcc-s hello. c
It will generate the. s assembly code. You can view it in a text editor.
Only activate preprocessing. If this file is not generated, You need to redirect it to an output file.
Example usage:
Gcc-e hello. c> pianoapan.txt
Gcc-e hello. c | more
Let's take a look. A Hello word should also process 800 lines of code.
Specify the target name. By default, the GCC compiled file is a. Out, which is hard to hear. If you feel the same with me
, Get rid of it, haha
Example usage
Gcc-O hello.exe hello. C (Oh, Windows is used to it)
Gcc-O hello. ASM-s hello. c
Using pipelines instead of temporary files during compilation may cause some problems when using non-GNU compilation tools.
Gcc-pipe-O hello.exe hello. c
Disable features incompatible with ansi c in gnu c and activate the proprietary features of ansi c (including disabling some ASM INL
INE typeof keywords, as well as preprocessing macros such as UNIX and VAX,
This option is part of the feature that implements the ANSI option. It prohibits the use of ASM, inline, and typeof as keywords.
It only works for G ++. With this option, G ++ considers that there is no explicit parameter pair for functions without parameters.
Instead of having no parameters.
Whether this parameter is used or not, GCC considers that there is no explicitly stated type for a function without parameters.

Is to align with the traditional C ++, you can use this when the general variable is used.
The second and third parameter types of conditional expressions do not match. The expression value is of the void type.
These four parameters are used to set the char type and decide to set the char type to unsigned char (the first two parameters
Number) or signed Char (the last two parameters)
-Include File
Contains a code. Simply put, a file can be used to set
The function is equivalent to using # include <FILENAME> in the code.
Example usage:
GCC hello. C-include/root/pianopan. h
-Imacros File
Extends the macro of the file to the input file of GCC/g ++. The macro definition does not appear in the input file.
Equivalent to # define macro in C
-Dmacro = defn
Equivalent to # define macro = defn in C
Equivalent to # UNDEF macro in C
Cancel definition of any non-standard macros
When you use # include "file", GCC/g ++ first searches for the header file in the current directory, as shown in figure
If it is not found, he will return to the default header file directory. If-I is used to specify the directory
Go back to the directory you created, and then find it in the regular order.
For # include <File>, GCC/g ++ searches for the directory specified by-I, and fails to find the directory.
Saved header file directory search
Is to cancel the function of the previous parameter, so it is generally used after-Idir
-Idirafter dir
An error occurred while searching in the-I directory.
-Iprefix prefix
-Iwithprefix dir
Generally, if-I Directory Search fails, it will be searched under prefix + dir
Enable the compiler to no longer find the header file in the system's default header file directory. It is generally used in conjunction with-I and explicitly limits the header.
File Location
-Nostdin C ++
It is specified that the search is not in the standard path specified by G ++, but is still in another path. This option is in the creation of libg ++ Library
During pre-processing, do not delete the annotation information, which is generally used with-E. Sometimes the analysis program is very convenient to use.

Generate file association information. You can use gcc-M hello. C to include all the source code that the target file depends on.
It's easy to test.
It is the same as the one above, but it will ignore the dependency caused by # include <File>.
It is the same as-M, but the output will be imported into the. d file.
It is the same as-mm, but the output will be imported into the. d file.
-Wa, Option
This option is passed to the assembler. If there is a comma in the option, the option is divided into multiple options.
And then pass it to the assembler.
-WL. Option
This option is passed to the connection program. If there is a comma in the option, the option is divided into multiple options.
And then pass it to the connected program.
Library Used during compilation
Example usage
Gcc-lcurses hello. c
Compile the program using the ncurses Library
Specify the path of the database to be searched during compilation. For example, you can use your own library to create a directory. Otherwise
The compiler will only find it in the standard library directory. This DIR is the directory name.
The compiler has four levels of optimization options.-O0 indicates no optimization,-O1 indicates the default value, and-O3 indicates the highest optimization level.
Only the compiler generates debugging information during compilation.
This option claims debugging information in stabs format, but does not include GDB debugging information.
-Gstabs +
This option claims debugging information in stabs format and contains additional debugging information for GDB only.
This option will generate debugging information that can be used by GDB as much as possible.
This option will disable the use of dynamic libraries. Therefore, compiled items are generally large and do not need any
The dynamic connection library can be run.
This option will try to use the dynamic library, so the generated file is small, but the system needs to use the dynamic library.
Try to make the compiler support the traditional C Language Features
-Linux/Unix Advanced Programming
Edited by zhongke Hongqi Software Technology Co., Ltd. Published by Tsinghua University Press
-GCC man page
Ver 0.1 released the original document
Ver 0.11 modify document format
Server 0.12 adds parameters for static and dynamic libraries
Ver 0.16 adds four phases of GCC compilation commands
Run gcc/egcs
GCC is the gnu c and C ++ compilers. In fact, GCC can compile three languages: C, C ++, and O
Bject C (an object-oriented extension of C ). The GCC command can be used to compile and Connect C and C ++ at the same time.
Source program.
If you have two or a few C source files, you can easily use GCC to compile, connect, and generate
Execution file. For example, if you have two source files main. C and factorial. C
Generate a program for calculating factorial.
List factorial. c
Int factorial (int n)
If (n <= 1)
Return 1;
Return factorial (n-1) * N;
Listing 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]);
N = atoi (argv [1]);
Printf ("factorial of % d is % d./N", N, factorial (n ));
Return 0;
The following command can be used to compile and generate executable files and execute programs:
$ Gcc-O factorial main. c factorial. c
$./Factorial 5
Factorial of 5 is 120.
GCC can be used to compile both C and C ++ programs. Generally, the C compiler uses the suffix of the source file.
To determine whether it is a C program or a C ++ program. In Linux, the extension of the C source file is. C, while the C ++ source file
The file suffix is. C or. cpp. However, the GCC command can only compile the C ++ source file, but cannot automatically
The library connection used by the ++ program. Therefore, the G ++ command is usually used to compile and connect the C ++ program.
GCC is automatically called to compile the program. Suppose we have the following C ++ source file (hello. C ):
# Include <iostream>
Void main (void)
Cout <"Hello, world! "<Endl;
You can call the G ++ command to compile, connect, and generate an executable file as follows:
$ G ++-O hello. c
Hello, world!
* ******************* Main options of GCC/egcs *********
Common GCC commands
Option description
-ANSI only supports the ANSI standard C syntax. This option will disable some features of gnu c,
For example, ASM or typeof keywords.
-C only compiles and generates the target file.
-Dmacro defines the macro with the string "1.
-Dmacro = defn: Define the macro with the string "defn.
-E only runs the C pre-compiler.
-G generates debugging information. The GNU Debugger can use this information.
-Idirectory: specify an additional header file to search for the path directory.
-Ldirectory: specify an additional function library to search for the path directory.
-Search for the specified library when connecting to llibrary.
-Msung optimizes code for 486.
-O file: generate the specified output file. Used to generate executable files.
-O0 is not optimized.
-O or-O1 optimized code generation.
-O2 is further optimized.
-O3 is further optimized than-O2, including the inline function.
-Shared shared object generation. It is usually used to create a shared library.
-Static prohibit the use of shared connections.
-Umacro undefines macro macros.
-W does not generate any warning information.
-Wall generates all warning information.

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