Robin Vogel
Robin Vogel

Reputation: 159

I can compile with g++ but get a buggy program with equivalent cmake (GTK error)

I have a very simple program in main.cpp that displays an image with openCV. I compile it using the following command:

g++ -o main main.cpp $(pkg-config opencv4 --cflags --libs)

where the output of pkg-config opencv4 --cflags --libs is provided in the appendix of this question.

The program then works flawlessly and I get to see my .jpg file in a new window.

I decided to compile it using cmake, following the example provided by openCV. My program (main.cpp) is essentially the same.

So my CMakeLists.txt file is:

cmake_minimum_required(VERSION 3.1)

project( main )
find_package( OpenCV REQUIRED )

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -pthread")

include_directories( ${OpenCV_INCLUDE_DIRS} )

add_executable( main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} )

I build my project using:

mkdir build
cd build
cmake ../
make

and start the program, which to my astonishment fails with the following error:

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.5.5-dev) ${ROOT_DIR_OF_MY_PROJECT}/opencv-4.x/modules/highgui/src/window.cpp:1251: 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 'cvNamedWindow'

where ROOT_DIR_OF_MY_PROJECT is the full path corresponding to ../ from where my main.cpp is located.

My question

Please ask for any further info.

Tried out

Appendix

Output of pkg-config opencv4 --cflags --libs (newlines for readability):

-I/usr/include/opencv4/opencv -I/usr/include/opencv4 -lopencv_stitching
-lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib
-lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui
-lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs
-lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg
-lopencv_rgbd -lopencv_saliency -lopencv_shape -lopencv_stereo -lopencv_structured_light
-lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching 
-lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot
-lopencv_ml -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_ximgproc
-lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d 
-lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto 
-lopencv_photo -lopencv_imgproc -lopencv_core

Upvotes: 3

Views: 91

Answers (1)

Robin Vogel
Robin Vogel

Reputation: 159

Dan Masek was absolutely right ! (see comments)

I was not linking to the same version, which I saw by printing the include directories for the cmake attempt. Please find below the corrected CMakeLists.txt file.

cmake_minimum_required(VERSION 3.1)

project( main )

# Links to the wrong version of openCV
# find_package( OpenCV REQUIRED )
find_package(PkgConfig REQUIRED)
pkg_check_modules(OpenCV REQUIRED opencv4)
# MESSAGE(${OpenCV_INCLUDE_DIRS})
# MESSAGE(${OpenCV_LIBRARIES})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -pthread")

include_directories( ${OpenCV_INCLUDE_DIRS} )

add_executable( main main.cpp )

target_link_libraries( main ${OpenCV_LIBRARIES})

Upvotes: 2

Related Questions