Getting Mono to play nice with ImageMagick on a Mac.
Mono doesn't ship with the greatest image manipulation library. Thankfully, there's a great, free library called ImageMagick that does the job and then some.
So, I started on a quest to interop some of the ImageMagick functionality with C# in Mono. But the first thing I had to do was install ImageMagick. And thus started my day of debugging.
I installed ImageMagick with MacPorts. It seemed simple enough:
sudo port install ImageMagick
And away went MacPorts, compiling and configuring and being an overall good little package manager.
MacPorts installed the ImageMagick libraries in opt/local/lib. The specific file I was interested in was /opt/local/lib/libMagickWand.dylib. First, you've got to create a Wand object, so I got out my interop pen and wrote some interop:
[DllImport("libMagickWand", EntryPoint = "MagickWandGenesis")] static extern void WandGenesis();
The Mono runtime is supposed to resolve the "libMagickWand" path to the actual path of "libMagickWand.dylib" on the system. Unfortunately, there's a known bug in the process for Mac computers that's documented here, so you have to add a little configuration to your config files:
<configuration> <dllmap dll="libMagickWand" target="/opt/local/lib/libMagickWand.5.dylib" /> </configuration>
That should help point the runtime to the correct library. Ok. Let's invoke WandGenesis(); and start her up...
DllNotFoundException: libMagickWand. Ok. Maybe this isn't going to be that easy.
The advice I get is to run my executable with MONO_LOG_LEVEL set to debug. Let's try that.
export MONO_LOG_LEVEL=debug mono ImageMagickWrapper.exe
And I see some interesting logs that point me to the root of my problem.
Mono: DllImport error loading library 'dlopen(/opt/local/lib/libMagickWand.5.dylib, 9): no suitable image found. Did find: /opt/local/lib/libMagickWand.5.dylib: mach-o, but wrong architecture'.
Wrong architecture, eh?
lipo -info libMagickWand.5.dylib Non-fat file: libMagickWand.5.dylib is architecture: x86_64
Ok, so we've got a 64-bit version of ImageMagick. Now let's check out our Mono runtime.
mono -V Mono JIT compiler version 2.10.5 (tarball Mon Aug 22 20:38:08 EDT 2011) Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com TLS: normal SIGSEGV: normal Notification: kqueue Architecture: x86 Disabled: none Misc: debugger softdebug LLVM: yes(2.9svn-mono) GC: Included Boehm (with typed GC)
And there you have it. I've got a 32-bit installation of Mono and a 64-bit installation of ImageMagick. Now I'm off to try to figure out how to compile ImageMagick as 32-bit.