欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

ubuntu16.04编译安装c++ opencv与vscode配置debug

发布时间:2024/7/23 39 豆豆
生活随笔 收集整理的这篇文章主要介绍了 ubuntu16.04编译安装c++ opencv与vscode配置debug 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

一.编译安装c++ opencv

1.下载zip包

本文安装的是opencv3.4.3,下载链接,以Sources方式下载zip包.

2.安装依赖

sudo apt-get install build-essential sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

3.执行命令

cd ~/opencv-3.4.3 # 进入opencv文件夹 mkdir build # 创建build文件夹 cd build # 进入build文件夹#cmake指令,如果没有特殊要求建议就选择默认的就可以 cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local .. #如果安装anconda遇到Makefile:160: recipe for target 'all' failed的报错,改为执行以下 cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_SHARED_LIBS=OFF -D WITH_OPENMP=ON -D ENABLE_PRECOMPILED_HEADERS=OFF ..make -j7 # 多线程执行make任务# 最后一步,安装库文件 sudo make install#安装完成

如果没报错说明安装成功.

pkg-config --modversion opencv

查看版本号

二.vscode配置进行debug

更详细的配置,参考这篇文章

1.代码

main.cpp

#include<opencv2/opencv.hpp> #include<iostream> #include<string> #include"a.h" using namespace std; int main(){ cout<<"CV_VERSION"<<CV_VERSION<<endl;const char* imgPath = "/home/fzh/AI/C_learn/datastruct/opencv/test.jpg";cv::Mat img = cv::imread(imgPath);cout<<"==main img.cols=="<<img.cols<<endl;cout<< "==main img.rows=="<<img.rows<<endl; int a = 1;int b = 1;int c; c = a + b;cout<<"=====hahhahhahhah===="<<endl;cout<<"===c=:=="<<c<<endl;A *A1 = new A();A1->readImg(imgPath);delete A1;A1 = nullptr;return 0; }

a.h

#ifndef A_H_ #define A_H_class A {public:A(){}~A(){}void readImg(const char* path);};#endif

a.cpp

#include<opencv2/opencv.hpp> #include <iostream> #include "a.h"using namespace std; void A::readImg(const char* path) {cv::Mat img = cv::imread(path);cout<<"==A img.cols=="<<img.cols<<endl;cout<< "==A img.rows=="<<img.rows<<endl;}

CMakeLists.txt 

cmake_minimum_required(VERSION 2.6)project(opencv)add_definitions(-std=c++11)set(CMAKE_CXX_STANDARD 11) set(CMAKE_BUILD_TYPE Debug)set(OpenCV_DIR "/usr/local/include") find_package(OpenCV 3.4.3 REQUIRED PATHS "/usr/local") include_directories(OpenCV_INCLUDE_DIRS) # set(OpenCV_DIR "/usr/include") # find_package(OpenCV 2.4.9.1 REQUIRED PATHS "/usr/include") # include_directories(OpenCV_INCLUDE_DIRS)add_executable(main ${PROJECT_SOURCE_DIR}/main.cpp a.cpp) target_link_libraries(main ${OpenCV_LIBS})add_definitions(-O2 -pthread)

2.vscode配置

tasks.json

{"tasks": [{"type": "shell","label": "C/C++: g++ build active file","command": "g++","args": [// "-g", "-std=c++11", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}","-g", "-std=c++11", "${fileDirname}/*.cpp", "-o", "${fileDirname}/${fileBasenameNoExtension}",// "-I", "/usr/include",// "-I", "/usr/include/opencv",// "-I", "/usr/include/opencv2","-I", "/usr/local/include","-I", "/usr/local/include/opencv","-I", "/usr/local/include/opencv2","-L", "/usr/local/lib","-l", "opencv_imgcodecs","-l", "opencv_imgproc", "-l", "opencv_dnn","-l", "opencv_features2d","-l", "opencv_flann","-l", "opencv_highgui", "-l", "opencv_imgproc","-l", "opencv_ml","-l", "opencv_objdetect","-l", "opencv_photo","-l", "opencv_shape","-l", "opencv_stitching","-l", "opencv_superres","-l", "opencv_videoio","-l", "opencv_video","-l", "opencv_videostab","-l", "opencv_core"],"options": {"cwd": "${workspaceFolder}"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true}}],"version": "2.0.0" }

launch.json

{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "g++ - Build and debug active file","type": "cppdbg","request": "launch","program": "${fileDirname}/${fileBasenameNoExtension}","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "C/C++: g++ build active file","miDebuggerPath": "/usr/bin/gdb"}] }

c_cpp_properties.json

{"configurations": [{"name": "Linux","includePath": ["${workspaceFolder}/**",// "/usr/include/opencv2",// "/usr/include/opencv",// "/usr/include","/usr/local/include/opencv2","/usr/local/include/opencv","/usr/local/include"],"defines": [],"compilerPath": "/usr/bin/gcc","cStandard": "gnu11","cppStandard": "c++17","intelliSenseMode": "gcc-x64"}],"version": 4 }

3.出现一个bug

在main.cpp设置断点下按f5,开始debug

这里最开始出现一个bug,ImportError:lib***.so--cannot open shared object file: No such file or directory

通过设定软链接方式解决的:

3.1 找到文件

     find  /  -name  lib**.so   (缺失的动态链接库)  

3.2 建立软链接

     ln -s  /path/to/lib**.so   /usr/lib

 3.3 sudo ldconfig

总结

以上是生活随笔为你收集整理的ubuntu16.04编译安装c++ opencv与vscode配置debug的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。