开发者

How do I retrieve the path to my dylib at runtime?

On OS X, how can code in a dylib find the path it was loaded from, at runtime?

Coming from a Windows background,开发者_如何学编程 I'm used being able to call GetModuleFileName(dllHandle,...).

There exists NSGetExecutablePath() which will give me the path of the executable for the current process. Is there an equivalent to give me the current dylib path?


Use dladdr(3). Given a memory address, dladdr() outputs a structure that has, amongst other data, the path of the library containing the address. For example, inside your library:

#include <stdio.h>
#include <dlfcn.h>

void test(void) {
    Dl_info info;
    if (dladdr(test, &info)) {
        printf("Loaded from path = %s\n", info.dli_fname);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜