简体   繁体   English

在Mac中加载动态库路径错误

[英]loading dynamic library path error in mac

I am now building a dynamic library and an command line illustration program that uses this dynamic library. 我现在正在构建一个动态库和一个使用该动态库的命令行插图程序。 The library and the illustration program are in the same folder: 库和插图程序位于同一文件夹中:

/user/xxx/develop/debug/libdynamic.dylib
/user/xxx/develop/debug/illustration

When the illustration program can work very well in my computer, I send the illustration program as well as the dynamic library to my colleague and he will run the illustration program in his computer. 当插图程序在我的计算机上可以很好地工作时,我将插图程序以及动态库发送给我的同事,他将在他的计算机上运行插图程序。 However, every time he runs the illustration program in the command window, the program also reminds that it can not load the libdynamic.dylib as it tries to find the library in /user/xxx/develop/debug/ , which is unavailable in my colleague's computer. 但是,每次他在命令窗口中运行插图程序时,该程序也会提醒它无法加载libdynamic.dylib因为它试图在/user/xxx/develop/debug/找到该库,而该库在我的系统中不可用。同事的计算机。 I was wondering what I should do. 我在想该怎么办。 Many thanks. 非常感谢。

EDIT: The output using otool for the illustration program is as follows: 编辑:使用otool的插图程序的输出如下:

 /Users/xxx/develop/debug/libdynamic.dylib (compatibility version 0.0.0, current version 0.0.0)

    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 744.18.0)

    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 56.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

You need to tell illustration where to find libdynamic.dylib which you can do post-build using install_name_tool ( manpage ). 您需要告诉illustration在哪里可以找到libdynamic.dylib ,可以使用install_name_toolmanpage )在构建后进行。 You'll want to set the new path to @executable_path/libdynamic.dylib , with (something like): 您需要使用(类似)将新路径设置为@executable_path/libdynamic.dylib

$ install_name_tool -change /user/xxx/develop/debug/libdynamic.dylib \
  @executable_path/libdynamic.dylib \
  /user/xxx/develop/debug/illustration

(the exact old name value to pass to install_name_tool will depend on what it's currently set to, which can be determined using otool -L /user/xxx/develop/debug/illustration ). (要传递给install_name_tool的确切旧名称值将取决于当前设置的名称,可以使用otool -L /user/xxx/develop/debug/illustration )。

One way to avoid this nonsense (and the way I do it myself) is to use the -install_name linker option: 避免这种废话(以及我自己做的这种方式)的一种方法是使用-install_name链接器选项:

$(BINDIR)/libdynamic.dylib: $(OBJS)
    $(CXX) -dynamiclib -current_version $(MAJOR_MINOR_VERSION) \
        -compatibility_version $(MAJOR_MINOR_VERSION) \
        -install_name @executable_path/libdynamic.dylib \
        $(LDFLAGS) -o $@ $(OBJS) $(LIBS)

( Makefile fragment taken from here ). (从此处获取的Makefile片段)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM