ubuntu18.04 配置vscode的C++/C环境+opencv

目录

一、安装vscode

二、安装C/C++插件

三、配置调试和编译文件

3.1 tasks.json文件----生成out文件

3.2 launch.json文件----执行out文件

四 执行

五 OPENCV编译环境

5.1 配置依赖

5.2 配置opencv

5.2 配置vscode环境

5.2.1 c_cpp_properties.json

5.2.2 task.josn

六 添加头文件路径

6.1 c_cpp_properties.json

6.2 task.josn

七 各种报错

7.1  缺少libjasper.so.1

7.2 编译出错/bin: error while loading shared libraries: libopencv_highgui.so.3.2: cannot open


先说下 win上我没配置成功。只配置成功linux了。

 

一、安装vscode

我使用anaconda安装的,可以参考vscode的主页,有安装方式。

二、安装C/C++插件

按Ctrl + shift + X,搜索C/C++并安装。安装它就行。

ubuntu18.04 配置vscode的C++/C环境+opencv_第1张图片

三、配置调试和编译文件

3.1 tasks.json文件----生成out文件

为了方便在VScode里编译C++代码,我们可以将类似g++ -g main.cpp等g++命令写入VScode的任务系统。

按ctrl+shift+p打开命令行,输入Tasks: Run task,回车!!!,会出现如下提示:

    No task to run found. configure tasks...

再回车,

    Create tasks.json file from template

然后选择:

   Others Example to run an arbitrary external command.

生成默认的tasks.json文件如下:

{
    // 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", "${file}", "-o", "${fileBasenameNoExtension}.out"]
        }
    ]
}

这里的label为任务名,我们将”label"= "echo"改为”label"= "build"。
由于我们的指令是g++,这里将”command“=”echo Hello“改为”command“=”g++“。
然后添加g++的参数args。如果我们的g++指令为:g++ -g main.cpp,这里可以把参数设置为如下:

3.2 launch.json文件----执行out文件

选中文件夹/cpp文件,按Ctrl+shift+D,点击左侧的Debug按钮,选择添加配置(Add configuration),然后选择C++(GDB/LLDB)回车!!将自动生成launch.json文件,修改内容如下:

            "program": "enter program name, for example ${workspaceFolder}/a.out",

改为

            "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",

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

"preLaunchTask":"build",这个参数必须有,否则会报错,在debug的时候,说lauch:file.out文件不存在。

四 执行

F5

五 OPENCV编译环境

5.1 配置依赖

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

5.2 配置opencv

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

unzip opencv-4.1.0.zip
cd ~/Download/opencv-4.1.0

# 编译opencv  
cd ~/Download/opencv-4.1.0
mkdir build
cd build
cmake ..
make
# 下一步要切换到root用户,因为要修改一些东西。
su
sudo make install

5.2 配置vscode环境

5.2.1 c_cpp_properties.json

按ctrl+shift+p,输入edit,选择C/C++:edit configuration(json),会弹出c_cpp_properties.json。他是配置外部环境的。

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include/opencv4",
                "/usr/include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}
修改它
"includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include/opencv4", //请确保你的opencv opencv4头文件夹安装在这个目录  opencv4版本中 文件路径与其他版本有所不同

5.2.2 task.josn

tasks.json里面修改的是参数问题,我还没试试那个好使

{
    // 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}.out",// 设置动态链接库
                "-I", "/usr/local/include",
                "-I", "/usr/local/include/opencv4",
                "-I", "/usr/local/include/opencv4/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"
            ],
        }
    ]
}
  "-I", "/usr/local/include/opencv4",
  "-I", "/usr/local/include/opencv4/opencv2",
要注意args中的这两个

六 添加头文件路径

6.1 c_cpp_properties.json

"configurations": [{

            "name": "Linux",

            "includePath": [

                "${workspaceFolder}/**",

                "/usr/local/include/opencv4",

                "/usr/include",

                "${workspaceFolder}/scoure"  // 他是我的头文件路径 添加这句话

            ],...

6.2 task.josn

"args": [

                "-g", "-std=c++11", "${file}", "-o", "${fileBasenameNoExtension}.out",// 设置动态链接库

                "-I", "/usr/local/include",

                "-l","${workspaceFolder}/scoure", // 他是我的头文件路径 添加这句话

 

七 各种报错

7.1  缺少libjasper.so.1

缺少libjasper.so.1,有啥办法,安装呗。
命令如下:

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

7.2 编译出错/bin: error while loading shared libraries: libopencv_highgui.so.3.2: cannot open

转载 : https://blog.csdn.net/cv_you/article/details/77341631

首先给出错误截图;
这里写图片描述
下面转载一个很好的解决方案。操作正确,是可以解决的。
在ubuntu 下用Qt写OpenCV的程序运行出现error while loading shared libraries: libopencv_highgui.so.2.4 错误,是opencv的库路径没有设置好。

接下来要给系统加入opencv库的环境变量(也就是库存放的路径,注意不是解压路径)

  1. 第一步

    用gedit打开/etc/ld.so.conf,注意要用sudo打开获得权限,不然无法修改,
    如:sudo gedit /etc/ld.so.conf,在文件中加上一行 /usr/loacal/lib,/user/loacal是opencv安装路径 就是makefile中指定的安装路径
    加了之后的变为
    这里写图片描述

  2. 第二步

    再运行sudo ldconfig,
    修改bash.bashrc文件,sudo gedit /etc/bash.bashrc
    在文件末尾加入:
    PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
    export PKG_CONFIG_PATH
    加后的图:
    这里写图片描述

  3. 第三步

然后source /etc/bash.bashrc

可以正常运行了

转载 :https://www.jianshu.com/p/b308d3bbde8a

下面方法好使,我是7.3.1没有做软链接。

当出现/sbin/ldconfig.real: /usr/local/cuda/lib64/libcudnn.so.7 is not a symbolic link错误时,如upgrade出现错误:

upgrade error.png

  1. check the link:

sudo ldconfig -v

sudo ldconfig -v.png

  1. Find the error:
    /usr/local/cuda/lib64/libcudnn.so.7 is not a symbolic link
    libcudnn.so.7 -> libcudnn.so.7.0.5

  2. Create the new link manually:
    sudo ln -sf /usr/local/cuda/lib64/libcudnn.so.7.0.5 /usr/local/cuda/lib64/libcudnn.so.7

7.4 提示:errorE: unable to locate libjasper-dev

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

 

 

 

 

你可能感兴趣的:(c/cpp学习)