Imshow() throws: The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support

I have installed opencv from scratch and included opencv contrib to extra modules path. After this, I wrote basic script to read and show an image and compiled the code using cmake. The compilation was successful but when I tried to run the executable, I get following error:

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.7.0-dev) /home/dushyant/Documents/cpp/opencv_scratch/opencv-4.x/modules/highgui/src/window.cpp:1272: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'

Aborted (core dumped)

My code is as follows:

#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/features2d.hpp>
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;
using namespace xfeatures2d;

int main(int argc, char** argv)
    Mat img1, img2;
    img1 = imread("../1.jpg");
    img2 = imread("../2.jpg");
    imshow("Keypoints", img1);

My CMakeList.txt is as follows:

cmake_minimum_required(VERSION 3.8)

find_package(OpenCV REQUIRED)

    message("xfeatures2d found")
    message("xfeatures2d not found")
add_executable(DisplayImage DisplayImage.cpp)
add_executable(Pano pano.cpp)
add_executable(KP sift_kp.cpp)
target_link_libraries(Pano ${OpenCV_LIBS})
target_link_libraries(DisplayImage ${OpenCV_LIBS})
target_link_libraries(KP ${OpenCV_LIBS})

When I check if I have libgtk in system using locate libgtk, I see libgtk-3-dev and libgtk2.0-dev files along with lots of other libgtk files. What should I do to run this basic code? What do I need to learn about build systems that will make it easier to solve such errors?


@crackwitz Can you guide me on this matter. The crosspost which you have linked does not have the answer on solving this issue