目录
- 1.新增配置文件
- (1)c_cpp_properties.json
- (2)files.associations
- (3)tasks.json
- (4)CMakeLists.txt
- 2.断点调试
1.新增配置文件
VS Code的配置文件一般是指特定目录下的JSON文件。所谓JSON是一种文本格式,用于轻量化地存储一组{键: 值}信息,诸如编译器的路径、调试器的路径和有关选项等。
我们这里所做的调试配置放在当前工作文件夹下的.vscode子目录中,具有最高优先级,出错了想从头来直接删除里面的配置文件即可。
这里新增的配置文件有:
(1)c_cpp_properties.json
这里是cpp的配置选项,主要用于配置头文件路径:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/c++/8",
"/usr/local/opencv/include/opencv4/opencv2/",
"/usr/local/opencv/include/opencv4"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
(2)files.associations
将某种特殊文件类型关联到vscode已支持的文件类型
{
"files.associations": {
"*.cu": "cuda-cpp",
"*.cpp": "cpp",
"deque": "cpp",
"string": "cpp",
"vector": "cpp",
"*.tcc": "cpp",
"__hash_table": "cpp",
"__split_buffer": "cpp",
"__tree": "cpp",
"array": "cpp",
"bitset": "cpp",
"initializer_list": "cpp",
"iterator": "cpp",
"map": "cpp",
"queue": "cpp",
"random": "cpp",
"set": "cpp",
"stack": "cpp",
"string_view": "cpp",
"unordered_map": "cpp",
"utility": "cpp",
"__atomic": "cpp",
"__functional_base": "cpp",
"__functional_base_03": "cpp",
"__tuple": "cpp",
"algorithm": "cpp",
"chrono": "cpp",
"type_traits": "cpp",
"filesystem": "cpp",
"functional": "cpp",
"limits": "cpp",
"memory": "cpp",
"ratio": "cpp",
"tuple": "cpp",
"istream": "cpp",
"ostream": "cpp"
}
}
(3)tasks.json
编译用,调试程序将在编译好的debug程序上进行调试
这里其实是执行了make -j6
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "make -j6"
},
]
}
(4)CMakeLists.txt
编写完后使用make命令产生makefile,供(3)进行make编译.
这里特别注意:#添加-g 参数以支持带有gdb调试信息,否则无法下断点
#设置CMAKE最`在这里插入代码片`低版本
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.3)
MESSAGE(STATUS "start!")
#设置项目名称
SET(PROJECT_NAME OpenCV_test)
#这里添加刚刚安装的opencv的目录,需要包含OpenCVConfig.cmake
SET(OpenCV_DIR /usr/local/opencv/lib64/cmake/opencv4)
#建立项目
PROJECT(${PROJECT_NAME})
#寻找OpenCV库
FIND_PACKAGE(OpenCV REQUIRED)
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
#打印调试信息
MESSAGE(STATUS "Project: ${PROJECT_NAME}")
MESSAGE(STATUS "OpenCV library status:")
MESSAGE(STATUS " version: ${OpenCV_VERSION}")
MESSAGE(STATUS " libraries: ${OpenCV_LIBS}")
MESSAGE(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
#添加-g 参数以支持带有gdb调试信息
set(CMAKE_CXX_FLAGS "-g")
set(CMAKE_C_FLAGS "-g")
#获取代码,在项目中,将所有代码都放在src文件夹中
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
MESSAGE(STATUS "Src file: ${DIR_SRCS}")
#编译可执行程序
ADD_EXECUTABLE(${PROJECT_NAME} ${DIR_SRCS})
#添加链接库
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS})
(5)launch.json
调试配置文件,用于指定调试用的环境等信息
{
"version": "0.2.0",
"configurations": [
{
"name": "program-debug",
"type": "cppdbg",
"request": "launch",
//待调试的程序
"program": "${workspaceFolder}/OpenCV_test",
"args": [],
"stopAtEntry": false,
//工作目录,用于寻找文件及图片等
"cwd": "${workspaceFolder}",
//配置的环境变量,相当于使用一次export命令,这里没有实际使用
"environment": [
{"name": "workdir", "value": "workspace"}
],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
//每次进行调试前先进行编译操作:
"preLaunchTask": "build"
}
]
}
对于"miDebuggerPath",需要安装调试工具gdb,并填写调试路径:
安装调试工具:
yum install gdb
查看gdb安装的所在路径:
rpm -ql gdb
上述工作准备完后,有:
2.断点调试
在cpp文件内加断点:
点选调试按钮,选择
选择上图蓝色选项,执行编译并调试。
断点进入成功。