Embedded GDB debugging and basic operation of opencv

1. GDB debugging exercises and basic usage

1. The executable function of GDB in debugging

1. Set breakpoints to stop the program.
2. Monitor or modify the value of variables in the program.
3. Trace the code execution process.
Note: For the program to be debugged, it must contain debugging information

2. GDB usage and GDB debugging example exercises

1. Gdb debugging example:
Function:
realize the inversion function of an integer number, for example, input 123, then output 321. But when inputting 100, the output result of the program is incorrect. Please find out the problem.
Code: debug1.c:
#include <stdio.h>
void ShowRevertNum(int iNum)
{ while (iNum> 10) { printf("%d", iNum% 10); iNum = iNum / 10; } printf("% d\n", iNum); } int main(void) {int iNum; printf("Please input a number :"); scanf("%d", &iNum); printf("After revert: "); ShowRevertNum( iNum); } 2. Compile the program, and then debug the generated file. When compiling the file, use the -g parameter to compile, then use gdb to debug the generated file. 3. View the program line number and set breakpoints according to the line number or function name for easy tracking Program debugging Note: Use the command b (break) to set the breakpoint and use the l command in GDB to view the program line number















Insert picture description here



Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
After the program is executed, the program is executed according to the breakpoint distribution, staying at the breakpoint, you can view the specific program information and execute the program according to your own progress, which is convenient for monitoring or modifying the value of the variable in the program and tracking the code execution process

3. GDB segfault debugging

1. Debug sample code:

sentence1.c:
#include<stdio.h>
#include<string.h>
#define BUFSIZE 256
static char acBuf[BUFSIZE];
static char *pStr;
int main(void)
{
    
    
	printf("Please input a string:");
	gets(pStr);
	printf("\nYour string is:%s\n",pStr);
}

2. Run the compiled file directly and find a segmentation error.
Insert picture description here
3. After the error is found, debug the file and view the specific information of the program.
Insert picture description here
4. Set a breakpoint to rerun, track the program and modify the program.
Insert picture description here
5. Generate a core file to quickly locate the segmentation error.
Insert picture description here
Let the system generate the core file: ulimit -c num, set the core file capacity (num is a number, 0 means no core file is generated) and then run the program to crash the program, thereby generating the core file, and finally gdb cooperates with the core file to locate the problem .

5. Use the core file to locate the memory error code (independent practice)

1. Practice code:

contain.c:
#include<stdio.h>
int main()
{
    
    
	int *p=0;
	*p=1;
	return 0;
}

2. Compile the file, generate the core file and debug according to the core file, locate the error code, and generate a prompt message.
Insert picture description here

Two, the basic operation and use of opencv

1. Download and installation of opencv

1. First download the compressed package of opencv and place the compressed package in the home directory.
Insert picture description here
2. Then enter the command line to install the opencv
command sequence:

unzip opencv-3.4.11.zip (解压压缩包)
cd opencv-3.4.11 (进入解压文件里) 
sudo apt-get install cmake (安装cmake)
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff5.dev libswscale-dev libjasper-dev  (安装依赖库) 
mkdir my_build_dir (创建编译文件夹)
cd my_build_dir (进入文件夹进行配置)
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make

Enter the compilation process at this time, the process is relatively slow
and then enter the installation link:

代码顺序:
sudo make install
sudo gedit /etc/ld.so.conf.d/opencv.conf
在上一条命令执行后在弹出的文件末尾输入/usr/local/lib并保存  
sudo ldconfig 
sudo gedit /etc/bash.bashrc  
在上条命令执行后生成的文件最末尾输入PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH  并保存
source /etc/bash.bashrc  
sudo updatedb  

The opencv installation is now complete.

3. It is very possible to encounter such a problem during the installation of opencv. When installing the dependent library, a prompt message such as E: Unable to locate package libjasper-dev will appear. Will it not be possible to locate libjasper-dev so that the dependent library cannot be located? Normal installation and the last step of compiling is that the system cannot find the Makefile file and stops directly.
Solution:

此时直接依次执行如下命令:
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
sudo apt update
apt list --upgradable
sudo apt install libjasper1 libjasper-dev

This process is relatively slow, but basically effective. Re-experiment can compile successfully

2. Write the code for special effects display under opencv

1. Enter the opencv decompression package to create an experiment file, put the picture you want to edit into the folder, and then write the cpp code in the file
Insert picture description here
2. Compile the .cpp file, and run the executable file, you can get the result

编译命令:
g++ +文件名+ -o+输出文件流名称 +` 支持包
注意:这里是.cpp文件要用g++编译,gcc可能会出问题

The dynamic inventory contains the address of the link library, and the library file path is obtained during compilation.
Insert picture description here
result:
Insert picture description here

Note: Put the picture to be edited into this folder. If the picture is placed under the home directory, it is very likely that the picture will not be found when running the executable file. As a result, you will not get any results when you run the executable program at the end. The picture should be placed in the same directory where the program file is saved
Insert picture description here

3. Write a program code to open the camera to display and process video under opencv

1. Write the .cpp code and compile the file with the last g++ command

代码:
#include <opencv2/opencv.hpp>
using namespace cv;

int main()
{
    
    
VideoCapture capture(0);
while(1)
{
    
    
Mat frame;
capture >> frame;
imshow("读取视频帧",frame);
waitKey(30);
}
system("pause");
return 0;
}

Insert picture description here
You may not be able to open the camera, you need to set the usb controller, connect the camera to the virtual machine to open it, and open it every time you turn it on.

2.
1) If you are required to open a video file on your hard drive to play, how do you modify the code?
VideoCapture capture(0) Change the parameter 0 in this line to the name of the video file.
2) In the while loop on line 9, what data structure is Mat? Why is it necessary to add a waitKey delay code and delete it?
Mat is essentially a matrix, and the values ​​on the rows and columns of the matrix are its pixel values, which means that the program will wait for the user's key press event without limitation and cannot be deleted. This function will leave time for the image to be drawn, otherwise the window will become unresponsive. And the image cannot be displayed.
3) This code will always run in the while loop. If you try to close the image display window with the mouse, you will find that it can't be closed all the time. You need to use the keyboard Ctrl+C to forcibly interrupt the program, which is very unfriendly. How to improve?
Add after line 13 of the program

 if(frame.empty())
{
    
    
break;
}

Guess you like

Origin blog.csdn.net/rude_dragon/article/details/109303433