I read a few tutorials about loading shared libraries and call functions in them. I succeded on both points. There’s just one thing I didn’t see in any of the tutorials:
How do I return a value from a function in a shared library to the main code?
This is my shared library source:
#include <stdio.h>
char* entry(){
printf("this is a working plugin\n");
return "here we go!";
}
When i call it, i get “this is a working plugin” on the stdout. My question is now, how i can get the “here we go” string back to the main.c which looks like:
void *lib_handle;
void (*lib_func)();
...
lib_handle = dlopen("/home/tectu/projects/tibbers/plugins.so", RTLD_LAZY);
if(!lib_handle)
error("coudln't load plugins", NULL);
lib_func = dlsym(lib_handle, "entry");
if(!lib_func)
error("coudln't find symbol in plugin library", NULL);
(*lib_func)(); // here i call the entry() from the .so
Something like this does not work:
printf("return value: %s\n, (*lib_func)());
So, any ideas?
Thank you.
It works when
lib_funcis properly declared:You might need to cast in the assignment from
dlsym.