I can run myapp to find the dylib as follows (Is it OK to use DYLD_LIBRARY_PATH on Mac OS X? And, what's the dynamic library search algorithm with it?):DYLD_LIBRARY_PATH="/PATH/lib" myapp我想我可以使用install_name_tool来更新库和可执行文件,以便可以使用rpath找到该库.我使用了这篇文章中的提示-如何指定rpath在dylib中?.I think I can use install_name_tool to update the library and executable so that the library can be found with rpath. I used the hints in this post - How can I specify the rpath in a dylib?.在lib中,我执行了此命令以添加rpath.In lib, I executed this command to add rpath.install_name_tool -id "@rpath/libtest.dylib" libtest.dylibinstall_name_tool -add_rpath "@executable_path/../lib/" libtest.dylib在垃圾箱中,我执行了install_name_tool -add_rpath "@executable_path/../lib/" myapp.In bin, I executed install_name_tool -add_rpath "@executable_path/../lib/" myapp.但是,当我在bin目录中执行myapp时,出现了错误消息.However, when I executed myapp in bin directory, I have the error messages.dyld: Library not loaded: libtest.dylib Referenced from: /PATH/bin/./myapp Reason: image not foundTrace/BPT trap: 5 otool -l myapp显示rpath已在myapp中正确更新.otool -l myapp shows the rpath is correctly updated in myapp.Load command 16 cmd LC_RPATH cmdsize 40 path @executable_path/../lib/ (offset 12) libtest.dylib也是如此The same is true with libtest.dylibLoad command 13 cmd LC_RPATH cmdsize 40 path @executable_path/../lib/ (offset 12)可能是什么问题?当然,我可以在编译和链接时使用cc -install_name,但是我想知道如何通过修改生成的dylib和执行二进制文件来做同样的事情.Of course, I can use cc -install_name when compile and link time, but I wanted to know how to do the same thing my modifying the generatd dylib and execution binary.来自库:cc -install_name "@loader_path/../lib/libtest.dylib" -dynamiclib -o libtest.dylib test.c或者,install_name可以使用@rpath:Or, the install_name can use @rpath:cc -install_name "@rpath/libtest.dylib" -dynamiclib -o libtest.dylib test.c从垃圾箱中cc -I../lib -c main.ccc -o main main.o ../lib/libtest.dylib -Wl,-rpath -Wl,@loader_path/../lib或仅一行:cc -I../lib -L../lib -o main main.c -ltest -Wl,-rpath -Wl,@loader_path/../lib推荐答案在otool -l中,我分析了应从原始库和二进制文件中添加或修改的内容.From otool -l, I analyzed what should be added or modified from the original library and binary.更改是在ID中:Load command 2 <-- OLD cmd LC_ID_DYLIB cmdsize 40 name libtest.dylib (offset 24) time stamp 1 Wed Dec 31 18:00:01 1969Load command 2 <-- NEW cmd LC_ID_DYLIB cmdsize 64 name @loader_path/../lib/libtest.dylib (offset 24)这是完成更改的命令:install_name_tool -id "@loader_path/../lib/libtest.dylib" libtest.dylib或使用rpath:install_name_tool -id "@rpath/libtest.dylib" libtest.dylib可执行文件有两个更改:rpath和load_dylibThe executableThere are two changes: rpath and load_dylibLoad command 12 <-- OLD cmd LC_LOAD_DYLIB cmdsize 40 name libtest.dylib (offset 24)Load command 12 <-- NEW cmd LC_LOAD_DYLIB cmdsize 64 name @loader_path/../lib/libtest.dylib (offset 24)这是完成更改的命令install_name_tool -change libtest.dylib @loader_path/../lib/libtest.dylib myapp我还需要添加rpathAlso I needed to add the rpathLoad command 14 cmd LC_RPATH cmdsize 32 path @loader_path/../lib (offset 12)这是完成添加操作的命令:This is the command to accomplish the addition: install_name_tool -add_rpath "@loader_path/../lib" myapp想法二进制文件试图找到该库,它从install_name_tool -add_rpath "@loader_path/../lib" myapp知道它的位置.它将加载库,并且库的ID为@rpath/libtest.dylib,其中在可执行二进制文件中将@rpath设置为@loader_path/../lib进行匹配.The ideaThe binary tries to find the library, it knows where it is located from install_name_tool -add_rpath "@loader_path/../lib" myapp. It loads the library, and the library's id is @rpath/libtest.dylib where @rpath is set to @loader_path/../lib in the executable binary to make the match. 您能帮助我了解Mach-O库在Mac Os X中的工作原理吗?使用CMake时,我们可以在CMakeLists.txt文件中添加以下内容来自动化该过程.When using CMake, we can automatize the process with the following addition in CMakeLists.txt file.应该添加ID.# https://cmake.org/pipermail/cmake/2006-October/011530.htmlSET_TARGET_PROPERTIES (test PROPERTIES BUILD_WITH_INSTALL_RPATH 1 INSTALL_NAME_DIR "@rpath" )可执行的 rpath应该指定:ExecutableThe rpath should be specified:SET(CMAKE_INSTALL_RPATH "@loader_path/../lib/libtest.dylib") 这篇关于install_name_tool以更新可执行文件以在Mac OS X中搜索dylib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-15 14:05