提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
如何在utunbun20.04系统下链接opencv库进行多文件的编译和调试
提示:以下是本篇文章正文内容,下面案例可供参考
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/**",
"/usr/local/include/**"#这两行一定要确保opencv4位置在这里
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
1.launch.json如何建立
点击从上向下数第四个按钮,之后选择建立launch.json文件
2.launch.json文件模板
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/testopencv.exe",#这里是生成可执行文件的目录位置
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",#这里确保一定为当前文件目录
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Build",#这里和task.json文件最后label按顺序执行后的总执行步骤的名称相对应,如果不用调试task文件,这里可以不用管
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build"#Makefile文件所在位置
},
"tasks": [
{
"label": "echo",
"type": "shell",
"args": [
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileBasenameNoExtension}.o",
"-I",
"/usr/local/include",
"-I",
"/usr/local/include/opencv",
"-I",
"/usr/local/include/opencv2",
"-L",
"/usr/local/lib",
"-l",
"opencv_core",
"-l",
"opencv_imgproc",
"-l",
"opencv_imgcodecs",
"-l",
"opencv_video",
"-l",
"opencv_ml",
"-l",
"opencv_highgui",
"-l",
"opencv_objdetect",
"-l",
"opencv_flann",
"-l",
"opencv_imgcodecs",
"-l",
"opencv_photo",
"-l",
"opencv_videoio"#opencv库的包含
]
},
{
"type": "shell",
"label": "cmake",
"command": "cmake",
"args": [
".."
]
},
{
"label": "make",
"group": {
"kind": "build",
"isDefault": true
},
"command": "make",
"args": [],
"problemMatcher": []
},
{
"label": "Build",#这里是依次执行camke.. ,make用于调试的时候不需要再次输入,与 launch.json 的preLaunchTask相对应
"dependsOn": [
"sequence",
"cmake",
"make"
]
}
],
}
1.setting.json文件的建立
选择程序的opencv所在的头文件点击转入定义,正常转入,setting.json文件自动建立
{
"files.associations": {
"iostream": "cpp"
}
}
cmake_minimum_required(VERSION 3.16)
project(testopencv)#工程的名称
set(CMAKE_BUILD_TYPE "Debug")#调试可以命中断点,这句话必须有,否则不能调试
find_package(OpenCV REQUIRED)#链接到opencv库,必须是上文所示的路径,不然要更改为其他方式指定路径
include_directories(${OpenCV_INCLUDE_DIRS})#链接默认路径的opencv库,这里也是其他头文件包含的路径,若有其他头文括号里这样写(${CMAKE_SOURCE_DIR}/头文件所在文件夹名)
add_executable(testopencv.exe new.cpp)#第一个变量是生成可执行文件的名称,第二个是用到的cpp,如果有多个继续往后写就可以。包含在其他文件夹下的空格后“文件夹名称/cpp文件名称”
target_link_libraries(testopencv.exe ${OpenCV_LIBS})#重新链接回opencv库,第一个变量是生成的可执行文件的名称
#include
#include
int main(int argc, char **argv)
{
cv::Mat image;
image = cv::imread("/home/star/code/project/a.png", cv::IMREAD_COLOR);
if (!image.data)
{
std::cout << "Could not open file" << std::endl;
return -1;
}
cv::namedWindow("namba image", cv::WINDOW_AUTOSIZE);
cv::imshow("namba image", image);
cv::waitKey(0);
int a = 10;
std::cout << "a=" << a << std::endl;
return 0;
}
按照上述操作,可以实现对new.cpp文件的执行和调试。
ps:这是本人自己的学习笔记,借鉴了很多博主,具体哪一个记不太清楚了,如果有发现的可以提醒我,我会及时标注。语言所用的是c++,中间有些不太对的也可以提醒我,对于四个配置文件的理解我也不是很透彻。