简体   繁体   English

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

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

I am new to CMake. I was trying to use ImageMagick's c++ api Magick++ in my code.我是 CMake 的新手。我试图在我的代码中使用 ImageMagick 的 c++ api Magick++

This is my whole directory structure:这是我的整个目录结构:

在此处输入图像描述

external/image_magick just contains the result of cloning the image magick library using: git submodule add https://github.com/ImageMagick/ImageMagick.git external/image_magick . external/image_magick仅包含使用以下方法克隆图像 magick库的结果: git submodule add https://github.com/ImageMagick/ImageMagick.git external/image_magick

This is the top-level CMakeLists.txt ( the one in the pic above):这是顶级CMakeLists.txt (上图中的那个):

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

This is main.cpp (it just crops the image magick image and saves it, just for the demo):这是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;
}

If I compile and run the app like this ( as per image magick docs ):如果我像这样编译并运行应用程序( 根据图像 magick 文档):

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

Then all is good and image is generated.然后一切都很好并生成图像。

But I can't use the CMakeLists.txt to build and run like this:但是我不能像这样使用CMakeLists.txt来构建和运行:

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

Because the external/image_magick directory I cloned does not contain CMakeLists.txt .因为我克隆的external/image_magick目录不包含CMakeLists.txt I tried searching inside that directory for the library file (something like libmagic++ ??) to use it like below in my top level CMakeLists.txt but I didn't know how to do it:我尝试在该目录中搜索库文件(类似于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

So how to properly add this library to my app while keeping using CMAke?那么如何在继续使用 CMAke 的同时将这个库正确地添加到我的应用程序中呢?

I have had a similar problem, and it was solved by instead of using link_libraries , using the set command like this:我有一个类似的问题,它是通过使用这样的set命令而不是使用link_libraries来解决的:

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

I understand this is not the intended use for this, but this was the way I got my project building.我知道这不是它的预期用途,但这是我构建项目的方式。

Also, expect many problems with libraries with C/C++, as everyone has many , many , MANY questions that need asking.此外,预计 C/C++ 库会出现很多问题,因为每个人都有很多很多很多问题需要问。

According to this answer , the solution was to add the following to CMakeLists.txt :根据这个答案,解决方案是将以下内容添加到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