Install and Use OpenCV on Linux

Build & Install

My enviroment:

  • Architecture: x86_64

  • OS: Ubuntu 22.04 LTS

  • Editor: VS Code 1.71.0

  • Compiler: gcc 11.2.0

  • Build Tool: CMake 3.22.1

Simply follow the steps in OpenCV Tutorials to install OpenCV on Linux.

I use VS Code as my editor while CMake and gcc to build and compile the program. VSCode support IntelliSense for cross-compiling, but if you need code completions for opencv (and no include path error when you use opencv!) in your VS Code, some configuration is required.

opencv4 was installed to /usr/local/ by default, and the headers are in /usr/local/include/opencv4 from OpenCV Tutorials. Use ls /usr/local/include/opencv4 to see here is only one directory opencv2 in it.

Press Ctrl+Shift+P to open the search box, then enter and choose C/C++: Edit Configuration (JSON), the editor will open .vscode/c_cpp_properties.json in current directory. Add the include path of opencv4 to your "includePath":

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include/opencv4"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "linux-gcc-x64",
            "configurationProvider": "ms-vscode.makefile-tools"
        }
    ],
    "version": 4
}

Now you can use #include <opecv2/...> in your code.

Last updated