2013年7月26日星期五

[总结] Library not loaded: /opt/local/lib/libiconv.2.dylib 错误解决之法

1. 背景
工作中需要用到 php 的一个图形库的扩展模块 gmagick.
而目前使用的 php 在编译时并没有将 gmagick 静态编译进去,于是通过编译扩展模块来实现.
扩展模块的编译很顺利,但挂在 php 上后发现没有生效.写的简单的脚本测试发现 php error.log 中报如下的错

PHP Warning:  PHP Startup: Unable to load dynamic library '/Users/hy0kl/php/ext/gmagick.so' - dlopen(/Users/hy0kl/php/ext/gmagick.so, 9): Library not loaded: /opt/local/lib/libiconv.2.dylib
  Referenced from: /Users/hy0kl/php/ext/gmagick.so
  Reason: Incompatible library version: gmagick.so requires version 8.0.0 or later, but libiconv.2.dylib provides version 7.0.0 in Unknown on line 0

谷歌后发现,我的机器中有两个版的 libiconv:
$ otool -LD /usr/lib/libiconv.2.dylib
/usr/lib/libiconv.2.dylib:
/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.0.0)

$ otool -LD /opt/local/lib/libiconv.2.dylib
/opt/local/lib/libiconv.2.dylib:
/opt/local/lib/libiconv.2.dylib (compatibility version 8.0.0, current version 8.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
初步确认是动态的加载顺序没有搞对.
多次尝试 + 不断的 google,终于发现了下面的有用帮助:
https://code.google.com/p/git-osx-installer/issues/detail?id=18

Found out that it wasn't a problem running git as root.

I had this in my personal .profile:
export DYLD_LIBRARY_PATH=/usr/lib
export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib:/sw/lib

Hashed it out and everything was fine..
Don't remember why I put it in there in the first place.. :-/


大家千万不要移除或重命名系统 /usr/lib 下的 libiconv,否则系统报各种问题,生不如死.

2. 解决方法:
$ vi ~/.bashrc

DYLD_LIBRARY_PATH=$HOME/local/lib:/usr/local/lib:/usr/local/mysql/lib
export DYLD_LIBRARY_PATH

DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib:/usr/lib
export DYLD_FALLBACK_LIBRARY_PATH

加入以后内容,通过小改变一下动态库的扫描路径的先后顺序,即可解决了

没有评论: