Compilation & Debugging via Command Line
Before reading this section, please install GCC and gdb. For specific methods, refer to the Compiler article.
Using g++ from the Command Line to Compile cpp Files
Manual Compilation
Enter g++ a.cpp in the command line to compile the file a.cpp (on Windows systems, you need to add the compiler's directory to PATH in advance).
You can add some compilation options during the process:
-o <filename>: Specifies the output executable filename.-g: Adds debugging information during compilation (required when using gdb for debugging).-Wall: Displays all compilation warning messages.-O1,-O2,-O3,-Ofast: Optimizes the compiled program. Higher levels indicate more optimization techniques applied (enabling optimization affects gdb debugging).-DDEBUG: Defines theDEBUGsymbol during compilation (the symbol can be changed arbitrarily, e.g.,-DONLINE_JUDGEdefines theONLINE_JUDGEsymbol).-UDEBUG: Undefines theDEBUGsymbol during compilation.-lm,-lgmp: Links a specific library (here math and gmp; the exact name should be checked in the library documentation, but it is usually the same as the library name).
Note
On Unix systems, if you use the math library (math.h) from the standard C library, you need to add the -lm parameter during compilation.2
How to Increase Stack Space?
On Windows, you can use the compilation option -Wl,--stack=536870912 to increase the stack space to 512 MB, where the number after the equals sign is in bytes.
On Unix, use ulimit -s [num] to set the stack space of the current terminal to [num] KiB.
Using GNU Make's Built-in Rules1
For C/C++ program source code named qwq.c/cpp, you can use make qwq to automatically compile it into a program named qwq.
If additional compilation options are needed, you can specify them using export CFLAGS="xxx" (for C programs) or export CXXFLAGS="xxx" (for C++ programs). If additional precompilation options are needed, use export CPPFLAGS="xxx". The above settings can also be written as CFLAGS="xxx" CPPFLAGS="xxx" make qwq to specify environment variables for a single command execution.
Sanitizers
Introduction
Sanitizers are tools integrated into compilers for debugging C/C++ code. They insert check code during compilation to detect runtime errors such as out-of-bounds memory access and undefined behavior.
They are divided into the following types:
- AddressSanitizer3: Detects out-of-bounds access to heap, stack, and global variables, invalid memory deallocation, and memory leaks (experimental).
- ThreadSanitizer4: Detects data races in multithreaded programs.
- MemorySanitizer5: Detects reads from uninitialized memory.
- UndefinedBehaviorSanitizer6: Detects undefined behavior.
Usage
The latest versions of clang++, g++, and MSVC (partial support) have built-in sanitizers, but their features and usage methods differ. Here, clang++ is used as an example. Its usage is as follows:
1 | |
Here, <name> is the feature to enable (a sanitizer can be understood as a collection of features). For example:
1 2 | |
Afterward, run the executable as usual. If the sanitizer detects an error, it will output to the stderr stream. For example:
1 2 | |
Warning
g++ on Windows does not support sanitizers. You need to use a modified MinGW64 or another compiler.
As of version 17.14, MSVC only supports AddressSanitizer.
Time/Memory Overhead
Obviously, these debugging tools significantly slow down code execution and increase memory usage. The time/memory overheads are as follows:
| Name | Memory Multiplier | Time Multiplier |
|---|---|---|
| AddressSanitizer | N/A | 2 |
| ThreadSanitizer | 5~15 | 5~10 |
| MemorySanitizer | N/A | 3 |
| UndefinedBehaviorSanitizer | N/A | N/A |
Using gdb from the Command Line for Debugging
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Press c to continue. Then, after the prompt Reading symbols from [filename]..., when (gdb) appears, you can enter commands for debugging.
Below is a categorized list of commonly used commands:
Basic gdb Commands
| Command | Description |
|---|---|
help |
Displays help information |
quit |
Exits gdb |
file [filename] |
Loads the program [filename] to debug |
Execution Control Commands
| Command | Description |
|---|---|
run |
Runs the program until a breakpoint is hit or the program ends |
continue |
Continues execution until a breakpoint is hit or the program ends |
next |
Steps over, enters functions when encountering function calls |
step |
Steps into, enters functions when encountering function calls |
finish |
Runs until the current function returns, then stops and waits for commands |
until [num] |
Runs until line number [num], then stops and waits for commands |
break [num] |
Sets a breakpoint at line [num]; the program stops there and waits for commands |
condition [id] [p] |
Sets a condition for breakpoint [id]; the breakpoint is enabled only when expression [p] is satisfied |
ignore [id] [num] |
Ignores the first [num] triggers of the breakpoint |
delete [id] |
Deletes the breakpoint with the specified ID |
disable [id] |
Disables the breakpoint with the specified ID |
enable [id] |
Enables the breakpoint with the specified ID |
list |
Lists source code, continuing from the last position, 10 lines at a time |
list [num] |
Lists source code centered around line [num] |
list [func-name] |
Lists source code centered around a specific function |
call [function] |
Calls a function and prints its return value |
break [num] outputs the breakpoint ID. You can also use break [func-name] to set a function breakpoint.
You can also use break [num] [p] to achieve an effect similar to condition [id] [p] when setting a breakpoint.
Stack Frame Commands
| Command | Description |
|---|---|
info args |
Displays function arguments |
backtrace |
Displays function call stack and arguments |
frame |
Selects a stack frame |
up |
Moves up one stack frame |
down |
Moves down one stack frame |
Variable Commands
| Command | Description |
|---|---|
print [p] |
Prints the value of expression [p]; variables can be modified via expressions |
display [p] |
Prints the value of expression [p] each time execution pauses, but does not step into functions |
watch [var] |
Monitors variable [var]; when the variable is written, it prints automatically and pauses |
rwatch [var] |
Monitors variable [var]; when the variable is read, it prints automatically |
awatch [var] |
When variable [var] is modified or written, it prints automatically and pauses |
set [assignment] |
Executes an assignment statement |
Both display and print commands support controlling the output format by appending / followed by a format character. For example, print/display [var] (prints the value of variable [var] in decimal). Supported format characters include:
| Format Character | Corresponding Format |
|---|---|
d |
Display variable in decimal |
x |
Display variable in hexadecimal |
a |
Display variable in hexadecimal |
t |
Display variable in binary |
c |
Display variable as a character |
f |
Display variable as a floating-point number |
u |
Display unsigned integer in decimal |
o |
Display variable in octal |
Information Commands
| Command | Description |
|---|---|
info breakpoints |
Lists all breakpoints |
info locals |
Lists local variables in the current stack frame |
info args |
Lists function arguments in the current stack frame |
info threads |
Lists all threads |
info program |
Displays the current state of the program |
info registers |
Displays current register values |
info frame |
Displays information about the current stack frame |
Other Commands
| Command | Description |
|---|---|
enable pretty-printer |
Enables pretty-printer, allowing STL containers to be printed in a human-readable format |
checkpoint7 |
Creates a checkpoint, allowing rollback to the checkpoint |
restart [num]7 |
Rolls back to the [num]th checkpoint |
save breakpoints [filename] |
Saves breakpoints to a file |
source [filename] |
Imports breakpoints from a file |
Tip
Most commands in gdb can be abbreviated to a unique letter or letters. For example, breakpoint can be abbreviated as b, step as s, and info args as i ar. See the help command for details.
References and Notes
-
https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html ↩
-
Commands related to checkpoints are only applicable on GNU/Linux platforms. For details, see the GDB Official Manual. ↩↩
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用