I have some function, say int foo(int x)
which I get from a DLL (using dlsym()
). So, currently my code looks something like this:
void foo(int x) {
void (*foo)(int x);
foo = dlsym(dll_handle, "foo");
int y = foo(x);
printf("y is %d", y);
}
What I want to is for (something like) this code to work:
void bar(int x) {
int y = foo(x);
printf("y is %d", y);
}
So that foo()
is a stub which calls the dll function (but does not have to search the DLL every time).
- What's the best approach to achieving this for a single function?
- For the case of many functions, how would I avoid writing a bunch of copy-paste stubs? A macro solution might be tricky, considering the signature. Perhaps a C++11-based variadic-arg template-based thing?
I have a basic idea for a solution for 1. in an answer below, but I'm not too sure about it, I'd like to adopt the 'best practice' approach here.