ubuntu16.04编译安装c++ opencv与vscode配置debug
生活随笔
收集整理的這篇文章主要介紹了
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-dev3.執行命令
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);};#endifa.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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在状态栏中显示鼠标位置坐标
- 下一篇: MFC中文件打开与保存