I want to use library function using dlopen and dlsym, library function has a default argument. When I use this function using dynamic loading of library it works fine, so how can I achieve that.?
extern "C" int checklib(char *arg1, char *arg2 = NULL)
{
if(arg2 != NULL)
{
printf("arg2 is not null");
}
printf("arg1 = %s\n", arg1);
}
int main()
{
void *libHandle = NULL;
char *error;
int ret;
libHandle = dlopen("libtest.so", RTLD_LAZY);
if (!libHandle)
{
fprintf(stderr, "%s\n", dlerror());
return(-8);
}
typedef int (*fun_ptr_to_test)(char *);
fun_ptr_to_test test;
test = (fun_ptr_to_test) dlsym(libHandle, "checklib");
if ((error = dlerror()) != NULL)
{
fprintf(stderr, "%s\n", error);
if (libHandle)
dlclose(libHandle);
return(-8);
}
ret = test("dadfsj");
if (libHandle)
dlclose(libHandle);
return ret;
}
I am getting some garbage value in arg2. so how can i achieve it.