Problems with adding callable library

Questions and problems about using polymake go here.
sebasguts
Posts: 17
Joined: 07 Nov 2011, 09:59

Problems with adding callable library

Postby sebasguts » 08 Nov 2011, 14:46

Hello,

I'm trying to write a package for using Polymake's callable library out of GAP. When I'm trying to create an instance of Main, I always get an Seqmentation Fault. I have broken down the Error to a problem with the dynamic loader in Gap. With this I send the code of two files, because I'm not allowed to upload them :( , one to create a loadable C-module, which loads the library, the other one loads the C-module and tries to execute the function that creates the polymake::Main. Both files are attached. I compiled them with

g++ -o module.so module.cpp $(polymake-config --cflags) $(polymake-config --libs) -fPIC -shared -O0 -g
gcc -o main main.c -Wall -O0 -g -ldl

Thank you for your help and Greetings
Sebastian Gutsche

modules.cpp

Code: Select all

#include <polymake/Main.h> #include <polymake/Matrix.h> #include <polymake/Rational.h> #include <iostream> #include <map> #include <string> using std::cerr; using std::endl; using std::string; using std::map; static polymake::Main *main_polymake_session; static polymake::perl::Scope *main_polymake_scope; static map<int, pm::perl::Object*> *polymake_objects; static int new_polymake_object_number; static std::string* teststr; static void TestFunc() { cerr << "Erstelle std::string zum Testen" << endl; teststr = new std::string("blabla"); *teststr += ".x"; cerr << *teststr << endl; // We start with initialising the polymake classes. cerr << "Erstelle polymake::Main" << endl; main_polymake_session = new polymake::Main; cerr << "Erstelle polymake::perl::Scope" << endl; main_polymake_scope = new polymake::perl::Scope(main_polymake_session->newScope()); //This is pretty slow. polymake_objects = new map<int, pm::perl::Object*>; new_polymake_object_number=0; } typedef void (*myfunc)(); extern "C" { myfunc GetPointer ( void ) { return &TestFunc; } }
main.c

Code: Select all

#include <dlfcn.h> #include <stdio.h> #define ERRMSG(m) fprintf(stderr, "%s\n", m) typedef void (*myfunc)(); typedef myfunc (*getpointerfunc)(); int main() { void *handle; getpointerfunc f; myfunc g; ERRMSG("Lade Modul."); handle = dlopen("./module.so", RTLD_LAZY); if(handle == NULL) { ERRMSG("Fehler beim Laden des Moduls."); return -1; } ERRMSG("Lade Symbol."); f = (getpointerfunc)dlsym(handle, "GetPointer"); if(f == NULL) { ERRMSG("Fehler beim Laden des Symbols."); return -1; } g = (*f)(); ERRMSG("Führe Funktion aus."); (*g)(); ERRMSG("Erfolg."); dlclose(handle); return 0; }

blorenz
Developer
Posts: 139
Joined: 10 Jan 2011, 17:21

Re: Problems with adding callable library

Postby blorenz » 09 Nov 2011, 11:31

Hello,

If you want to use the callable library from a shared module which is loaded on demand via dlopen you need to specify the RTLD_GLOBAL flag (at least for now).

More details:
polymake (and the same is true for perl) consists of one main library (libpolymake.so) and several different modules (e.g. polytope.so) which are loaded via dlopen depending on the requested features. Those libraries need access to the functions of the main library. When the main executable is linked against libpolymake.so its symbols will be visible for the other modules, but if libpolymake.so is loaded via (a dependency of) a dlopened library these symbols are only visible to the main program but not to the other modules. This can be changed by setting RTLD_GLOBAL for dlopen when loading the library which is linked against libpolymake.so.

In the next release polymake will have a mechanism to automatically upgrade the symbol visibility of libpolymake to avoid such problems. So instead of setting RTLD_GLOBAL you can also try applying the attached patch (patch -p3 < filename) for Main.cc and rerunning make and make install.

Benjamin

PS: Depending on the polymake installation you might also want to specify polymake-config --includes and polymake-config --ldflags when compiling the module.
Attachments
libpolymake-dlopen.patch
(1.62 KiB) Downloaded 491 times

sebasguts
Posts: 17
Joined: 07 Nov 2011, 09:59

Re: Problems with adding callable library

Postby sebasguts » 09 Nov 2011, 15:53

Thank you very much for the quick help and also for the patch. I think I need the patch as I cannot set the flags in GAP itself. Are you already planning the release?

EDIT: Tried to use the patch, works great!

Greetings and again thank You,
Sebastian


Return to “Helpdesk”