ubuntu16.04/18.04安装vscode和opencv3.4.6教程

文章目录

  • 一、Opencv3.4.6
    • 1.install
    • 2.test
  • 二、VScode
    • 1.下载
    • 2.配置
      • (1)安装c/c++插件(都必须有)
      • (2)建立工程(都必须有)
      • (3)配置launch.json(都必须有)
      • (4)配置tasks.json(不安装opencv的情况)
      • (5)配置tasks.json(安装opencv的情况)
      • (6)c_cpp_properties.json(安opencv的情况)
      • (7)快速查看已配置的文件
      • (8)测试一下
    • 3.参考:
    • 4.神奇的错误
  • 三、cmake编译


一、Opencv3.4.6

1.install

  1. 去官网下载opencv,在本教程中选用的时opencv3.4.6,其他版本的配置方法异曲同工。
    下载链接http://opencv.org/releases.html,选择sources版本

  2. 进入下载的路径,解压下载下来的zip包

cd XXX
unzip opencv-3.4.6.zip
  1. 安装一些东西(哪个不行就算了)
sudo apt-get update
sudo apt-get install vim
sudo apt-get install g++
sudo apt-get install gcc
sudo apt-get install cmake
sudo apt-get install build-essential
sudo apt-get install libgtk2.0-dev
sudo apt-get install libavcodec-dev
sudo apt-get install libavformat-dev
sudo apt-get install libswscale-dev 
sudo apt-get install libatlas-base-dev 
sudo apt-get install gfortran
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt-get update
sudo apt-get install libjasper1 libjasper-dev
  1. 进入到解压后的文件包中,创建编译文件夹
cd XXX/opencv-3.4.6
mkdir Release
cd Release
  1. CMAKE
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

如果以下错误:

qmake: could not exec '/usr/lib/x86_64-linux-gnu/qt4/bin/qmake': No such file     or         directory

执行以下指令:

sudo apt-get install qt5-default
  1. MAKE
sudo make
  1. 安装
sudo make install
  1. 执行完毕后OpenCV编译过程就结束了,接下来就需要配置一些OpenCV的编译环境首先将OpenCV的库添加到路径,从而可以让系统找到
sudo gedit /etc/ld.so.conf.d/opencv.conf 

执行此命令后打开的可能是一个空白的文件,不用管,只需要在文件末尾添加

/usr/local/lib  

ubuntu16.04/18.04安装vscode和opencv3.4.6教程_第1张图片

  1. 执行如下命令使得刚才的配置路径生效
sudo ldconfig  
  1. 配置bash
sudo gedit /etc/bash.bashrc  

在最末尾添加

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

保存,执行如下命令使得配置生效

source /etc/bash.bashrc  
  1. 更新
sudo updatedb  

2.test

cd到opencv-3.4.1/samples/cpp/example_cmake目录下
我们可以看到这个目录里官方已经给出了一个cmake的example我们可以拿来测试下

cd ..
cd samples
cd cpp
cd example_cmake
cmake .
make
./opencv_example

参考:
ubuntu16.04安装opencv3.4.1教程
Ubuntu16.04安装OpenCV3.4.2


二、VScode

1.下载

先下好gcc、g++

sudo apt-get update
sudo apt-get install gcc
sudo apt-get install g++

下载VScode:
图形推荐(一键式操作,第一推荐):在ubuntu18.04版本安装vscode
在线umake:Ubuntu 16.04 安装VSCode
本地deb(不会再有问题,第二推荐)
在官网里(https://code.visualstudio.com/Download)里下载Linux的.deb格式
然后安装

cd 文件存在的位置
sudo dpkg -i xxx.deb

2.配置

下面分为安opencv和不安的情况

(1)安装c/c++插件(都必须有)

首先通过左边栏的Extension在这里插入图片描述栏目安装C++插件

ubuntu16.04/18.04安装vscode和opencv3.4.6教程_第2张图片

(2)建立工程(都必须有)

由于VScode是以文件夹的形式管理工程的,因此我们首先新建一个文件夹

然后通过VScode打开此文件夹:File→Open Folder

新建main.cpp文件(Ctrl+N)并输入程序:

#include
using namespace std;
int main()
{
	cout<<"Hello World\n";
	return 0;
}

保存Ctrl+S,记住要写后缀(.cpp),不然
Ubuntu下VScode编译报错:File not recongnized不可识别的文件格式

(3)配置launch.json(都必须有)

点击左侧的Debug按钮选择添加配置(Add configuration),然后选择C++(GDB/LLDB),然后选个Default,将自动生成launch.json文件.
ubuntu16.04/18.04安装vscode和opencv3.4.6教程_第3张图片

{
    // 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": "${workspaceRoot}/${fileBasenameNoExtension}.main.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build"
        }
    ]
}

(4)配置tasks.json(不安装opencv的情况)

Ctrl+Shift+P打开面板

输入Task:Configure Default Build Task
(有的要输入Edit,再输入Task:Configure Default Build Task)
ubuntu16.04/18.04安装vscode和opencv3.4.6教程_第4张图片

{
    // 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}.main.out",
    ],// 编译命令参数
    "problemMatcher":{
        "owner": "cpp",
        "fileLocation":[
            "relative",
            "${workspaceFolder}"
        ],
        "pattern":[
            {
                "regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",
                "file": 1,
                "location": 2,
                "message": 3
            }
        ]
    },
    "group": {
        "kind": "build",
        "isDefault": true
    }
}

(5)配置tasks.json(安装opencv的情况)

Ctrl+Shift+P
输入Edit
选择Task:Configure Default Build Task
ubuntu16.04/18.04安装vscode和opencv3.4.6教程_第5张图片

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "-std=c++11",
                "${file}",
                "-o",
                "${fileBasenameNoExtension}.main.out",
			        "-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
            }
        }
    ]
}

(6)c_cpp_properties.json(安opencv的情况)

Ctrl+Shift+P
输入Edit
选择C/C++:Edit Configurations(JSON)
ubuntu16.04/18.04安装vscode和opencv3.4.6教程_第6张图片

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include",
                "/usr/local/include/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "/usr/bin/cpp",
            "cStandard": "c11",
            "cppStandard": "c++11",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

(7)快速查看已配置的文件

左侧Explorer→MAIN→.vscode
ubuntu16.04/18.04安装vscode和opencv3.4.6教程_第7张图片

(8)测试一下

ubuntu16.04/18.04安装vscode和opencv3.4.6教程_第8张图片

Try.cpp

#include
using namespace cv;
int main()
{
    
    Mat srcImage=imread("1.jpg");
    imshow("Origin",srcImage);
    waitKey(0);
    return 0;
}

1.jpg

ubuntu16.04/18.04安装vscode和opencv3.4.6教程_第9张图片

效果

ubuntu16.04/18.04安装vscode和opencv3.4.6教程_第10张图片

3.参考:

https://blog.csdn.net/weixin_43374723/article/details/84064644#commentBox
https://blog.csdn.net/zzz_xxj/article/details/86568353

4.神奇的错误

  1. 检查代码的头文件,符号(; 、(、))
  2. Ubuntu下VScode编译报错:File not recongnized不可识别的文件格式:https://blog.csdn.net/sandalphon4869/article/details/94410509
  3. Ubuntu下编译错误:Unable to open ’raise.c‘:
    https://blog.csdn.net/sandalphon4869/article/details/94414403

三、cmake编译

Linux 使用cmake构建OpenCV项目
Ubuntu18.04下安装配置VScode以及VS的使用总结

你可能感兴趣的:(opencv(C++),linux环境下配置)