I'm using dlsym() in C and I have a question whether the return value of dlsym() should be explicitly cast or if it is implicitly cast correctly. Here is the function:
double (*(compile)(void))(double x, double y)
{
if (system("scan-build clang -fPIC -shared -g -Wall -Werror -pedantic "
"-std=c11 -O0 -lm foo.c -o foo.so") != 0) {
exit(EXIT_FAILURE);
}
void *handle;
handle = dlopen("./foo.so", RTLD_LAZY);
if (!handle) {
printf("Failed to load foo.so: %s\n", dlerror());
exit(EXIT_FAILURE);
}
foo f;
f = (foo)dlsym(handle, "foo");
if (!f) {
printf("Failed to find symbol foo in foo.so: %s\n", dlerror());
exit(EXIT_FAILURE);
}
return f;
}
The function compile() does not take a value and returns a pointer to a function that takes two doubles as input and which returns a double. I then set a system call which compiles a shared object foo.so. I then open foo.so with dlopen(). Then dlsym() finds foo in foo.so and returns an object of type foo which I defined in a header as:
typedef double (*foo)(double, double);
Do I have to cast dlsym()?
dlsym()a bit opaque in this regard but if someone can enlighten me on the manpage that would also help. - lord.garbagegccandclangcomplain. - lord.garbage