g++ Compilation Summary
First, build attention to detail
1. Use g++ to compile CPP files if you compile C + + source files with GCC, the following option is used:-lstdc++, otherwise, file compilation using C + + operations will fail.
2.gcc/g++ requires 4 steps to perform the compilation
① preprocessing, generating. i files [using the-e parameter]
② the preprocessed file is not converted to assembly language, generating the file. s[using the-s parameter]
③ has assembly changed to target code (machine code) to generate. o files [using the-C parameter]
④ Connect the target code, generate the executable program [using the-o parameter]
Second, GDB debugging
A list of DEBUG commands for 1.gdb.
Command |
Explain |
Shorthand |
File |
Load the executable file you want to debug |
No |
List |
List part of the source code that generated the execution file |
L |
Next |
Executes a line of source code but does not enter inside the function |
N |
Step |
Executes a line of source code and goes inside the function |
S |
Run |
Executing a program that is currently being debugged |
R |
Continue |
Continue to execute the program |
C |
Quit |
Terminate GDB |
Q |
Print |
Outputs the value of the current set variable |
P |
Break |
Set breakpoints in code |
B |
Info break |
To view information about setting breakpoints |
Ib |
Delete |
Delete a set breakpoint |
D |
Watch |
Monitor the value of a variable, and the program stops once the value changes. |
Wa |
Help |
GDB's help command |
H |
How to use:
Add the –g parameter when compiling the program, for example:
g++-g-o Hello Hello.cpp
The use of debugging in GDB to re-enter the command gdb+ executable file name, such as:
GDB Hello
View Source file List 1, enter repeat last instruction
Then, according to the parameter list, enter your own commands according to your own requirements.
Reference URL:
http://blog.csdn.net/wfdtxz/article/details/7368357
In addition, if you compile with makefile, pay special attention to the use of regular expressions, otherwise the compiled files will not be used for debugging.
such as Makefile1: (operating normally)
EXECUTEMAIN:MAIN.O COMMONFUN.O mytool.o g++-o executemain main.o commonfun.o mytool.omain.o:main.cpp commonfun.h Mytool.h g++-g-c main.cpp common.o:commonfun.h commonfun.cpp g++-g-c commonfun.cppmytool.o:mytool.h mytool . cpp g++-g-c mytool.cppclean: rm-f executemain main.o commonfun.o MYTOOL.O
and Makefile2:
COMPILER=G++EXECUTEMAIN:MAIN.O COMMONFUN.O MYTOOL.O $ (COMPILER)-o [email protected] $^. C.O: $ (COMPILER)-g-c $<clean: rm-f *.o Executemain
The following error is indicated:
Third, g++ compilation options in detail:
For details, see annex: g++ Compilation Options table.
Attachment 1:g++ compilation Options table:
gcc/g++ |
Common options format for commands (option interpretation) |
-O FILE |
Specifies the output file name, which is not required when compiling to the target code. If file is not specified, the default file name is a.out. |
-C |
Compile only build target file, not link |
-m486 |
Code optimization for 486. |
-o0 |
No optimization processing is performed. |
-O or-o1 |
Optimize the generated code. |
-o2 |
Further optimization. |
-o3 |
Further optimizations than-O2, including inline functions. |
-W |
Turn off all warnings and do not recommend using this item |
-wall |
Allows all useful warnings provided by GCC to be issued, or the specified warning can be marked with-W (warning) |
-werror |
Converts all warnings to errors to abort the compilation process when a warning occurs |
-mm |
Output a make-compatible related list |
-V |
Displays the commands used in each step of the compilation process |
-E |
Only the C pre-compiler is run. |
-shared |
Generate a shared destination file. Typically used when building a shared library. |
-static |
Link static libraries, i.e. perform static links |
-lfoo |
Link to a library of functions named Libfoo |
-G |
Include standard debugging information in an executable program |
-ggdb |
Include only GNU debugger in the executable program to make the other up to two is information |
-O |
Optimization of compiled code |
-on |
Specifies that the level of code optimization is n,o<=n<=3 |
-ansi |
The standard syntax for Ansi/iso C is supported, and the part of the GNU syntax extension that conflicts with the standard (but this option does not guarantee the generation of ANSI-compatible code) will prohibit certain features of GNU C, such as ASM or typeof keywords. |
-pedantic |
Allow all warnings listed in the Ansi/iso C standard to be issued |
-errors |
Allow all errors listed in the Ansi/iso C standard to be issued |
-traditional |
Supports Kernighan & Ritchie C syntax (such as defining functions with legacy syntax); If you don't know what this option means, it doesn't matter. |
-idirectory |
Specifies additional header file search paths for directory. |
-ldirectory |
Specifies the additional library search path directory. |
-dfoo=bar |
Define the preprocessing macro foo at the command line with a value of bar |
-idirname |
Add dirname to the search directory list in the header file |
-ldirname |
Add dirname to the search directory list of the library file, by default GCC only links the shared library |
-dmacro |
The macro macro is defined with the string "1". |
-dmacro=defn |
The macro macro is defined as the string "DEFN". |
-umacro |
Cancels the definition of macro macros. |
g++ Compilation Summary