Mac OpenCV 修改dylib的默认链接位置 : Library not loaded … Reason: Image not loaded

在MAC OS X系统下,当我们尝试运行一个可执行文件时,会得到以下错误:

dyld: Library not loaded: libopencv_dnn.3.3.dylib
  Referenced from: /Users/name/Desktop/project/client"
  Reason: image not found
Trace/BPT trap:5

首先 cd 到可执行件./client所在目录:$ otool -L client ,输出如下:

client:
	@rpath/libopencv_dnn.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_ml.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_objdetect.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_shape.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_stitching.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_superres.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_videostab.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_calib3d.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_features2d.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_highgui.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_videoio.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_imgcodecs.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_video.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_photo.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_imgproc.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_flann.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_core.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	/usr/local/lib/libwebsockets.12.dylib (compatibility version 12.0.0, current version 0.0.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.5.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.60.2)

由与OpenCV的安装后的动态链接库 可执行程序搜索不到,就回报以上错误,这就需要将“@rpath/libopencv_dnn.3.3.dylib ” 用真实的动态库所在路径去修复并替代,使其指引到真实库上。假设我的OpenCV的动态就在当前目录下的./lib/目录下,只需执行:$ install_name_tool -change  @rpath/libopencv_dnn.3.3.dylib  ./lib/libopencv_dnn.3.3.dylib client 。

然后在执行:$ otool -L client ,即可修正动态链接库的路径,显示如下:

client:
	./lib/libopencv_dnn.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_ml.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_objdetect.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_shape.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_stitching.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_superres.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_videostab.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_calib3d.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_features2d.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_highgui.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_videoio.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_imgcodecs.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_video.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_photo.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_imgproc.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_flann.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_core.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	/usr/local/lib/libwebsockets.12.dylib (compatibility version 12.0.0, current version 0.0.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.5.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.60.2)

按照此方式修正所有的链接库路径,当我们再去执行时还会报错的,因为libopencv_dnn.3.3.dylib 等依赖了其他的OpenCV动态库,执行:$otool -L ./lib/libopencv_dnn.3.3.dylib

./lib/libopencv_dnn.3.3.dylib:
	@rpath/libopencv_dnn.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	@rpath/libopencv_imgproc.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	/System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate (compatibility version 1.0.0, current version 4.0.0)
	@rpath/libopencv_core.3.3.dylib (compatibility version 3.3.0, current version 3.3.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.5.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.60.2)

此时,我们需要将所有的OpenCV的动态库彼此依赖的库路径按照上面方法都要修正过来。

自己写了一个修正的脚步程序,供参考:


#!/bin/bash

install_name_tool -change  /usr/local/lib/libwebsockets.12.dylib  ./lib/libwebsockets.12.dylib server


install_name_tool -change  @rpath/libopencv_dnn.3.3.dylib  ./lib/libopencv_dnn.3.3.dylib client
install_name_tool -change  @rpath/libopencv_ml.3.3.dylib  ./lib/libopencv_ml.3.3.dylib client
install_name_tool -change  @rpath/libopencv_objdetect.3.3.dylib  ./lib/libopencv_objdetect.3.3.dylib client
install_name_tool -change  @rpath/libopencv_shape.3.3.dylib   ./lib/libopencv_shape.3.3.dylib  client
install_name_tool -change  @rpath/libopencv_stitching.3.3.dylib  ./lib/libopencv_stitching.3.3.dylib client
install_name_tool -change  @rpath/libopencv_superres.3.3.dylib  ./lib/libopencv_superres.3.3.dylib client
install_name_tool -change  @rpath/libopencv_videostab.3.3.dylib ./lib/libopencv_videostab.3.3.dylib client
install_name_tool -change  @rpath/libopencv_calib3d.3.3.dylib  ./lib/libopencv_calib3d.3.3.dylib client
install_name_tool -change  @rpath/libopencv_features2d.3.3.dylib  ./lib/libopencv_features2d.3.3.dylib client
install_name_tool -change  @rpath/libopencv_highgui.3.3.dylib  ./lib/libopencv_highgui.3.3.dylib client
install_name_tool -change  @rpath/libopencv_videoio.3.3.dylib  ./lib/libopencv_videoio.3.3.dylib client
install_name_tool -change  @rpath/libopencv_imgcodecs.3.3.dylib  ./lib/libopencv_imgcodecs.3.3.dylib client
install_name_tool -change  @rpath/libopencv_video.3.3.dylib  ./lib/libopencv_video.3.3.dylib client
install_name_tool -change  @rpath/libopencv_photo.3.3.dylib  ./lib/libopencv_photo.3.3.dylib client
install_name_tool -change  @rpath/libopencv_imgproc.3.3.dylib  ./lib/libopencv_imgproc.3.3.dylib client
install_name_tool -change  @rpath/libopencv_flann.3.3.dylib  ./lib/libopencv_flann.3.3.dylib client
install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib client
install_name_tool -change  /usr/local/lib/libwebsockets.12.dylib  ./lib/libwebsockets.12.dylib client
#install_name_tool -change  /usr/lib/libc++.1.dylib  ./lib/libc++.1.dylib client
#install_name_tool -change  /usr/lib/libSystem.B.dylib  ./lib/libSystem.B.dylib client

install_name_tool -change  @rpath/libopencv_imgproc.3.3.dylib  ./lib/libopencv_imgproc.3.3.dylib  lib/libopencv_dnn.3.3.dylib
install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_dnn.3.3.dylib

install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib lib/libopencv_imgproc.3.3.dylib

install_name_tool -change  @rpath/libopencv_imgproc.3.3.dylib  ./lib/libopencv_imgproc.3.3.dylib  lib/libopencv_imgcodecs.3.3.dylib
install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_imgcodecs.3.3.dylib

install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_ml.3.3.dylib

install_name_tool -change  @rpath/libopencv_imgproc.3.3.dylib  ./lib/libopencv_imgproc.3.3.dylib  lib/libopencv_objdetect.3.3.dylib 
install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_objdetect.3.3.dylib 

install_name_tool -change  @rpath/libopencv_imgproc.3.3.dylib  ./lib/libopencv_imgproc.3.3.dylib lib/libopencv_shape.3.3.dylib
install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_shape.3.3.dylib

install_name_tool -change  @rpath/libopencv_imgproc.3.3.dylib  ./lib/libopencv_imgproc.3.3.dylib  lib/libopencv_video.3.3.dylib
install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_video.3.3.dylib

install_name_tool -change  @rpath/libopencv_calib3d.3.3.dylib  ./lib/libopencv_calib3d.3.3.dylib  lib/libopencv_stitching.3.3.dylib
install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_stitching.3.3.dylib
install_name_tool -change  @rpath/libopencv_features2d.3.3.dylib  ./lib/libopencv_features2d.3.3.dylib  lib/libopencv_stitching.3.3.dylib
install_name_tool -change  @rpath/libopencv_flann.3.3.dylib  ./lib/libopencv_flann.3.3.dylib   lib/libopencv_stitching.3.3.dylib
install_name_tool -change  @rpath/libopencv_highgui.3.3.dylib  ./lib/libopencv_highgui.3.3.dylib  lib/libopencv_stitching.3.3.dylib
install_name_tool -change  @rpath/libopencv_videoio.3.3.dylib  ./lib/libopencv_videoio.3.3.dylib  lib/libopencv_stitching.3.3.dylib
install_name_tool -change  @rpath/libopencv_imgcodecs.3.3.dylib  ./lib/libopencv_imgcodecs.3.3.dylib   lib/libopencv_stitching.3.3.dylib
install_name_tool -change  @rpath/libopencv_imgproc.3.3.dylib  ./lib/libopencv_imgproc.3.3.dylib   lib/libopencv_stitching.3.3.dylib

install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_calib3d.3.3.dylib
install_name_tool -change  @rpath/libopencv_features2d.3.3.dylib  ./lib/libopencv_features2d.3.3.dylib  lib/libopencv_calib3d.3.3.dylib
install_name_tool -change  @rpath/libopencv_flann.3.3.dylib  ./lib/libopencv_flann.3.3.dylib   lib/libopencv_calib3d.3.3.dylib
install_name_tool -change  @rpath/libopencv_highgui.3.3.dylib  ./lib/libopencv_highgui.3.3.dylib  lib/libopencv_calib3d.3.3.dylib
install_name_tool -change  @rpath/libopencv_videoio.3.3.dylib  ./lib/libopencv_videoio.3.3.dylib  lib/libopencv_calib3d.3.3.dylib
install_name_tool -change  @rpath/libopencv_imgcodecs.3.3.dylib  ./lib/libopencv_imgcodecs.3.3.dylib   lib/libopencv_calib3d.3.3.dylib
install_name_tool -change  @rpath/libopencv_imgproc.3.3.dylib  ./lib/libopencv_imgproc.3.3.dylib   lib/libopencv_calib3d.3.3.dylib


install_name_tool -change  @rpath/libopencv_flann.3.3.dylib  ./lib/libopencv_flann.3.3.dylib   lib/libopencv_features2d.3.3.dylib
install_name_tool -change  @rpath/libopencv_highgui.3.3.dylib  ./lib/libopencv_highgui.3.3.dylib  lib/libopencv_features2d.3.3.dylib
install_name_tool -change  @rpath/libopencv_videoio.3.3.dylib  ./lib/libopencv_videoio.3.3.dylib  lib/libopencv_features2d.3.3.dylib
install_name_tool -change  @rpath/libopencv_imgcodecs.3.3.dylib  ./lib/libopencv_imgcodecs.3.3.dylib   lib/libopencv_features2d.3.3.dylib
install_name_tool -change  @rpath/libopencv_imgproc.3.3.dylib  ./lib/libopencv_imgproc.3.3.dylib   lib/libopencv_features2d.3.3.dylib
install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_features2d.3.3.dylib

install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_flann.3.3.dylib

install_name_tool -change  @rpath/libopencv_videoio.3.3.dylib  ./lib/libopencv_videoio.3.3.dylib  lib/libopencv_highgui.3.3.dylib 
install_name_tool -change  @rpath/libopencv_imgcodecs.3.3.dylib  ./lib/libopencv_imgcodecs.3.3.dylib   lib/libopencv_highgui.3.3.dylib 
install_name_tool -change  @rpath/libopencv_imgproc.3.3.dylib  ./lib/libopencv_imgproc.3.3.dylib   lib/libopencv_highgui.3.3.dylib
install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_highgui.3.3.dylib

install_name_tool -change  @rpath/libopencv_imgcodecs.3.3.dylib  ./lib/libopencv_imgcodecs.3.3.dylib   lib/libopencv_videoio.3.3.dylib
install_name_tool -change  @rpath/libopencv_imgproc.3.3.dylib  ./lib/libopencv_imgproc.3.3.dylib   lib/libopencv_videoio.3.3.dylib
install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_videoio.3.3.dylib

install_name_tool -change  @rpath/libopencv_videoio.3.3.dylib  ./lib/libopencv_videoio.3.3.dylib  lib/libopencv_superres.3.3.dylib 
install_name_tool -change  @rpath/libopencv_imgcodecs.3.3.dylib  ./lib/libopencv_imgcodecs.3.3.dylib   lib/libopencv_superres.3.3.dylib
install_name_tool -change  @rpath/libopencv_imgproc.3.3.dylib  ./lib/libopencv_imgproc.3.3.dylib   lib/libopencv_superres.3.3.dylib
install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_superres.3.3.dylib

install_name_tool -change  @rpath/libopencv_calib3d.3.3.dylib  ./lib/libopencv_calib3d.3.3.dylib  lib/libopencv_videostab.3.3.dylib
install_name_tool -change  @rpath/libopencv_features2d.3.3.dylib  ./lib/libopencv_features2d.3.3.dylib  lib/libopencv_videostab.3.3.dylib
install_name_tool -change  @rpath/libopencv_flann.3.3.dylib  ./lib/libopencv_flann.3.3.dylib   lib/libopencv_videostab.3.3.dylib
install_name_tool -change  @rpath/libopencv_highgui.3.3.dylib  ./lib/libopencv_highgui.3.3.dylib  lib/libopencv_videostab.3.3.dylib
install_name_tool -change  @rpath/libopencv_videoio.3.3.dylib  ./lib/libopencv_videoio.3.3.dylib  lib/libopencv_videostab.3.3.dylib
install_name_tool -change  @rpath/libopencv_imgcodecs.3.3.dylib  ./lib/libopencv_imgcodecs.3.3.dylib   lib/libopencv_videostab.3.3.dylib
install_name_tool -change  @rpath/libopencv_imgproc.3.3.dylib  ./lib/libopencv_imgproc.3.3.dylib   lib/libopencv_videostab.3.3.dylib
install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_videostab.3.3.dylib
install_name_tool -change  @rpath/libopencv_video.3.3.dylib  ./lib/libopencv_video.3.3.dylib lib/libopencv_videostab.3.3.dylib
install_name_tool -change  @rpath/libopencv_photo.3.3.dylib  ./lib/libopencv_photo.3.3.dylib lib/libopencv_videostab.3.3.dylib


install_name_tool -change  @rpath/libopencv_imgproc.3.3.dylib  ./lib/libopencv_imgproc.3.3.dylib lib/libopencv_photo.3.3.dylib
install_name_tool -change  @rpath/libopencv_core.3.3.dylib  ./lib/libopencv_core.3.3.dylib  lib/libopencv_photo.3.3.dylib

install_name_tool -change  /usr/local/lib/libssl.1.1.dylib  ./lib/libssl.1.1.dylib     lib/libwebsockets.12.dylib
install_name_tool -change  /usr/local/lib/libcrypto.1.1.dylib ./lib/libcrypto.1.1.dylib     lib/libwebsockets.12.dylib
install_name_tool -change  /usr/local/lib/libcrypto.1.1.dylib ./lib/libcrypto.1.1.dylib     lib/libssl.1.1.dylib


也可参考:

http://matthew-brett.github.io/docosx/mac_runtime_link.html#install-names-and-static-absolute-or-relative-paths




你可能感兴趣的:(Mac OpenCV 修改dylib的默认链接位置 : Library not loaded … Reason: Image not loaded)