By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,795 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,795 IT Pros & Developers. It's quick & easy.

dlopen success, dlsym returns undefined symbol

100+
P: 365
opening shared lib with dlopen, gets success, but dlsym returns an error saying undefined symbol, also passing "-rdynamic" during linking this shared lib, any idea?. dlopen success, dlsym gets error, also when I do nm on this shared lib, I can see symbol, this is standalone lib, not linked with executable.


if ((handle = dlopen(libname, RTLD_NOW)) == NULL) {
printf("ERROR: cannot load library: %s, %s\n", libname, dlerror());
exit(1) ;
} else {
ret = dlsym(handle, func_name);
if ((error = dlerror()) != NULL) {
printf("ERROR: load condition function(%s) failed in %s: error:%s\n",
func_name, libname, error );
exit(1);
}
}
Dec 18 '09 #1
Share this Question
Share on Google+
6 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Is this C or C++?

C++ requires func_name be extern "C" in the shared library.
Dec 18 '09 #2

100+
P: 365
This is C code only, but file names are .cpp. eventhough C code, do I need "extern "C"?.
Dec 18 '09 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
If the file names are .cpp I expect you have a C++ compiler.

If so, you will need the extern "C" when you build the shared library OR you must change the project settings to compile the .cpp files as C code only.

I say this since most C compiles use .c for this implementation files.
Dec 18 '09 #4

100+
P: 365
Thanks, it works, now dlsym works.

but when I tried to execute the function which was in shared lib, it fails. this func1 is part of shared lib was opened above using dlopen, then dlsym gets success, execution fails?. any idea?.


(func1)(param1, param2, param3).
Dec 18 '09 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
dlsym returns a void*. This is the address of the function in the library.

If this library function looks like:

Expand|Select|Wrap|Line Numbers
  1. void MyFunc(int a, int b);
Then you must typecast the void* returned from dlsym into a pointer to a function that takes two int arguments and returns a void.

Then you use the typecast pointer to make the call:

ret = dlsym(handle, func_name);

void (*MyPointer)(int, int) = (void (*)(int,int))ret;

MyPointer(3,4);
Dec 18 '09 #6

100+
P: 365
Thanks for your posting.
Dec 19 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.