OpenCV is a library that I will be working with in C++, so this blog is going to be dedicated to installing OpenCV and setting it up for C++. The steps I will be following are the ones found on the linux tutorial section of the OpenCV website.

To start, we need to install the following dependancies.

sudo apt install build-essential \
                 cmake \
                 git \
                 libgtk2.0-dev \
                 pkg-config \
                 libavcodec-dev \
                 libavformat-dev \
                 libswscale-dev \
                 python3-dev \
                 python3-numpy \
                 libtbb2 \
                 libtbb-dev \
                 libjasper-dev \
                 libjpeg-dev \
                 libpng-dev \
                 libtiff-dev \

Next to clone the OpenCV and OpenCV contrib git repositories via

git clone and

git clone

To simplfy things I have made a directory called opencv_dev, and cloned the repositories inside of it.

Now to create a directory called build inside of the opencv directory and cd into that new directory.

Within build we now run the following command.

      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules 
      -D WITH_V4L=ON \
      -D WITH_QT=OFF \
      -D WITH_TBB=ON \
      -D WITH_CUDA=ON \

The next command to run is make -j7, then sudo make install.

With that I now have the ability to use #include <opencv2/opencv.hpp> in the following example.

// main.cpp
#include <opencv2/opencv.hpp>

using namespace cv;

int main(int argc, char const *argv[])
    if (argc < 2) {
        printf("Usage: ./main <image_path>\n");
        return -1;

    Mat img;
    img = imread(argv[1], 1);

    if (! {
        printf("No image data\n");
        return -1;

    namedWindow("Image", WINDOW_AUTOSIZE);
    imshow("Image", img);


    return 0;

Then to make the CMakeLists.txt file, which is as follows.

cmake_minimum_required(VERSION 3.10)
project( main )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} )

Because the above is taken from the gcc and cmake tutorial, I want to cover each line of the CMakeLists.txt file. This is mainly for my personal learning, but will be beneficial if the reader happens to also be learning.

The first line cmake_minimum_required(VERSION 3.10), sets the minimum required version of cmake for a project and update policy settings to match the version given. In my case, 3.10 is specified since that is the installed version of cmake on my machine. [source]

The second line project( main ), sets a name, version, and enables languages for the entire project. [source]

The third line find_package( OpenCV REQUIRED ), loads settings for an external project. [source]

The fourth line include_directories( ${OpenCV_INCLUDE_DIRS} ), adds include directories to the build. [source]

The fifth line add_executable( main main.cpp ), adds an executable to the project using the specified source files. [source]

The sixth and final line target_link_libraries( main ${OpenCV_LIBS} ), specify libraries or flags to use when linking a given target and/or its dependents. [source]

Finally to build/compile via mkdir build && cd build, cmake .., make, then ./main os.jpeg.

We got the os.jpeg picture to open; OpenCV with C++ works!