Srujan Srujan - 2 months ago 7
C++ Question

library that invokes dialog box with pushbutton using GTK+ in linux

Hi I am using GTK+ for the first time. Here i am trying to create GUI environment using GTK+. Here my question is - Is there any possibility to create window/dialogs, pushbuttons using GTK+ with out main function in library..?

Answer

Practically speaking, there is no such possiblity and you should not try that.

Recall that GTK is a LGPL2.1 licensed library, and that license sort-of "requires" the user to be able to upgrade the GTK library quite easily (in any proprietary application using it). The very usual way of doing that is to dynamically link the libgtk-3.so; as soon as that shared library is upgraded, the application using it would be able to transparently use the new version of libgtk ...

So make your own library a dynamically linked ELF shared object, as everyone do.

If you give or distribute a statically linked library using GTK, you need (to obey to the LGPL license) to provide a mean for the user to relink it to a newer version of GTK.

And your library (whatever form it has) should provide a function initializing the GTK interface, e.g. creating all the GTK widgets and installing callbacks -using g_signal_connect etc...- on them.

So just make a shared library e.g. libmycode.so (which you could link to libgtk-3.so) which would have some initialization functions, e.g. run the following commands (probably thru your Makefile) on your source files mysrc1.c mysrc2.c

 gcc -Wall -fPIC -O -g -c $(pkg-config --cflags gtk+-x11-3.0) \
     mysrc1.c -o mysrc1.pic.o
 gcc -Wall -fPIC -O -g -c $(pkg-config --cflags gtk+-x11-3.0) \
     mysrc2.c -o mysrc2.pic.o
 gcc -O -g -shared mysrc1.pic.o mysrc2.pic.o \
      $(pkg-config --libs  gtk+-x11-3.0) \
     -o libmycode.so

For details, read the Program Library HowTo & Drepper's paper: How To Write Shared Libraries

Notice that a static library libsomecode.a is just an amalgamation of member object files (*.o). So there is no way to transparently link it to any shared object (you could adopt an outside convention that the user of libsomecode.a should link a shared object -lgtk). In contrast, you can link a shared library libmycode.so to another one when building libmycode.so