Well I asked about that issue already 3 years ago, but got no really helpful answer.
The problem is, that some languages like Nim loads dynamic libs with dlopen(). Python seems to have done it also some years ago, I found a related issue with google, but again no solution.
I hacked together some C code to show the issue:
It is based on
http://tldp.org/HOWTO/Program-Library-HOWTO/dl-libraries.html
// gcc -o foo foo.c -ldl
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
int main(int argc, char **argv) {
void *handle;
void *handle2;
long (*gtype)(char*);
char *error;
handle = dlopen ("/usr/lib64/libgobject-2.0.so", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
handle2 = dlopen ("/usr/lib64/libgtk-3.so", RTLD_LAZY | RTLD_GLOBAL);
if (!handle2) {
fputs (dlerror(), stderr);
exit(1);
}
gtype = dlsym(handle, "g_type_from_name");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}
printf ("%ld\n", (*gtype)("gfloat"));
printf ("%ld\n", (*gtype)("gchararray"));
printf ("%ld\n", (*gtype)("guint"));
printf ("%ld\n", (*gtype)("GtkGradient"));
dlclose(handle);
}
Output is
stefan@nuc /tmp/gtk3 $ gcc -o foo foo.c -ldl
stefan@nuc /tmp/gtk3 $ ./foo
56
64
28
0
The last line should be not zero. (I added RTLD_GLOBAL flag, which google told me, but that makes no difference.) My impression is, that for the “fundamental” gtypes all is OK, but the other (boxed) are not initialized with dlopen(). There is one fix: Call gtk_gradient_get_type() once before calling g_type_from_name(). That seems to work reliable, users have done that in the past for all the gtksourceview types which have the same issue. For (some) gtksourceview types a call of _new() seemed also to work.
So calling _get_type() for all the not fundamental types at program startup seems to be a solution, but an ugly one. I assume that the gtk related libs contain some startup code, which is executed when libs are linked regulary, but not when only dlopen() is called. So can there be a solution for this issue?