Gtk+ 3 Error when compiling with OpenCV

筅森魡賤 提交于 2019-12-11 01:31:12

问题


I have recently built OpenCV 3 from source on linux. I am able to successfully compile and run OpenCV programs.

I then downloaded Gtk+ 3 and all the required packages (GLib 2.52, Pango 1.40, Gdk-Pixbuf 2.36, ATK 2.24, GObject-Introspection 1.52). I am able to successfully compile and run this simple program compiling with:

g++ -std=c++11 gtkexample.cpp `pkg-config --cflags gtk+-3.0` -o gtkexample `pkg-config --libs gtk+-3.0`

Source code:

#include <gtk-3.0/gtk/gtk.h>

int main( int   argc,
      char *argv[] )
{
    GtkWidget *window;
    GdkRGBA *color;
    gtk_init (&argc, &argv);
    gdk_rgba_parse(color,"(0,0,0)");
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_decorated(GTK_WINDOW (window),FALSE);
    gtk_window_set_position(GTK_WINDOW (window), GTK_WIN_POS_CENTER);
    gtk_widget_override_background_color(window, GTK_STATE_FLAG_NORMAL, color);
    gtk_widget_show  (window);

    for (int i=200;i<400;i++)
        gtk_window_move(GTK_WINDOW (window),i,200);



    gtk_main ();

    return 0;
}

After successfully compiling I try to execute my OpenCV program with Gtk+ and a FlyCapture camera and I am faced with:

"Gtk-ERROR **: GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported"

The output of pkg-config --cflags gtk+-3.0 is:

-pthread -I/usr/local/include/gtk-3.0 -I/usr/local/include/gio-unix-2.0/ -I/usr/local/include/cairo -I/usr/local/include/pango-1.0 -I/usr/local/include/harfbuzz -I/usr/local/include/pango-1.0 -I/usr/local/include/atk-1.0 -I/usr/local/include/cairo -I/usr/local/include/gdk-pixbuf-2.0 -I/usr/local/include/glib-2.0 -I/usr/local/lib/glib-2.0/include -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/aarch64-linux-gnu/dbus-1.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12

Output of pkg-config --libs gtk+-3.0 is :

-L/usr/local/lib -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0

A snippet of the OpenCV program looks like:

#include "FlyCapture2.h"

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/core/cuda.hpp>
#include <opencv2/cudalegacy/NCVHaarObjectDetection.hpp>
#include <opencv2/cudaobjdetect.hpp>

#include <thread>
#include <iostream>
#include <vector>
#include <gtk-3.0/gtk/gtk.h>

using namespace FlyCapture2;

int main( int   argc,
      char *argv[])
{
    GtkWidget *window;

    gtk_init (&argc, &argv); //This is where the error occurs
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_decorated(GTK_WINDOW (window),FALSE);
    gtk_window_set_position(GTK_WINDOW (window), GTK_WIN_POS_CENTER);

    gtk_widget_show(window);
    gtk_main();
    //  ...
}

Compiling successfully with:

g++ -std=c++11 FlyCap2_to_MatImage.cpp `pkg-config --cflags gtk+-3.0` -I/home/nvidia/flycapture.2.11.3.121_arm64/include -I/usr/local/cuda-8.0/include -o FlyCap2_to_MatImage `pkg-config --libs gtk+-3.0` -lflycapture -lopencv_core -lopencv_highgui -lopencv_objdetect -lopencv_imgproc -lopencv_cudaobjdetect

My questions are:

  • Could OpenCV 3 possibly be using an old version of GTK+ ?
  • Will I have to rebuild OpenCV to configure with newer version of Gtk+ ?

回答1:


Considering you've installed Gtk+ 3 after building OpenCV, it's quite likely that it would have been built to work with Gtk+ 3. Since it complains, it suggests you already had some older version installed, and it uses that.

Hence, if you want to use the GUI convenience functions in OpenCV, you need to rebuild OpenCV, making sure it is built with with the correct version of Gtk+.

However, since you're providing your own GUI, it would seem that you don't really need to use that part of OpenCV. Since you have built OpenCV as individual modules, you could simply not link with opencv_highgui in order to avoid the conflict.




回答2:


I have the same problem, but in python I have resolve it by append this two lines in the head of the main python file. It will specify only version, which you want when your environment have many version of GTK. Good lucks !

import gi  
gi.require_version('Gtk', '2.0')


来源:https://stackoverflow.com/questions/45107654/gtk-3-error-when-compiling-with-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!