Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create and load a GTK module?

Tags:

module

gtk

I want to create a custom GTK module which should be loaded when I start a GTK application.

Documentation on this topic is rare, I searched a lot but I failed to get it running. I'm on Ubuntu Linux with GTK3 and tried sofar:

  • Compiled and linked a shared library with the method void gtk_module_init(gint *argc, gchar ***argv[]) inside. As far as I understood, this should be enough to create a simple module. Full code:
#include <iostream>
#include <gtk/gtk.h>

void gtk_module_init(gint *argc, gchar ***argv[]) {
    std::cout << "huhu" << std::endl;
}
  • Put this lib into /usr/lib/x86_64-linux-gnu/gtk-3.0/modules/libtest-gtk-module.so
  • Tried to launch an application like this: gnomine --gtk-module=libtest-gtk-module.so But all I get is: Gtk-Message: Failed to load module "libtest-gtk-module.so"

So what else has to be done in order to make GTK load this library?

Many thanks in advance!

like image 778
Simme Avatar asked Apr 21 '12 16:04

Simme


1 Answers

You need to make the system aware of the library. For a library in a system directory, it should be enough to run ldconfig as root. Take a look at the tutorial here.

[EDIT]

I got the module to load as follows:

  • Since this is C++ code, you need to make sure the function name isn't name mangled:

    extern "C" {
    void gtk_module_init(gint *argc, gchar ***argv[]) {
        std::cout << "huhu" << std::endl;
    }
    }
    
  • I built it with the following:

    g++ -fPIC -shared -Wl,-soname,libfoo.so.1 -olibfoo.so.1.0.1 `pkg-config --libs --cflags gtk+-3.0` t.c
    
  • I used an absolute path to avoid messing with ldconfig, this is probably the best thing to do while developing the module:

    ~$ gedit --gtk-module=/home/eric/libfoo.so.1.0.1 t.c
    huhu
    

This is on Mint LMDE, not Ubuntu, but I don't think it matters.

like image 84
ergosys Avatar answered Nov 30 '22 07:11

ergosys