C/C++教程

Ubuntu20.04,在Vscode下配置C/C++和Opencv

本文主要是介绍Ubuntu20.04,在Vscode下配置C/C++和Opencv,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Ubuntu20.04,在Vscode下配置C/C++和Opencv

参考文章 Ubuntu 18.04系统下的环境配置vscode&&opencv&&c++

Opencv的安装

1.准备工作

ubuntu20.04
cmake>=3.10.2
unzip
mlocate包

更新包

sudo apt update
sudo apt upgrade

简单安装cmake

sudo apt install cmake

此安装方式安装的cmake可能不是最新的
查看cmake版本

cmake -version

安装mlocate

sudo apt-get install mlocate

2.编译

安装依赖

sudo apt install openssl libssl-dev mingetty zip unzip git curl vim gcc g++ -y
sudo apt install socat zsh curl git vim make cmake -y
sudo apt install autojump -y
sudo apt-get install -y libtiff5-dev libeigen3-dev
sudo apt install -y git gcc g++ make cmake libgtk2.0-dev libgoogle-glog-dev
sudo apt install -y libtesseract-dev libgtkglext1 libgtkglext1-dev ffmpeg
sudo apt install -y libv4l-0 libv4l-dev
sudo apt install -y liblapacke-dev libopenblas-dev libgtk-3-dev libgstreamer-plugins-base1.0-dev
sudo apt install -y libgstreamer1.0-dev libavresample-dev libgphoto2-dev libdc1394-22-dev
sudo apt-get install -y build-essential make cmake cmake-curses-gui git g++ pkg-config curl
sudo apt-get install -y libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libeigen3-dev libglew-dev libgtk2.0-dev
sudo apt-get install -y libtbb2 libtbb-dev v4l-utils qv4l2 v4l2ucp
sudo apt-get install -y libdc1394-22-dev libxine2-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y libjpeg8-dev libjpeg-turbo8-dev libtiff-dev libpng-dev
sudo apt-get install -y libxvidcore-dev libx264-dev libgtk-3-dev
sudo apt-get install -y libatlas-base-dev libopenblas-dev liblapack-dev liblapacke-dev gfortran
sudo apt-get install -y qt5-default
sudo apt-get install -y python3-dev python3-testresources
sudo apt-get install -y libcanberra-gtk-module

下载源代码

选择合适的opencv版本,contrib库版本需与opencv版本相同,以目前使用的3.4.7为例,下载zip压缩包
opencv3.4.7
OpenCV-Contrib
使用shell将压缩包下载到文件夹内,需要将[version_id]改为需要下载的版本号

cd ~
wget -O opencv.zip https://github.com/opencv/opencv/archive/[version_id].zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/[version_id].zip
unzip opencv.zip
unzip opencv_contrib.zip
mv opencv-[version_id] opencv
mv opencv_contrib-[version_id] opencv_contrib

比如我下载的是3.4.7版本,那么

cd ~
wget -O opencv.zip https://github.com/opencv/opencv/archive/3.4.7.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.4.7.zip
unzip opencv.zip
unzip opencv_contrib.zip
mv opencv-[version_id] opencv
mv opencv_contrib-[version_id] opencv_contrib

配置cmake并编译OpenCV

cd ~/opencv
mkdir build
cd build

Cmake编译命令

-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \此处路径必须填写正确,否则编译测试通过后代码仍然可能编译报错,缺少xxx文件

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D ENABLE_NEON=OFF \
-D ENABLE_FAST_MATH=ON \
-D WITH_GSTREAMER=ON \
-D WITH_LIBV4L=ON \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_EXAMPLES=OFF \
-D WITH_TBB=ON \
-D BUILD_opencv_world=OFF \
-D BUILD_opencv_xfeatures2d=OFF \
-D WITH_OPENGL=ON \
-D WITH_GTK_2_X=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D EIGEN_INCLUDE_PATH="/usr/include/eigen3" \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D WITH_XINE=ON \
-D WITH_GDAL=ON \
..
make -j8

此处8为最大支持线程数,一般为cpu数两倍,一般情况下为4核8线程,线程数过多时可能减低编译效率,设置导致编译报错。

make时如看到

$ make
make: * No targets specified and no makefile found. Stop.

此时问题为Cmake出现问题,请查看Cmake报错信息

3.安装

编译成功后

sudo make install
sudo ldconfig
sudo vim /etc/bash.bashrc

在末尾添加,此处是vim编辑器,如果不会使用请百度“如何在vim末尾添加”“vim编辑器如何保存退出”

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

保存退出

source /etc/bash.bashrc
sudo updatedb

配置生效
查看Linux opencv安装库

pkg-config opencv --libs

查看安装版本

pkg-config opencv --modversion

4. vscode的配置

下载和安装vscode


注意下载C/C++插件

vscode环境配置

创建一个文件夹,作为我们的工作文件夹,之后的opencv代码需要在此文件夹中运行

launch.json

这个文件主要是配置debug,具体设置如下:

{
    // 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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.o",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "preLaunchTask": "g++",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /bin/gdb -q --interpreter=mi",
        },
    ]
}

task.json

一般情况下,在上一步运行失败之后,会直接提示你设置task.json文件,如果没有,你也可以点击Ctrl+Shift+P"调出命令面板,然后搜索Task,点击第一个

Tasks:Configure Default Build Task

会提示你依据模板创建task.json,这里不管什么模板,随便选一个,然后直接复制粘贴以下设置代码:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "command": "g++",
    "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"
    ],// 编译命令参数
    "problemMatcher":{
        "owner": "cpp",
        "fileLocation":[
            "relative",
            "${workspaceFolder}"
        ],
        "pattern":[
            {
                "regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",
                "file": 1,
                "location": 2,
                "message": 3
            }
        ]
    },
    "group": {
        "kind": "build",
        "isDefault": true
    }
}

里面有很多参数是适配opencv的

OpenCV 编译环境配置

配置opencv,其实主要就解决两个问题,一个是头文件包含,一个是库文件搜索。头文件包含在c_cpp_properties.json中解决,库文件包含则要继续修改task.json中的编译命令。首先来看c_cpp_properties.json:

c_cpp_properties.json

怎么打开这个文件呢,两种方式,一个就是打开命令面板(还记得那个快捷键吗?我一直记,一直忘),然后搜索cpp: Edit,点击结果就可以进入这一文件。
另一种就是在自己的cpp文件里直接包含opencv库,聪明的cpp扩展会给你一个小提示提醒你打开c_cpp_properties.json设置头文件包含。
不说废话:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include", //请确保你的opencv opencv2头文件夹安装在这个目录
                "/usr/include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

测试代码

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
	Mat img = imread("xxxx"); //xxxx为图片路径,绝对路径
	imshow("test", img);
	waitKey(0);
	return 0;
}

随便找一张图片,放在某个路径然后替换imread函数的参数
运行后,我弹出报错了

提示我 无法打开"raise.c":无法读取文件.........
需要安装glibc-source
在终端中输入

$ sudo apt install glibc-source

然后在launch.json中的"configurations"下添加

"sourceFileMap": {
    "/build/glibc-YYA7BZ": "/usr/src/glibc"
}

其中"YYA7BZ"要改为报错信息中显示的glibc后缀
比如我的是

"sourceFileMap": {
    "/build/glibc-eX1tMB": "/usr/src/glibc"
},

这个要根据你们弹出的错误去看
然后发现成功运行!

这篇关于Ubuntu20.04,在Vscode下配置C/C++和Opencv的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!