在CMake (c++, ImageMagick)中设置路径

Set path in CMake (C++, ImageMagick)

本文关键字:设置 路径 ImageMagick CMake c++      更新时间:2023-10-16

我正试图添加一些东西到使用CMake开发的更大的c++项目。在我要添加的部分,我想使用magick++。

如果我只编译我的小示例程序

#include <Magick++.h>
int main()
{
  Magick::Image image;
  return 0;
}

g++ -o example example.cxx

它失败了,因为它没有找到" magick++ .h"。

如果我使用

g++ -I /usr/include/ImageMagick -o example example.cxx

我得到"未定义的引用"错误。

如果我按照http://www.imagemagick.org/script/magick++.php上的说明使用

编译
g++ `Magick++-config --cxxflags --cppflags` -o example example.cxx `Magick++-config --ldflags --libs`

:我如何将其纳入使用CMake的更大项目?我如何改变CMakeLists.txt?

有FindImageMagick。cmake模块在基本的cmake发行版,所以你是幸运的。你应该在CMakeLists.txt:

中添加如下内容
find_package(ImageMagick COMPONENTS Magick++)

之后,可以使用以下变量:

ImageMagick_FOUND                    - TRUE if all components are found.
ImageMagick_INCLUDE_DIRS             - Full paths to all include dirs.
ImageMagick_LIBRARIES                - Full paths to all libraries.
ImageMagick_<component>_FOUND        - TRUE if <component> is found.
ImageMagick_<component>_INCLUDE_DIRS - Full path to <component> include dirs.
ImageMagick_<component>_LIBRARIES

所以你可以直接写

include_directories(${ImageMagick_INCLUDE_DIRS})
target_link_libraries(YourApp ${ImageMagick_LIBRARIES})