Magick ++对Magick :: Image :: Columns的未定义引用 [英] Magick++ undefined reference to Magick::Image::Columns

查看:141
本文介绍了Magick ++对Magick :: Image :: Columns的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天, 这是我的代码

Good day, here is my code

#include <iostream>
#include <Magick++.h>
using namespace std;
using namespace Magick;
int main(int argc, char **argv) {
  InitializeMagick(*argv);
  Image image;
  try {
    image.read(argv[1]);
  } 
  catch( Exception &error_ ) {
    cout << "Caught exception: " << error_.what() << endl; 
      return 1; 
  } 
  int x = image.columns();
  cout<<"your picture's width is "<< x << "px"<<endl;
  return 0; 
}

我使用KDevelop(使用CMake作为构建器), 当我尝试编译应用程序时,它引发了一个错误

I use KDevelop(which uses CMake as builder), when I try to compile the app, it throws me an error

main.cpp:25:未定义对`Magick :: Image :: columns()const'的引用

main.cpp:25: undefined reference to `Magick::Image::columns() const'

这是我的CMakeLists.txt包含的内容.

Here's what my CMakeLists.txt contains.

cmake_minimum_required(VERSION 3.5)
project(hello)

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

set(SOURCE_FILES main.cpp)

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

我发现当CMakeLists编写不正确时,未定义的引用经常会出现问题,但是我是根据

I figured out that there're often issues with undefined references when CMakeLists isn't written correctly, but I made it according to this About Magick++, how to write the CMakeLists?

我在哪里错了?我可以添加所需的任何信息.

where am I wrong? I can add any information needed.

UPD 1. 版本的magick ++, 8:6.8.9.9-7ubuntu5.7

UPD 1. version of magick++, 8:6.8.9.9-7ubuntu5.7

系统信息: 说明:Linux Mint 18.1 Serena

system info: Description: Linux Mint 18.1 Serena

UPD 2. 我只是删除了括号,并在尝试使用

UPD 2. I just removed parenthesis and when tryed to compile with

size_t x = image.columns;
size_t y = image.rows;

KDevelop把我扔了

KDevelop threw me

main.cpp:25:22:错误:无法从类型'size_t(Magick :: Image ::)()const {aka long unsigned int(Magick :: Image: :)()const}'键入'size_t {aka long unsigned int}'

main.cpp:25:22: error: cannot convert ‘Magick::Image::columns’ from type ‘size_t (Magick::Image::)() const {aka long unsigned int (Magick::Image::)() const}’ to type ‘size_t {aka long unsigned int}’

即使

auto x = image.columns;
auto y = image.rows;

它抛出

main.cpp:25:20:错误:无法从 类型‘size_t(Magick :: Image ::)()const {aka long unsigned int (Magick :: Image ::)()const}"键入"long unsigned int (Magick :: Image :: *)()const’

main.cpp:25:20: error: cannot convert ‘Magick::Image::columns’ from type ‘size_t (Magick::Image::)() const {aka long unsigned int (Magick::Image::)() const}’ to type ‘long unsigned int (Magick::Image::*)() const’

发生了什么事?

P.S. hooray,这是我关于stackoverflow的第一个问题! :-)

P.S. hooray, this is my first question on stackoverflow! :-)

推荐答案

如果您可以使用g++ main.cpp `Magick++-config --cxxflags --cppflags --ldflags --libs`在不使用CMake的情况下编译程序(但由于某些原因无法在CMake中使用${ImageMagick_LIBRARIES}),则可以使用CMakeLists.txt中Magick++-config的内容:

If you are able to compile your program without CMake using g++ main.cpp `Magick++-config --cxxflags --cppflags --ldflags --libs` (but for some reason cannot use ${ImageMagick_LIBRARIES} in CMake), then you can make use of Magick++-config in your CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(hello LANGUAGES CXX)

add_executable(hello main.cpp)
target_compile_features(hello PRIVATE cxx_std_11)
find_package(ImageMagick REQUIRED COMPONENTS Magick++)
target_compile_definitions(hello PRIVATE
  MAGICKCORE_QUANTUM_DEPTH=16
  MAGICKCORE_HDRI_ENABLE=0
)
target_include_directories(hello PRIVATE ${ImageMagick_INCLUDE_DIRS})

execute_process(COMMAND Magick++-config --ldflags
  OUTPUT_VARIABLE ImageMagick_LINK_FLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE
)
target_link_libraries(hello PRIVATE ${ImageMagick_LINK_FLAGS})

在这里,execute_process允许我们将Magick++-config --ldflags的结果转换为变量,我们可以将其作为标志通过target_link_libraries传递给链接器.

Here, execute_process allows us to get the result of Magick++-config --ldflags into a variable, which we can pass as flags to the linker through target_link_libraries.

此外,请注意我如何使用target_compile_features而不是设置全局CMAKE_CXX_FLAGS变量,target_compile_definitions而不是add_definitionstarget_include_directories而不是include_directories.最好在编程和CMake中都使用本地(基于目标的)命令而不是修改全局状态,因为它们可能会对线下产生不可预见的影响-在CMake的上下文中,这些全局命令会影响嵌套的子目录.项目.

Also, note how I've used target_compile_features rather than setting the global CMAKE_CXX_FLAGS variable, target_compile_definitions rather than add_definitions and target_include_directories rather than include_directories. It's better to use local (target-based) commands rather than modifying global state, both in programming and in CMake, since they can have unforeseen repercussions down the line -- in the context of CMake, those global commands would have affected nested sub-projects.

这篇关于Magick ++对Magick :: Image :: Columns的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆