Plugin führt code im Hauptprogramm erzeugt symbol lookup error
-
Hallo zusammen!
Ich habe ein Plugin, welches Code im Hauptprogramm ausführen soll. Es bekommt den Pointer auf eine Klasseninstanz (core) des Hauptprogramms und soll eine Funktion von dieser Klasse ausführen.
/* code im plugin */ extern "C" void getInfo(plugin_info *i) { strcpy(i->p_name,"seeker"); core = i->l_core; sleep(2); core->startBroadcast (); }
Compilen kein Problem aber in der Laufzeit kriege ich einen Fehler
libhupl_seeker.so: undefined symbol: _ZN8arp_core14startBroadcastEv
Sicherlich muss ich im Hauptprogramm (der Core-Klasse) auch mit extern "C" arbeiten, aber wie?
Bei/* code im hauptprogramm */ extern "C" class arp_core{/*...*/}
bekomme ich bei jeder statischen Funktion in der Klasse
error: invalid use of ‘static’ in linkage specification
Was mach ich falsch?
-
Wird die passende cpp Datei mit den arp_core Methoden ins Hauptprogramm gelinkt?
Ich hab nämlich schnell ein Beispiel gemacht:
main.cpp
#include <iostream> #include <dlfcn.h> #include "plugin_info.hpp" using namespace std; int main() { void *lib = dlopen("./plugin.so", RTLD_NOW); if (lib) { void (*fkt)(plugin_info *); *(void **) (&fkt) = dlsym(lib, "getInfo"); if (fkt) { plugin_info p; (*fkt)(&p); cout << "Main: " << p.name << endl; } else cerr << "Failed to load function " << dlerror() << endl; dlclose(lib); } else cerr << "Failed to open lib " << dlerror() << endl; return 0; }
plugin.cpp
#include "plugin_info.hpp" extern "C" void getInfo(plugin_info *i) { i->name = "testme"; i->exec(); }
plugin_info.cpp
#include <iostream> #include "plugin_info.hpp" void plugin_info::exec() { std::cout << "Ich heiße \"" << name << "\"." << std::endl; }
plugin_info.hpp
#ifndef __PLUGIN_INFO_H #define __PLUGIN_INFO_H #include <string> class plugin_info { public: std::string name; void exec(); }; #endif /*__PLUGIN_INFO_H*/
$ g++ -o plugin.so -shared -fPIC -Wall -s plugin.cpp $ g++ -o main -ldl -rdynamic -Wall -s main.cpp $ ./main Failed to open lib ./plugin.so: undefined symbol: _ZN11plugin_info4execEv $ g++ -o main -ldl -rdynamic -Wall -s main.cpp plugin_info.cpp $ ./main Ich heiße "testme". Main: testme $
-
danke danke danke!!!!!
Du hast völlig recht gehabt: -ldl -rdynamic hat den Fehler völlig behoben!