OSX:无法使用dlopen()解析依赖项

时间:2012-11-29 03:59:45

标签: c macos dlopen

我在OSX上有一个库libni我正在尝试使用dlopen()打开它,它似乎无法解析对另一个dlopen()库的依赖。

让我们不要理解为什么你不应该使用dlopen();足以说,我必须在这种情况下使用它。

无论如何,这是libni.dylib上的otool -L:

lib/libni.dylib:
  libni.dylib (compatibility version 0.0.0, current version 0.0.0)
  libn.dylib (compatibility version 0.0.0, current version 0.0.0)
  /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)
  libzlib.dylib (compatibility version 0.0.0, current version 0.0.0)
  libpng.dylib (compatibility version 0.0.0, current version 0.0.0)
  liblua.dylib (compatibility version 0.0.0, current version 0.0.0)
  libSDL2.0.dylib (compatibility version 0.0.0, current version 2.0.0)
  libstb.dylib (compatibility version 0.0.0, current version 0.0.0)
  libax.dylib (compatibility version 0.0.0, current version 0.0.0)
  libsqlite3.dylib (compatibility version 0.0.0, current version 0.0.0)
  libfreetype.dylib (compatibility version 0.0.0, current version 0.0.0)
  /usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
  /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 19.0.0)
  /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
  /System/Library/Frameworks/ForceFeedback.framework/Versions/A/ForceFeedback (compatibility version 1.0.0, current version 1.0.2)
  /System/Library/Frameworks/Carbon.framework/Versions/A/Carbon (compatibility version 2.0.0, current version 155.0.0)
  /System/Library/Frameworks/CoreAudio.framework/Versions/A/CoreAudio (compatibility version 1.0.0, current version 1.0.0)
  /System/Library/Frameworks/AudioUnit.framework/Versions/A/AudioUnit (compatibility version 1.0.0, current version 1.0.0)

这是在libn,zlib和png:

libn.dylib:
  libn.dylib (compatibility version 0.0.0, current version 0.0.0)
  /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

libpng.dylib:
  libpng.dylib (compatibility version 0.0.0, current version 0.0.0)
  libzlib.dylib (compatibility version 0.0.0, current version 0.0.0)
  /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

libzlib.dylib:
  libzlib.dylib (compatibility version 0.0.0, current version 0.0.0)
  /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

现在我使用dlopen()按顺序打开我'lib'目录中的所有动态库,使用:

/** Open a dynamic library file */
void *Type__(open) (char *path) {
  void *impl = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
  if (impl == NULL) {
    char *err = dlerror();
    N_LOG(N_LOG_DEBUG, "Failed to open: %s", err);
  }
  else {
    N_LOG(N_LOG_DEBUG, "Happy open: %s", path);
    rtn = nPosixLinker__Lib(path, impl);
  }
  return rtn;
}

...并获取如下错误/消息:

Happy open: /Users/doug/projects/libni/build/tests/IImageLoader/lib/libzlib.dylib
Happy open: /Users/doug/projects/libni/build/tests/IImageLoader/lib/libstb.dylib
Happy open: /Users/doug/projects/libni/build/tests/IImageLoader/lib/libsqlite3.dylib
Happy open: /Users/doug/projects/libni/build/tests/IImageLoader/lib/libSDL2.dylib
Happy open: /Users/doug/projects/libni/build/tests/IImageLoader/lib/libn.dylib
Failed to open: dlopen(/Users/doug/projects/libni/build/tests/IImageLoader/lib/libpng.dylib, 10): Library not loaded: libzlib.dylib
  Referenced from: /Users/doug/projects/libni/build/tests/IImageLoader/lib/libpng.dylib
  Reason: image not found
Failed to open: dlopen(/Users/doug/projects/libni/build/tests/IImageLoader/lib/libni.dylib, 10): Library not loaded: libn.dylib
  Referenced from: /Users/doug/projects/libni/build/tests/IImageLoader/lib/libni.dylib
  Reason: image not found
Happy open: /Users/doug/projects/libni/build/tests/IImageLoader/lib/liblua.dylib
Happy open: /Users/doug/projects/libni/build/tests/IImageLoader/lib/libfreetype.dylib
Happy open: /Users/doug/projects/libni/build/tests/IImageLoader/lib/libax.dylib
Failed to open: dlopen(/Users/doug/projects/libni/build/tests/IImageLoader/lib/libtest-ni-IImageLoader__.dylib, 10): Library not loaded: libni.dylib
  Referenced from: /Users/doug/projects/libni/build/tests/IImageLoader/lib/libtest-ni-IImageLoader__.dylib
  Reason: image not found

所以这里肯定会发生一些奇怪的事情。 zlib在libpng之前打开,但是libpng无法解析引用。 libn在libni之前打开,但libni无法解析引用。

就像我的RTLD_GLOBAL被dlopen()忽略,或者依赖(例如.libn)没有找到加载的库(blah / libn.dylib),因为路径不同......

事实上,我似乎无法让dlopen()打开一个依赖依赖的库,所以我想我一定做错了。

有什么建议吗?

在BSD和Linux上这一切都正常;这绝对是一个与OSX处理这些库的方式有关的问题。

1 个答案:

答案 0 :(得分:0)

看起来这是this的副本,但是直到我已经找到解决方案之后我才完全找不到它,所以我现在要留下问题和这个链接。

排序答案:使用-install_name @ rpath / lib

构建库