c++cmakeimagemagick

How to link ImageMagic static libraries successfully?


I compiled source code so I can use build files to my program as a third party resources. When I link IM with its dynamic libraries it makes sense, however, when it comes to static libraries, linking error happens:

2021-08-02 15-49-44 的屏幕截图

Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
project(image_magic_compare)

set(CMAKE_CXX_STANDARD 14)


link_directories(ImageMagick/build/lib)

add_executable(image_magic_compare main.cpp)

target_include_directories(${PROJECT_NAME} PUBLIC ImageMagick/build/include/ImageMagick-7)
target_link_libraries(${PROJECT_NAME} PUBLIC libMagickCore-7.Q16HDRI.a)
target_link_libraries(${PROJECT_NAME} PUBLIC libMagickWand-7.Q16HDRI.a)
target_link_libraries(${PROJECT_NAME} PUBLIC libMagick++-7.Q16HDRI.a)

main.cpp:

#include <iostream>
#include "Magick++.h"

using namespace Magick;
int main() {
    system("export LD_LIBRARY_PATH=LD_LIBRARY_PATH:./lib/");
    std::string source_image, target_image, diff_image;
    double fuzz = 0.0;
    std::cout << "input source image path: ";
    std::cin >> source_image;
    std::cout << "\ninput target image path: ";
    std::cin >> target_image;
    std::cout << "\ninput fuzz threshold: ";
    std::cin >> fuzz;
    std::cout << "\ninput diff image path: ";
    std::cin >> diff_image;

//    Magick::InitializeMagick()
    Image image1, image2;
    image1.read(source_image);
    image2.read(target_image);
//    auto res = image1.compare(image2);
    Magick::ExceptionInfo e;

    Image &ref = image2;
    Image diff = image1.compare(ref, MagickCore::MetricType::FuzzErrorMetric, &fuzz);
    diff.write(diff_image);
//    CompareImages();
    std::cout << "finish compare!!!" << std::endl;
    return 0;
}

File construction of ImageMagick build directory:

ImageMagick/build/lib/
├── dump.txt
├── ImageMagick-7.1.0
│   └── config-Q16HDRI
│       └── configure.xml
├── libMagick++-7.Q16HDRI.a
├── libMagick++-7.Q16HDRI.la
├── libMagick++-7.Q16HDRI.so -> libMagick++-7.Q16HDRI.so.5.0.0
├── libMagick++-7.Q16HDRI.so.5 -> libMagick++-7.Q16HDRI.so.5.0.0
├── libMagick++-7.Q16HDRI.so.5.0.0
├── libMagickCore-7.Q16HDRI.a
├── libMagickCore-7.Q16HDRI.la
├── libMagickCore-7.Q16HDRI.so -> libMagickCore-7.Q16HDRI.so.10.0.0
├── libMagickCore-7.Q16HDRI.so.10 -> libMagickCore-7.Q16HDRI.so.10.0.0
├── libMagickCore-7.Q16HDRI.so.10.0.0
├── libMagickWand-7.Q16HDRI.a
├── libMagickWand-7.Q16HDRI.la
├── libMagickWand-7.Q16HDRI.so -> libMagickWand-7.Q16HDRI.so.10.0.0
├── libMagickWand-7.Q16HDRI.so.10 -> libMagickWand-7.Q16HDRI.so.10.0.0
├── libMagickWand-7.Q16HDRI.so.10.0.0
└── pkgconfig
    ├── ImageMagick-7.Q16HDRI.pc
    ├── ImageMagick.pc
    ├── Magick++-7.Q16HDRI.pc
    ├── MagickCore-7.Q16HDRI.pc
    ├── MagickCore.pc
    ├── Magick++.pc
    ├── MagickWand-7.Q16HDRI.pc
    └── MagickWand.pc

3 directories, 25 files

Can Someone help me?


Solution

  • I made it work by using following CMakeLists.txt:

    cmake_minimum_required(VERSION 3.20)
    project(image_magic_compare)
    set(CMAKE_AUTOGEN_VERBOSE on)
    set(CMAKE_CXX_STANDARD 14)
    set(ImageMagickLib
            Magick++-7.Q16HDRI
            MagickWand-7.Q16HDRI
            MagickCore-7.Q16HDRI
            jbig
            lcms2
            tiff
            freetype
            jpeg
            lqr-1
            glib-2.0
            png16
            z
            djvulibre
            fontconfig
            freetype
            Xext
            Xt
            SM
            ICE
            X11
            lzma
            bz2
            IlmImf
            xml2
        )
    
    
    link_directories(ImageMagick/static_build/lib)
    add_executable(image_magic_compare main.cpp)
    target_compile_definitions(${PROJECT_NAME} PUBLIC MAGICKCORE_HDRI_ENABLE=1)
    target_compile_definitions(${PROJECT_NAME} PUBLIC MAGICKCORE_QUANTUM_DEPTH=16)
    target_include_directories(${PROJECT_NAME} PUBLIC ImageMagick/static_build/include/ImageMagick-7)
    
    target_link_options(${PROJECT_NAME} PUBLIC -fopenmp)
    foreach(item IN LISTS ImageMagickLib)
        message(STATUS item=${item})
        target_link_libraries(${PROJECT_NAME} ${item})
    endforeach()