everyone.
There is my 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})
And my code looks like this.
#include <iostream>
#include <Magick++.h>
using namespace std;
int main(int argc, char **argv) {
Magick::Image image("640*480", "white");
}
It reports some errors like undefined reference to 'Magick::Color::Color(char const*)'
. And the solution seems that I should write something like g++ 'Magick++-config --cxxflags --cppflags' -o example example.cxx 'Magick++-config --ldflags --libs'
.
Sadly, I just don't know how to write a correct CMakeLists' items for it, or, for the whole Magick++. (I learned some basics about CMake. Yet useless, sadly.)
Many thanks if you could help me!
ImageMagick ships with a Magick++-config
utility. Since you're already setting CMAKE_CXX_FLAGS
directly, you might as well ask Magick++-config
about cxx
& lib
flags.
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})
But! CMake is already very good about handling libraries & dependancies. This should also work.
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})
来源:https://stackoverflow.com/questions/37759833/about-magick-how-to-write-the-cmakelists