简体   繁体   English

无法设置`DYLD_LIBRARY_PATH`时,如何在macOS上可移植地安装ImageMagick?

[英]How to install ImageMagick portably on macOS when I can't set `DYLD_LIBRARY_PATH`?

I am developing a command line utility for macOS (Mojave) that manipulates images using ImageMagick. 我正在为macOS(Mojave)开发命令行实用程序,该实用程序使用ImageMagick来处理图像。 I want to share it as a standalone app in such a way that others can use it out-of-the-box without having to install any additional dylibs or frameworks. 我想以独立应用程序的形式共享它,以便其他人可以直接使用它,而不必安装任何其他dylib或框架。 The Homebrew and MacPorts versions of ImageMagick seem to be "hardwired" to the Mac's system directory structure ( /usr/local and /opt/local , respectively) in such a way that it's difficult (impossible?) to put ImageMagick and its delegate libraries into a portable application bundle. ImageMagick的Homebrew和MacPorts版本似乎已“硬连接”到Mac的系统目录结构(分别为/usr/local/opt/local ),以至于很难(不可能?)放置ImageMagick及其委托库。进入便携式应用程序捆绑包。 So I am instead using the distribution directly from the ImageMagick website . 所以我改为直接使用ImageMagick网站上的发行版

I followed the installation instructions on that page and put the ImageMagick folder in my home directory (at ~myname ). 我按照该页面上的安装说明进行操作,然后将ImageMagick文件夹放在主目录中(位于~myname )。 As instructed, I did export DYLD_LIBRARY_PATH="/Users/myname/ImageMagick-7.0.8/lib/" . 按照指示,我确实export DYLD_LIBRARY_PATH="/Users/myname/ImageMagick-7.0.8/lib/" But when I run magick , I get an error message: 但是当我运行magick ,我收到一条错误消息:

$ ~myname/ImageMagick-7.0.8/bin/magick logo: test.jpg
dyld: Library not loaded: /ImageMagick-7.0.8/lib/libMagickCore-7.Q16HDRI.6.dylib
  Referenced from: /Users/myname/ImageMagick-7.0.8/bin/magick
  Reason: image not found
Abort trap: 6
$

Clearly magick isn't finding its dylibs, even when I set DYLD_LIBRARY_PATH as instructed. 显然,即使我按照指示设置了DYLD_LIBRARY_PATHmagick也没有找到它的dylib。 It appears, in fact, that I can't even export DYLD_LIBRARY_PATH into the environment: 实际上,我似乎什至无法将DYLD_LIBRARY_PATH导出到环境中:

$ export MAGICK_HOME="/Users/myname/ImageMagick-7.0.8"
$ export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib/"
$ echo $DYLD_LIBRARY_PATH
/Users/myname/ImageMagick-7.0.8/lib
$ printenv | grep DYLD_LIBRARY_PATH
   # (nothing)
$ printenv | grep ImageMagick
MAGICK_HOME=/Users/myname/ImageMagick-7.0.8
$ 

What's going on? 这是怎么回事? How can I make ImageMagick portable? 如何使ImageMagick便携式?

First of all, the version of IM currently on the ImageMagick website (version 7.0.8) is for macOS High Sierra. 首先,ImageMagick网站上当前的IM版本(版本7.0.8)适用于macOS High Sierra。 It's therefore not too surprising that you're having trouble installing it on Mojave. 因此,在Mojave上安装它遇到麻烦并不奇怪。 (FWIW, the current Homebrew version (IM 7) and MacPorts (IM 6) do, however, work on Mojave. But, like you, I don't know how to make those versions' handling of the delegates truly portable.) (FWIW,当前的Homebrew版本(IM 7)和MacPorts(IM 6)可以在Mojave上运行。但是,像您一样,我不知道如何使这些版本对委托的处理真正可移植。)

The reason you can't export DYLD_LIBRARY_PATH is because of Apple's "System Integrity Protection" (SIP) , which it added to newer versions of macOS (El Capitan and later). 您无法导出DYLD_LIBRARY_PATH的原因是由于Apple的“系统完整性保护”(SIP) ,它已将其添加到更新的macOS版本(El Capitan及更高版本)中。 By default, SIP prohibits doing things like changing the env variable DYLD_LIBRARY_PATH . 默认情况下,SIP禁止执行诸如更改环境变量DYLD_LIBRARY_PATH Although it is possible to disable SIP , Apple does not recommend doing so. 尽管可以禁用SIP ,但Apple不建议这样做。

You can, however, modify magick and its dylibs manually using install_name_tool so that IM 7.0.8 works fine on Mojave. 但是,您可以使用install_name_tool手动修改magick及其dylib,以使IM 7.0.8在Mojave上正常运行。 Here's how (in bash ): 这是( bash ):

# magick: set the correct path to libMagickCore.dylib 
install_name_tool -change \
    /ImageMagick-7.0.8/lib/libMagickCore-7.Q16HDRI.6.dylib \
    @executable_path/../lib/libMagickCore-7.Q16HDRI.6.dylib \
    /Users/myname/ImageMagick-7.0.8/bin/magick

# magick: set the correct path to libMagickWand.dylib 
install_name_tool -change \
    /ImageMagick-7.0.8/lib/libMagickWand-7.Q16HDRI.6.dylib \
    @executable_path/../lib/libMagickWand-7.Q16HDRI.6.dylib \
    /Users/myname/ImageMagick-7.0.8/bin/magick

# libMagickWand.dylib: set the correct ID
install_name_tool -id \
    @executable_path/../lib/libMagickWand-7.Q16HDRI.6.dylib \
    /Users/myname/ImageMagick-7.0.8/lib/libMagickWand-7.Q16HDRI.6.dylib

# libMagickWand.dylib: set the correct path
install_name_tool -change \
    /ImageMagick-7.0.8/lib/libMagickCore-7.Q16HDRI.6.dylib \
    @executable_path/../lib/libMagickCore-7.Q16HDRI.6.dylib \
    /Users/myname/ImageMagick-7.0.8/lib/libMagickWand-7.Q16HDRI.6.dylib

# libMagickCore.dylib: set the correct ID
install_name_tool -id \
    @executable_path/../lib/libMagickCore-7.Q16HDRI.6.dylib \
    /Users/myname/ImageMagick-7.0.8/lib/libMagickCore-7.Q16HDRI.6.dylib

Now it works: 现在可以正常工作:

$ /Users/myname/ImageMagick-7.0.8/bin/magick logo: test.jpg
$ open test.jpg
$ # (Preview opens a nice picture of the ImageMagick logo.)

This modifies the paths to the dylibs relative to the location of the magick command. 这修改了相对于magick命令位置的dylib的路径。 As long as you keep the directory structure of the ImageMagick folder intact, it should now be completely portable. 只要您保持ImageMagick文件夹的目录结构完整,它现在就应该是完全可移植的。

You can easily put those five install_name_tools commands into a little bash script. 您可以轻松地将这五个install_name_tools命令放入一个小的bash脚本中。 I'll leave that as an exercise for the reader. 我将其留给读者练习。 :) :)

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

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