Table of contents
Program Basic Concepts
Use ubuntu22.0 as a demonstration environment (vmware virtual machine setup)
Configure the development environment
After configuring the basic development environment, you can directly download a vscode to use as the initial LDE (you will gradually become familiar with Vim before switching to it, don’t go to the sky at the beginning)
Development requires compilers, header files, and corresponding standard libraries and documents
must be downloadedgcc gdb make
● gcc: The GNU C compiLer
● Libc6-dev: GNU C Library: DeveLopment Libraries and Header FiLes
● manpages-dev: ManuaL pages about using GNU/Linux for deveLopment
● manpages-posix-dev: ManuaL pages about using a POSLX system for deveLopment
● binutiLs: The GNU assembLer, linker and binary utiLities
● gdb: The GNU Debugger
● make: The GNU version of the “make” utiLity
first program
Create a folder, create a new file main.c
, and gedit
edit
mkdir linuxc
cd linuxc
touch main.c
gedit main.c
main.c
Add simple code for
#include <stdio.h>
int main(void){
printf("%s\n","helloworld");
return 0;
}
Use gcc to compile to get the default file output a.out, and then directly call the a.out file to execute!
gcc main.c
./a.out
gcc compile specific name gcc xxx.c -o main.out
gcc compile echo all warningsgcc -Wall xxx.c
C review
Review a ghost, find it yourself cprimerplus
and primer
read it in C++
Get started directly with gdb in the next section
gdb
Stepping and Tracing
First write a simple c file: main.c
#include <stdio.h>
int add_range(int Low, int high)
{
int i, sum;
for (i = Low; i <= high; i++)
sum = sum + i;
return sum;
}
int main(void)
{
int result[100];
result[0] = add_range(1, 10);
result[1] = add_range(1, 100);
printf("result[0]=%d\nresult[1]=%d\n", result[0], result[1]);
return 0;
}
If we want gdb to debug the code, gcc must add the -g parameter when compiling, indicating that the source code (reference) is inserted into the compiled file
gcc -g main.c -o main
Then use gdb to run the compiled file
gdb main
The -g parameter does not directly put the source code into the compiled file. When we use gdb to debug, the source code file still needs to exist in the current folder. The simply compiled file cannot be executed!
View the source code, list 1
press Enter to quickly execute the previous command
list specified functionsl [函数名]
Quit gdb (you will be asked once) quit
to force quitexit
Start to execute the program start
Next n
| next
Deep mode (can enter the inside of the executed function) s
|step
View function call frame stack bt
View function local variables i
Select specified stack framef [栈帧号]
Print the value of the variable p [变量名]
Run until the end of the current functionfinish
breakpoint
Prepare to test the code
#include <stdio.h>
int main(void)
{
int sum = 0, i = 0;
char input[5];
while (1) {
scanf("%s", input);
for (i = 0; input[i] != '\0'; i++)
sum = sum*10 + input[i] - '0';
printf("input=%d\n", sum);
}
return 0;
}
Label variables (labeled variables will be displayed once per run) display [变量名]
Unlabel variables undisplay [变量名]
Clear all labelsclear
Breakpoints for specified lines b [行号]
Temporarily turn off breakpoints disable b [断点号]
Delete all breakpoints delete breakpoints
Continue to run code until a breakpoint is encountered and stop c
Running from the beginning r
View all breakpoint detailsi breakpoints
Breakpoints can also set conditions that activate the breakpoint when the condition is met
break 9 if sum!=0
Observation Point
#include <stdio.h>
int main(void)
{
int sum = 0, i = 0;
char input[5];
while (1) {
sum = 0;
scanf("%s", input);
for (i = 0; input[i] != '\0'; i++)
sum = sum*10 + input[i] - '0';
printf("input=%d\n", sum);
}
return 0;
}
set watchpointwatch [欲观察的变量名]
segment fault
#include <stdio.h>
int main(void)
{
int man = 0;
scanf("%d", man);
return 0;
}
Obviously, there is an & symbol missing in front of the corresponding man in the scanf summarized in the above code.
At this time, if you use gdb for run debugging, an error will be reported directly in the corresponding line;
For some errors, it may not be thrown directly when running line by line, but may be thrown when return is executed, so pay attention to this!