I have a program that will load a shared library and call functions from within that library using the dlsym family of functions. The shared library needs to be able to call functions defined within the parent executable.
E.g. this functionality:
parent.hpp
namespace parent{
extern "C"
void show_message(const char *str);
}
lib.cpp -> test.so
g++ -shared -fPIC -std=c++14 lib.cpp -o test.so
#include "parent.hpp"
extern "C"
void run(){
parent::show_message("shared lib called 'show_message'");
}
parent.cpp -> executable
g++ -rdynamic -export-all-symbols -std=c++14 parent.cpp -o executable -ldl
#include "parent.hpp"
#include <dlfcn.h>
extern "C"
void parent::show_message(const char *str){
// print/show message
}
int main(int argc, char *argv[]){
void *handle = dlopen("./test.so", RTLD_LAZY);
auto func = reinterpret_cast<void(*)()>(dlsym(handle, "run"));
func();
dlclose(handle);
}
With the approach above I get a symbol lookup error:
symbol lookup error: ./test.so: undefined symbol: show_message
What is the correct way to come at this issue?
How do I expose the symbols of the executable to the shared library?
Aucun commentaire:
Enregistrer un commentaire