关于Magick,如何编写CMakeLists?

大家.

有我的CMakeLists.

cmake_minimum_required(VERSION 3.5)
project(Blah)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(Blah ${SOURCE_FILES})

find_package(ImageMagick)
find_package(ImageMagick COMPONENTS Magick++)
find_package(ImageMagick COMPONENTS convert)
find_package(ImageMagick COMPONENTS Magick++ convert)

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

我的代码看起来像这样.

#include <iostream>
#include <Magick++.h>
using namespace std;

int main(int argc, char **argv) {
    Magick::Image image("640*480", "white");
}

它会报告一些错误,例如对’Magick :: Color :: Color(char const *)’的未定义引用.解决方案似乎我应该写一些像g’Magick -config –cxxflags –cppflags’ – 示例example.cxx’Magick -config –ldflags –libs’.

可悲的是,我只是不知道如何为它编写正确的CMakeLists项目,或者对于整个Magick来说. (我学到了一些关于CMake的基础知识.但遗憾的是没用.)

非常感谢你能帮助我!

最佳答案
ImageMagick随附Magick -config实用程序.由于您已经直接设置了CMAKE_CXX_FLAGS,因此您可以向Magick -config询问cxx& lib标志.

cmake_minimum_required(VERSION 3.5)
project(Blah)

# Find where Magick++-config lives
find_program(MAGICK_CONFIG "Magick++-config")
# Ask about CXX and lib flags/locations
execute_process(COMMAND "${MAGICK_CONFIG}" "--cxxflags" OUTPUT_VARIABLE MAGICK_CXX_FLAGS)
execute_process(COMMAND "${MAGICK_CONFIG}" "--libs" OUTPUT_VARIABLE MAGICK_LD_FLAGS)
# Remove trailing whitespace (CMAKE warns about this)
string(STRIP "${MAGICK_CXX_FLAGS}" MAGICK_CXX_FLAGS)
string(STRIP "${MAGICK_LD_FLAGS}" MAGICK_LD_FLAGS)
# Append all flags to CMAKE_CXX_FLAGS
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${MAGICK_CXX_FLAGS} ${MAGICK_LD_FLAGS}")

set(SOURCE_FILES main.cpp)

add_executable(Blah ${SOURCE_FILES})

但! CMake已经非常善于处理库和依赖关系.这也应该有效.

cmake_minimum_required(VERSION 3.5)
project(Blah)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)

add_executable(Blah ${SOURCE_FILES})
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(Blah ${ImageMagick_LIBRARIES})

转载注明原文:关于Magick,如何编写CMakeLists? - 代码日志