You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ImageMagick C++编译失败求助:配置脚本及头文件报错问题

问题描述

安装ImageMagick时发现库文件安装在ImageMagick-7目录,执行以下命令复制文件:

saprykin@work:/usr/local/include/ImageMagick-7$ sudo cp -r * ../

编译时执行这条命令:

saprykin@work:~/image_proc$ c++ `Magick++-config --cxxflags --cppflags` -O2 -o main src/main.cpp   `Magick++-config --ldflags --libs`

出现以下错误:

/usr/local/bin/Magick++-config: 47: --cflags: not found
/usr/local/bin/Magick++-config: 50: --cflags: not found
/usr/local/bin/Magick++-config: 53: --libs: not found
/usr/local/bin/Magick++-config: 56: --libs: not found
In file included from /usr/local/include/Magick++/Include.h:16,
                 from /usr/local/include/Magick++.h:12,
                 from src/main.cpp:1:
/usr/local/include/MagickCore/magick-config.h:63:3: warning: #warning "you should set MAGICKCORE_QUANTUM_DEPTH to sensible default set it to configure time default" [-Wcpp]
   63 | # warning "you should set MAGICKCORE_QUANTUM_DEPTH to sensible default set it to configure time default"
      |   ^~~~~~~
/usr/local/include/MagickCore/magick-config.h:64:3: warning: #warning "this is an obsolete behavior please fix your makefile" [-Wcpp]
   64 | # warning "this is an obsolete behavior please fix your makefile"
      |   ^~~~~~~
/usr/local/include/MagickCore/magick-config.h:86:3: error: #error "you should set MAGICKCORE_HDRI_ENABLE"
   86 | # error "you should set MAGICKCORE_HDRI_ENABLE"
      |   ^~~~~
/usr/local/include/MagickCore/magick-config.h:121:3: error: #error "you should set MAGICKCORE_HDRI_ENABLE"
  121 | # error "you should set MAGICKCORE_HDRI_ENABLE"

附上Magick++-config脚本代码:

#!/bin/sh
#
# Configure options script for re-calling Magick+ compilation options
# required to use the Magick++ library.
#

prefix=/usr/local
exec_prefix=${prefix}

usage='Usage: Magick++-config [--cppflags] [--cxxflags] [--exec-prefix] [--ldflags] [--libs] [--prefix] [--version]

 For example, "magick.cpp" may be compiled to produce "magick" as follows:

  "c++ -o magick magick.cpp `Magick++-config --cppflags --cxxflags --ldflags --libs`"'

if test $# -eq 0; then
      echo "${usage}" 1>&2
      exit 1
fi

while test $# -gt 0; do
  case "$1" in
    -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;    *) optarg= ;;  esac
  case $1 in
    --prefix=*)      prefix=$optarg      ;;
    --prefix)      echo $prefix      ;;
    --exec-prefix=*)      exec_prefix=$optarg      ;;
    --exec-prefix)      echo $exec_prefix      ;;
    --version)      echo '7.1.1 Q16 HDRI'      ;;
    --cflags)       --cflags Magick++-7.Q16HDRI      ;;
    --cxxflags)       --cflags Magick++-7.Q16HDRI      ;;
    --cppflags)       --cflags Magick++-7.Q16HDRI      ;;
    --ldflags)       --libs Magick++-7.Q16HDRI      ;;
    --libs)       --libs Magick++-7.Q16HDRI      ;;
    *)      echo "${usage}" 1>&2      exit 1      ;;
  esac
  shift
done
解决方案

1. 修复Magick++-config脚本错误

脚本中直接写--cflags、--libs是无效命令,需要调用系统中存在的MagickCore-config工具来获取正确参数。修改后的脚本如下:

#!/bin/sh
#
# Configure options script for re-calling Magick+ compilation options
# required to use the Magick++ library.
#

prefix=/usr/local
exec_prefix=${prefix}
magick_core_config="${exec_prefix}/bin/MagickCore-config"

usage='Usage: Magick++-config [--cppflags] [--cxxflags] [--exec-prefix] [--ldflags] [--libs] [--prefix] [--version]

 For example, "magick.cpp" may be compiled to produce "magick" as follows:

  "c++ -o magick magick.cpp `Magick++-config --cppflags --cxxflags --ldflags --libs`"'

if test $# -eq 0; then
      echo "${usage}" 1>&2
      exit 1
fi

while test $# -gt 0; do
  case "$1" in
    -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;    *) optarg= ;;  esac
  case $1 in
    --prefix=*)      prefix=$optarg      ;;
    --prefix)      echo $prefix      ;;
    --exec-prefix=*)      exec_prefix=$optarg      ;;
    --exec-prefix)      echo $exec_prefix      ;;
    --version)      echo '7.1.1 Q16 HDRI'      ;;
    --cflags)       "$magick_core_config" --cflags Magick++-7.Q16HDRI      ;;
    --cxxflags)      "$magick_core_config" --cxxflags Magick++-7.Q16HDRI      ;;
    --cppflags)      "$magick_core_config" --cppflags Magick++-7.Q16HDRI      ;;
    --ldflags)       "$magick_core_config" --ldflags Magick++-7.Q16HDRI      ;;
    --libs)       "$magick_core_config" --libs Magick++-7.Q16HDRI      ;;
    *)      echo "${usage}" 1>&2      exit 1      ;;
  esac
  shift
done

保存修改后,给脚本添加执行权限:

sudo chmod +x /usr/local/bin/Magick++-config

2. 补全编译宏定义

错误提示缺少MAGICKCORE_QUANTUM_DEPTH和MAGICKCORE_HDRI_ENABLE宏定义,可直接在编译命令中添加:

c++ -DMAGICKCORE_QUANTUM_DEPTH=16 -DMAGICKCORE_HDRI_ENABLE=1 `Magick++-config --cxxflags --cppflags` -O2 -o main src/main.cpp `Magick++-config --ldflags --libs`

3. 规范修复方式(推荐)

手动复制头文件会破坏ImageMagick的目录结构,建议重新安装时指定正确路径,或改用pkg-config获取编译参数:

# 使用pkg-config替代Magick++-config
c++ `pkg-config Magick++ --cxxflags --cppflags` -O2 -o main src/main.cpp `pkg-config Magick++ --ldflags --libs`

如果系统缺少Magick++的pkg-config配置文件,重新编译安装时可添加参数--with-pkgconfigdir=/usr/lib/pkgconfig,确保生成正确的.pc文件。


内容的提问来源于stack exchange,提问作者maridj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 02:14:54