C++ Crash Course: a fast-Paced Introduction



Yüklə 7 Mb.
Pdf görüntüsü
səhifə60/71
tarix20.09.2023
ölçüsü7 Mb.
#145939
1   ...   56   57   58   59   60   61   62   63   ...   71
C Crash Course A Fast-Paced Introduction by Josh Lospinoso

break main.cpp:5
5. You can also tell 
gdb
to break at a particular function by name:
(gdb) 
break main
6. Either way, you can now execute your program:
(gdb) 
run
Starting program: /home/josh/stepfun
Breakpoint 1, main () at main.cpp:5
5 int num1 = 42;
(gdb)


Up and Running
27
7. To single step into an instruction, you use the 
step
command to follow 
each line of the program, including steps into functions:
(gdb) 
step
6 int result1 = step_function(num1);
8. To continue stepping, press 
enter
to repeat the last command:
(gdb)
step_function (x=42) at step_function.cpp:4
9. To step back out of a function invocation, you use the 
finish
command:
(gdb) 
finish
Run till exit from #0 step_function (x=42) at step_function.cpp:7
0x0000000000400546 in main () at main.cpp:6
6 int result1 = step_function(num1);
Value returned is $1 = 1
10. To execute an instruction without stepping into a function call, you use 
the 
next
command:
(gdb) 
next
8 int num2 = 0;
11. To inspect the current value of variables, you use the 
info locals
command:
(gdb) 
info locals
num2 = -648029488
result2 = 32767
num1 = 42
result1 = 1
num3 = 0
result3 = 0
Notice that any variables that have not yet been initialized will not 
have sensible values.
12. To continue execution until the next breakpoint (or until the program 
completes), use the 
continue
command:
(gdb) continue
Continuing.
Num1: 42, Step: 1
Num2: 0, Step: 0
Num3: -32768, Step: -1
[Inferior 1 (process 1322) exited normally]
13. Use the 
quit
command to exit 
gdb
at any time.


28
Chapter 1
GDB supports many more features. For more information, check out 
the documentation at https://sourceware.org/gdb/current/onlinedocs/gdb/.
Summary
This chapter got you up and running with a working C++ development envi-
ronment, and you compiled your first C++ program. You learned about the 
components of a build tool chain and the roles they play in the compilation 
process. Then you explored a few essential C++ topics, such as types, declar-
ing variables, statements, conditionals, functions, and 
printf
. The chapter 
wrapped up with a tutorial on setting up a debugger and stepping through 
your project.
N O T E
 
If you have problems setting up your environment, search on your error messages 
online. If that fails, post your question to Stack Overflow at https://stackoverflow 
.com/, the C++ subreddit at https://www.reddit.com/r/cpp_questions/, or the 
C++ Slack channel at https://cpplang.now.sh/.

Yüklə 7 Mb.

Dostları ilə paylaş:
1   ...   56   57   58   59   60   61   62   63   ...   71




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin