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;
}
}
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;
}