Pregunta

i have followind problem. I have text parser located in parser.o library. I would like to parse some text(find functions) and call them. Functions are implemented in functions.o file. I am able to parse text, but i dont know how to call function from library functions.o. I am using dlopen(NULL, RTLD_NOW); to get reference to main program, but when i run actual program, it cant find given function and error "./program: undefined symbol: function_name" appears(function_name is located in functions.o) :/. I cant link functions.o with dlopen(functions.o,...) because it is not dynamically linked library. I am compiling program with:

   `gcc main.c lib/*`

Any help? :)

¿Fue útil?

Solución 2

dlopen(NULL, RTLD_NOW);

Instead of doing dlopen and dlsym, just call the functions directly. Since you link everything into the main executable, there is absolutely no reason to use dlopen at all.

Otros consejos

If you have to link the program statically, and all the functions share a signature, you can define your own table of function pointers.

struct entry
{
  const char * const func_name;
  void (*func) (void);
};

static struct entry table[] = 
{
  {"func_A", func_A},
  {"func_B", func_B},
  ...
};

You than look up the name of the function from the input and call it through the pointer. If the function signatures differ, you can still accomplish this using thunks that wrap your function call.

void func_A_thunk(void* dummy, ...)
{
  // parse the var_args to match the signature for func_A
  func_A (arg1, arg2, arg3);
}

All of that having been said... This is quite messy, so just compile the library code into dynamically linked libraries if you can, and use dlopen properly.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top