c++xcodeimagemagickmagick++

Problems using ImageMagick in XCode C++ application


I am attempting to use ImageMagick to load an image into my C++ application; I'm developing in XCode 7. And I'm struggling. Here is my code.

#include "Magick++.h"

int main(int argc, const char * argv[]) {
  Magick::Image img;
  return 0;
}

The error that I get follows below.

Ld /Users/ndwork/Library/Developer/Xcode/DerivedData/clsFusion-ghktzemmmuxtfidyywslbqxztyqz/Build/Products/Debug/clsFusion normal x86_64
    cd /Users/ndwork/Desktop/clsFusion
    export MACOSX_DEPLOYMENT_TARGET=10.11
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -L/Users/ndwork/Library/Developer/Xcode/DerivedData/clsFusion-ghktzemmmuxtfidyywslbqxztyqz/Build/Products/Debug -L/opt/X11/lib -F/Users/ndwork/Library/Developer/Xcode/DerivedData/clsFusion-ghktzemmmuxtfidyywslbqxztyqz/Build/Products/Debug -filelist /Users/ndwork/Library/Developer/Xcode/DerivedData/clsFusion-ghktzemmmuxtfidyywslbqxztyqz/Build/Intermediates/clsFusion.build/Debug/clsFusion.build/Objects-normal/x86_64/clsFusion.LinkFileList -mmacosx-version-min=10.11 -Xlinker -no_deduplicate -stdlib=libc++ -lX11.6 -Xlinker -dependency_info -Xlinker /Users/ndwork/Library/Developer/Xcode/DerivedData/clsFusion-ghktzemmmuxtfidyywslbqxztyqz/Build/Intermediates/clsFusion.build/Debug/clsFusion.build/Objects-normal/x86_64/clsFusion_dependency_info.dat -o /Users/ndwork/Library/Developer/Xcode/DerivedData/clsFusion-ghktzemmmuxtfidyywslbqxztyqz/Build/Products/Debug/clsFusion

Undefined symbols for architecture x86_64:
  "Magick::Image::Image()", referenced from:
      _main in main.o
  "Magick::Image::~Image()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

You can see that I've included the directory where Magick++.h is located in the header search path. I'm not sure how to resolve this. I installed ImageMagick with homebrew.

All help is appreciated!


Solution

  • If you don't have the homebrew package called pkg-config installed, install that first with:

    brew install pkg-config
    

    It can then tell you how to compile and link all packages it knows about, so let's see what packages it knows about:

    pkg-config --list-all | grep -i Magick
    

    Sample Output

    ...
    ...
    libswscale                          libswscale - FFmpeg image rescaling library
    ImageMagick                         ImageMagick - ImageMagick - convert, edit, and compose images (ABI Q16HDRI)
    vips-cpp                            vips-cpp - C++ API for vips8 image processing library
    lept                                leptonica - An open source C library for efficient image processing and image analysis operations
    MagickCore-7.Q16HDRI                MagickCore - MagickCore - C API for ImageMagick (ABI Q16HDRI)
    MagickWand                          MagickWand - MagickWand - C API for ImageMagick (ABI Q16HDRI)
    gdk-pixbuf-2.0                      GdkPixbuf - Image loading and scaling
    OpenEXR                             OpenEXR - OpenEXR image library
    vips                                vips - Image processing library
    Magick++                            Magick++ - Magick++ - C++ API for ImageMagick (ABI Q16HDRI)
    ...
    ...
    

    Ok, Magick++ looks good for you. Let's find the header file path (for #includes):

    pkg-config --cflags-only-I Magick++
    

    Sample Output

    -I/usr/local/Cellar/imagemagick/HEAD-a781824/include/ImageMagick-7
    

    and the linker flags - which is actually your issue:

    pkg-config --libs Magick++
    

    Sample Output

    -L/usr/local/Cellar/imagemagick/HEAD-a781824/lib -lMagick++-7.Q16HDRI -lMagickWand-7.Q16HDRI -lMagickCore-7.Q16HDRI
    

    Obviously, your values will be different depending on what build-time options you built ImageMagick with - so use your own values, not mine.

    Now you just need to put those into Xcode - so click the red-outlined boxes in order and edit in the details you learned above, namely:

    enter image description here

    You should also define the same defines as suggested by:

    pkg-config --cflags-only-other Magick++
    

    Sample Output

    -DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16
    

    in a place that you are happy with.