Gcc and g ++ are respectively gnu c & c ++ Compilers
Gcc/g ++ requires a total of four steps for compiling.
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. a. o file is generated by means of compilation into the 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. It will be valid for multiple later versions, that is, the root
It is agreed that the c language suffix is. C, and the C ++ suffix is. c or. cpp. If
You are very personal and decide that the suffix of your C code file is. pig Haha, then you need to use this
Parameter. This parameter applies to all file names after it, unless the next parameter is reached.
.
Can I use the following parameters?
'C', 'Objective-C', 'c-header', 'c ++ ', 'cpp-output ',
'Referer', and 'referer-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
-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
-S
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.
-E
Only activate preprocessing. This does not generate a file. You need to redirect it to an output file.
Area.
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.
-O
Specify the target name. The gcc compiled file is a. out by default. If
You and I share the same feelings, 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
-Pipe
Use pipelines to replace temporary files in compilation. When using non-gnu assembly tools, you may have some questions.
Question
Gcc-pipe-o hello.exe hello. c
-Ansi
Disable the incompatible features of ansi c in gnu c and activate the proprietary features of ansi c (including disabling
Some asm inline typeof keywords, as well as pre-processing macros such as UNIX and vax,
-Fno-asm
This option is part of the feature that implements the ansi option. It does not allow the use of asm, inline, and typeof
Keyword.
-Fno-strict-prototype
It only works for g ++. With this option, g ++ considers all functions without parameters as not explicitly
Instead of having no parameters.
Whether this parameter is used or not, gcc will not explicitly describe functions without parameters.
Explicit type
-Fthis-is-varialble
Is to align with the traditional c ++, you can use this when the general variable is used.
-Fcond-mismatch
The second and third parameter types of conditional expressions do not match. The expression value is of the void type.
-Funsigned-char
-Fno-signed-char
-Fsigned-char
-Fno-unsigned-char
These four parameters are used to set the char type and decide to set the char type to unsigned char (before
Two Parameters) or signed char (the last two parameters)
-Include file
Contains a code. Simply put, a file can be used when another file is needed.
Using it to set the function is equivalent to using # include <filename> in 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.
Medium
-Dmacro
Equivalent to # define macro in C
-Dmacro = defn
Equivalent to # define macro = defn in C
-Umacro
Equivalent to # undef macro in C
-Undef
Cancel definition of any non-standard macros
-Idir
When you use # include "file", gcc/g ++ first searches for the header you have specified in the current directory.
File. If 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>, the gcc/g ++ will go to the directory specified by-I, and the gcc/g ++ will not find it.
Default header file directory search
-I-
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
-Nostdinc
Make the compiler no longer look for the header file in the system's default header file directory, which is generally used in conjunction with-I, clear
Specify 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 created
Libg ++ library usage
-C
During pre-processing, do not delete the annotation information, which is generally used with-E. Sometimes the analysis program uses this
Convenient
-M
Generate file association information. Contains all source code on which the target file depends
You can use gcc-M hello. c to test it.
-MM
It is the same as the one above, but it will ignore the dependency caused by # include <file>.
-MD
It is the same as-M, but the output will be imported into the. d file.
-MMD
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.
-Llibrary
Library Used during compilation
Example usage
Gcc-lcurses hello. c
Compile the program using the ncurses Library
-Ldir
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.
-O0
-O1
-O2
-O3
The compiler has four levels of optimization options.-O0 indicates no optimization,-O1 indicates the default value, and-O3 indicates the most optimized level.
High
-G
It is only a compiler. during compilation, information is generated.
-Maid
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.
-Ggdb
This option will generate debugging information that can be used by gdb as much as possible.
-Static
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.
-Share
This option will try to use the dynamic library, so the generated file is small, but the system needs to use the dynamic library.
-Traditional
Try to make the compiler support the traditional C Language Features
The GNU Debugger is called gdb. This program is an interactive tool that works in character mode. In the X Window System, there is a gdb
The front-end graphics tool is called xxgdb. Gdb is a powerful debugging program that can complete the following debugging tasks:
* Set breakpoints;
* The value of the Monitored Program variable;
* One-Step Program Execution;
* Modify the value of a variable.
Before you can use gdb to debug a program, you must use the-g option to compile the source file. You can define the CFLAGS variable in makefile:
CFLAGS =-g
The following command is often used to run the gdb debugging program:
Gdb progname
Enter help at the gdb prompt to list the command categories, including:
* Aliases: Command alias
* Breakpoints: breakpoint definition;
* Data: View data;
* Files: Specify and view files;
* Internals: maintenance command;
* Running: program execution;
* Stack: view the call stack;
* Statu: view the status;
* Tracepoints: trace program execution.
Enter help and the classification name of the command to obtain a detailed list of such commands.
# DENO #
Common gdb commands
Table 1-4 common gdb commands
Command explanation
Break NUM sets a breakpoint on the specified row.
Bt displays all call stack frames. This command can be used to display the call sequence of a function.
Clear deletes a breakpoint set on a specific source file or line. The usage is clear FILENAME: NUM.
Continue continues to execute the program being debugged. This command is used by the program to process signals or breakpoints.
This causes the instance to stop running.
Display EXPR: The expression value is displayed after each program is stopped. An expression is composed of variables defined by the program.
File FILE to load the specified executable file for debugging.
Help NAME: displays the help information of a specified command.
Info break displays the current breakpoint list, including the number of times the breakpoint is reached.
Info files: displays detailed information about the file to be debugged.
Info func shows all function names.
Info local shows the local variable information in the function.
Info prog displays the execution status of the program to be debugged.
Info var displays all global and static variable names.
Kill the program being debugged.
List shows source code segments.
Make runs the make tool without exiting gdb.
Next executes a source code line forward without entering other functions.
Print EXPR: the value of the expression EXPR.
Quit exits the current program
Finsh is directly executed
The difference between setp and next is that it can enter the function.
Until runs directly to a line
In addition, use gc-g-oo during compilation to disable optimization, because optimization may be detrimental to debugging,
1.8.5 example of gdb
-----------------
Listing a wrong C source code bugging. c
-----------------
# Include
# Include
Static char buff [256];
Static char * string;
Int main ()
{
Printf ("Please input a string :");
Gets (string );
Printf ("\ nYour string is: % s \ n", string );
}
-----------------
The above program is very simple, the purpose is to accept the user input, and then print out the user input. This program uses
String. Therefore, after compiling and running, the Segment Fault error will occur:
$ Gcc-o test-g test. c
$./Test
Please input a string: asfd
Segmentation fault (core dumped)
To find problems in the program, we use gdb and follow the steps below:
1. Run the gdb bugging command to load the bugging executable file;
2. Run the loaded bugging command;
3. Run the where command to view the errors in the program;
4. Use the list command to view the code near the call to the gets function;
5. the only cause of the gets function error is the variable string. Use the print command to view the string value;
6. In gdb, we can directly modify the value of the variable, as long as a valid pointer value is taken for string. Therefore, in
Set a breakpoint at 11 rows;
7. Run the program again and stop it at line 11th. Then, we can use the set variable command to modify the value of string;
8. Continue running. The correct program running result is displayed.
The difference between gcc and g ++ is:
When we compile c/c ++ Code, some people use gcc and some people use g ++, so all kinds of statements come, such as c code using gcc, c ++ Code uses g ++, or gcc for compilation, and g ++ for link connection. I do not know which statement is correct at the moment. If the previous extern "C ", there are more differences. Here I want to end it. After all, the purpose of knowledge is to be clearer, not more confused.
Misunderstanding 1: gcc can only compile c code, while g ++ can only compile c ++ code
Both can be used, but note the following:
1. suffix. c, gcc treats it as a C program, while g ++ treats it as a c ++ program; suffix. cpp, both of which are considered as c ++ programs. Note that although c ++ is a superset of c, their syntax requirements are different. For example:
# Include <stdio. h>
Int main (int argc, char * argv []) {
If (argv = 0) return;
PrintString (argv );
Return;
}
Int printString (char * string ){
Sprintf (string, "This is a test. \ n ");
}
If it is OK according to the C syntax, but once the suffix is changed to cpp, three errors are immediately reported: "printString undefined ";
"Cannot convert 'Char ** 'to 'Char *";
"Return-statement with no value";
Corresponding to the red part. It can be seen that the syntax rules of C ++ are more rigorous.
2. in the compilation phase, g ++ calls gcc. For c ++ code, the two are equivalent, but because the gcc command cannot be automatically connected to the library used by the C ++ program, therefore, we usually use g ++ to complete the link. For the sake of unification, we simply use g ++ to compile/link all. This gives us the illusion that, it seems that cpp programs can only use g ++.
Misunderstanding 2: gcc does not define the _ cplusplus macro, while g ++ will
In fact, this macro only indicates that the compiler will interpret the code in the C or C ++ syntax. As mentioned above, if the suffix is. c. If the gcc compiler is used, the macro is undefined. Otherwise, the macro is defined.
Misunderstanding 3: only gcc can be used for compilation, and only g ++ can be used for Link
Strictly speaking, this sentence is not a mistake, but it obfuscated the concept. It should be said that gcc/g ++ can be used for compilation, the link can use g ++ or gcc-lstdc ++. Because the gcc command cannot be automatically connected to the library used by the C ++ program, g ++ is usually used to complete the connection. However, in the compilation phase, g ++ automatically calls gcc, which is equivalent to the two.
Misunderstanding 4: extern "C" is related to gcc/g ++
In fact, it does not matter. Whether it is gcc or g ++, when using extern "c", it is named as C. Otherwise, all are named in c ++ mode.
++
My own summary:
1> In the link stage, gcc-lstdc ++ is equivalent to g ++. If the program uses a C ++ library, without-lstdc ++, many undefined Link errors are reported.
2> one way to avoid mistakes is:Compile the link c program with gcc; Compile the link c ++ program with g ++