I upgrade a program that needs to run on both old and new platform. In the new platform, I have new config so certain functions will not get called in the old platform. However, when I run the program on old platform, it complains missing shared library. What I need is something like "lazyload" or "delayload" mechanism such that when a function is not called the system won't bother to load its dependent library. But so far I haven't found a way to achieve it on Linux. I use gcc/c with normal link options.
I try simple approach of "-Wl,-z,nodefs" but with error.
/usr/bin/ld: warning: -z nodefs ignored.
So how to enforce it?
CodePudding user response:
You have two options:
- Instead of calling functions in the optional shared library as normal, call them via
dlopen()anddlsym(). This will let you explicitly load your shared library at runtime, when needed. - On the platform where the shared library is not available, make a dummy shared library and link that (or load it using
LD_PRELOAD). It will need to contain dummy versions of whatever functions your program calls, they don't need to do anything useful if you don't actually invoke them.
CodePudding user response:
It looks like your particular linker (you didn't specify which one, but we can guess it might be the default ld.bfd linker) does not support -z nodefs. Try --allow-shlib-undefined to achieve the same result.
