问题
I have an ANSI C program that dynamically loads a .so file using dlopen() passing RTLD_LAZY. I receive
Undefined symbol "_nss_cache_cycle_prevention_function"
warnings whenever the .so file is accessed in FreeBSD 7.2. nss_cache_cycle_prevention_function() is not one of my program's functions and I imagine must be coming from FreeBSD. This might also be a problem on Linux too although I am not experiencing the issue there. I would prefer not to load FreeBSD specific header files into my program. I would like to either include this function in a portable way or suppress these warnings.
回答1:
What do you mean saying "I receive warning"? Does your program
examine the value returned by dlerror()
and prints it if it is not NULL?
The _nss_cache_cycle_prevention_function
is a marker symbol which is used by nsdispatch(3)
on FreeBSD to determine whether to employ the services of nscd(8)
, the name service caching daemon. It is perfectly normal that it does not exist in an
executable or a shared library.
But when nsdispatch(3)
executes dlsym(3)
, and the symbol is not found, the error will be set. And dlerror(3)
returns the description of the last error, and not the description of the error of the last call. I suspect that's what you are hitting.
The solution (quite portable) would be to:
- for
dlopen(3)
, check its return value before usingdlerror()
to see whether there was an error at all; - for
dlsym(3)
, sinceNULL
is a valid return value, to calldlerror()
in a void context before the call todlsym(3)
; that will clear any previous error, so that whatever the second call todlerror(3)
returns later on can be trusted.
In general, it will not harm anything to call an empty dlerror()
before any other dl* calls.
来源:https://stackoverflow.com/questions/3559147/dlerror-undefined-symbol-nss-cache-cycle-prevention-function-on-freebsd-7-2