vscode configures OpenCV compilation environment (ubuntu)

   It is necessary to understand the internal compilation logic, not the superficial phenomenon. There will be many such problems in the future, and it is impossible to rummage through the box every time. What needs to be done is to face it calmly, analyze carefully, and find out the root of the problem.
    Not much to say, directly give the specific method of configuration, this is a general version, as long as you are using the ubutu system, and the C++ version of opencv is basically installed, you can basically use this method to achieve.
    1 step: First create a new folder, create a cpp file under this file, and then use vscode to open the folder.
    2 step: Use ctrl+shift+p to open the command line, look for launch.json, and configure the file as follows after finding it.

Install opencv3.4 on ubuntu16.04

1. Go to the official website to download opencv. Opencv3.4.0 is used in this tutorial. The configuration methods of other versions are similar. 
Download link http://opencv.org/releases.html , select sources version

2. Unzip the downloaded zip package. If you have a graphical interface, right-click the file directly, and there will be an option to decompress.

3. Enter the decompressed file package

4. Install dependent libraries and cmake. If prompted that apt-get update is required, first sudo su to enter root privileges (please use Baidu to create a super user), then sudo apt-get update, and then execute the following command

sudo apt-get install cmake
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev

5. Execute the command after installing cmake to create a compilation folder. If it is not created, it will prompt (as shown below) 
In-source builds are not allowed.

mkdir my_build_dir 
cd my_build_dir

6. cmake (in fact, -D and so on are optional. If you want to know what it does, please refer to the blog: https://blog.csdn.net/u013066730/article/details/79411730 )

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

Note: If it has been compiled in a new folder, but the previous error still occurs, delete cmakecache.txt and compile again

7. Execute the command, long compilation process

sudo make

8. Execute the command

sudo make install

9. After the execution of sudo make install, the OpenCV compilation process is over. Next, you need to configure some OpenCV compilation environments. First, add the OpenCV library to the path, so that the system can find

sudo gedit /etc/ld.so.conf.d/opencv.conf

After executing this command, a blank file may be opened. Don't worry about it. Just add

/usr/local/lib 

10. Execute the following command to make the configuration path just take effect

sudo ldconfig 

11. Configure bash

sudo gedit /etc/bash.bashrc

Add at the end

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

Save and execute the following command to make the configuration take effect.

source /etc/bash.bashrc 

renew

sudo updatedb 

12. So far all configurations have been completed. 
Let’s test it with a small program.

Find 
the cd to the opencv-3.4.0/smaples/cpp/example_cmake directory, 
we can see that the official has given a cmake example in this directory, we can use it to test and 
execute in order

cmake . 
make 
./opencv_example

You can see that the camera is turned on, and there is a hello opencv in the upper left corner, 
which means the configuration is successful.

OpenCV compilation environment configuration

Configuring opencv, in fact, mainly solves two problems, one is header file inclusion, and the other is library file search. The header file is included in c_cpp_properties.json, and the library file is included to continue to modify the compilation command in task.json.
First look at c_cpp_properties.json :

c_cpp_properties.json

How to open this file? There are two ways. One is to open the command panel (remember that shortcut key? I keep remembering it and forget it), then search for cpp: Edit, click on the result to enter this file.
The other is to directly include the opencv library in your own cpp file. The smart cpp extension will give you a small prompt to remind you to open c_cpp_properties.json to set the header file inclusion.
No nonsense:

{
    "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
}

task.json

The previous step solved the problem of header file inclusion. Here we need to solve the library file search. It is also simple. Just modify the compilation command directly, which is the args parameter in task.json:

"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"
    ],// 编译命令参数

Among them, -I indicates the header file directory, -L indicates the library file directory, and -l indicates the library file.

 


reference link:

https://blog.csdn.net/u013066730/article/details/79411767

https://www.jianshu.com/p/6d3f4a30945d
https://blog.csdn.net/m0_37975258/article/details/101467438

Guess you like

Origin blog.csdn.net/Fan0920/article/details/108284778