Search code examples
c++ubuntugtkgtkmm

Gtkmm-3.0 compiled code throwing Gtk+2 symbol error


I'm on Ubuntu 13.10 using gtkmm 3.0 dev package, compiling the following code:

#include <gtkmm.h>
#include <iostream>
#include "opencv2/opencv.hpp"
#include <chrono>
#include <thread>
#include <mutex>

//GASP! global vars!
std::mutex FRAME_MUTEX;
std::thread CV_THREAD;
cv::Mat FRAME, CLEAN;
Glib::Dispatcher dispatcher;
volatile bool THREADRUN;

void cvThread() {
    THREADRUN = true;
    cv::VideoCapture capture(0);
    while(THREADRUN) {
        FRAME_MUTEX.lock();
        capture >> FRAME;
        FRAME_MUTEX.unlock();
        dispatcher.emit();
    }
}

int main(int argc, char** argv)
{
    Gtk::Main gtkMain(argc, argv);
    Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("main_window.glade");

    Gtk::Window *mainWindow;
    Gtk::Image *cleanDisplay;
    Gtk::Image *evmDisplay;

    builder->get_widget("main_window", mainWindow);
    builder->get_widget("clean_display", cleanDisplay);
    builder->get_widget("evm_display", evmDisplay);

    dispatcher.connect([&]() {
        FRAME_MUTEX.lock();
        cv::cvtColor(FRAME, CLEAN, CV_BGR2RGB);
        cleanDisplay->set(Gdk::Pixbuf::create_from_data(CLEAN.data, Gdk::COLORSPACE_RGB, false, 8, CLEAN.cols, CLEAN.rows, CLEAN.step));
        cleanDisplay->queue_draw();
        FRAME_MUTEX.unlock();
    });

    CV_THREAD = std::thread(&cvThread);
    gtkMain.run(*mainWindow);

    return 0;
}

Using the command:

$ g++ test.cpp -o uitest `pkg-config --cflags --libs gtkmm-3.0 opencv` -std=c++11

Compilation works fine, but when I run the executable, it fails with the following error:

(uitest:20300): Gtk-ERROR **: GTK+ 2.x symbols detected.
Using GTK+ 2.x and GTK+ 3 in the same process is not supported
Trace/breakpoint trap (core dumped)

This is my first attempt to delve into using gtkmm, and I don't have the foggiest idea of where to go to start debugging. I checked the glade file, and it has an xml comment at the top indicating that gtk+3 is required by the contents of that file. The answer here didn't help me much; I got the following output from following those instructions:

-lgtkmm-3.0 -latkmm-1.6 -lgdkmm-3.0 -lgiomm-2.4 -lpangomm-1.4 -lgtk-3 -lglibmm-2.4
-lcairomm-1.0 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0
-lcairo-gobject -lpango-1.0 -lcairo -lsigc-2.0 -lgobject-2.0 -lglib-2.0
-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann
-lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml
-lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres
-lopencv_ts -lopencv_video -lopencv_videostab 

Any suggestions on how to fix this issue?


Solution

  • Till the OpenCV 3 gets released you can use Gtk::Image or Gdk::pixbuf to read/write images and skip linking highgui wich is the cause of the problem. Here is how you can convert Mat & pixbuf:

    cv::Mat mat(cv::Size(pixbuf->get_width(),pixbuf->get_height()),
     CV_8UC3, (uchar*)pixbuf->get_pixels(), pixbuf->get_rowstride());
    

    RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_data((guint8*)mat.data,
    Gdk::COLORSPACE_RGB,false,8,mat.cols,mat.rows,mat.step);
    

    The "false" parameter is for transparency.