簡體   English   中英

如何使用 CMake 將 ImageMagick/Magick++ 添加到我的 c++ 項目中?

[英]How to add ImageMagick/Magick++ to my c++ project with CMake?

我是 CMake 的新手。我試圖在我的代碼中使用 ImageMagick 的 c++ api Magick++

這是我的整個目錄結構:

在此處輸入圖像描述

external/image_magick僅包含使用以下方法克隆圖像 magick庫的結果: git submodule add https://github.com/ImageMagick/ImageMagick.git external/image_magick

這是頂級CMakeLists.txt (上圖中的那個):

cmake_minimum_required (VERSION 3.22.1)
project(DEMO)
add_executable(${PROJECT_NAME} main.cpp)

這是main.cpp (它只是裁剪圖像 magick 圖像並保存它,僅用於演示):

#include <iostream>

#include <Magick++.h>

using namespace std;

using namespace Magick;

int main()
{
    cout << "Hello World!" << endl;

    // Construct the image object. Seperating image construction from the
    // the read operation ensures that a failure to read the image file
    // doesn't render the image object useless.
    Image image;
    try
    {
        // Read a file into image object
        image.read("logo:");

        // Crop the image to specified size (width, height, xOffset, yOffset)
        image.crop(Geometry(100, 100, 100, 100));

        // Write the image to a file
        image.write("logo.png");
        printf("Image written to logo.png");
    }
    catch (Exception &error_)
    {
        cout << "Caught exception: " << error_.what() << endl;
        printf("Error: %s", error_.what());
        return 1;
    }

    return 0;
}

如果我像這樣編譯並運行應用程序( 根據圖像 magick 文檔):

c++ main.cpp -o main.out `Magick++-config --cppflags --cxxflags --ldflags --libs`
./main.out

然后一切都很好並生成圖像。

但是我不能像這樣使用CMakeLists.txt來構建和運行:

cmake -S . -B out/build
cd out/build; make
cd out/build
./DEMO

因為我克隆的external/image_magick目錄不包含CMakeLists.txt 我嘗試在該目錄中搜索庫文件(類似於libmagic++ ??)以在我的頂級CMakeLists.txt中使用它,但我不知道該怎么做:

add_subdirectory(external/image_magick/Magick++)

target_include_directories(${PROJECT_NAME} 
    PUBLIC external/image_magick/
)
target_link_directories(${PROJECT_NAME} 
PRIVATE external/image_magick/Magick++
)
target_link_libraries(${PROJECT_NAME} 
    PUBLIC ${PROJECT_SOURCE_DIR}/Magick++
)
# DOES NOT WORK

那么如何在繼續使用 CMAke 的同時將這個庫正確地添加到我的應用程序中呢?

我有一個類似的問題,它是通過使用這樣的set命令而不是使用link_libraries來解決的:

set(CMAKE_CXX_FLAGS "-LlibraryPath -lMagick++ ${CMAKE_CXX_FLAGS}")

我知道這不是它的預期用途,但這是我構建項目的方式。

此外,預計 C/C++ 庫會出現很多問題,因為每個人都有很多很多很多問題需要問。

根據這個答案,解決方案是將以下內容添加到CMakeLists.txt中:

#ImageMagick
add_definitions( -DMAGICKCORE_QUANTUM_DEPTH=16 )
add_definitions( -DMAGICKCORE_HDRI_ENABLE=0 )
find_package(ImageMagick COMPONENTS Magick++)
include_directories(${ImageMagick_INCLUDE_DIRS})
target_link_libraries(demo ${ImageMagick_LIBRARIES})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM