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

Debian Sid编译CGAL程序出现mpfr_srcptr类型转换错误

问题描述

在Debian Sid系统中,已从官方仓库安装libcgal-dev 5.5.1-2、libmpfr-dev 4.1.1-1及其依赖,编译一段实现2D Delaunay三角剖分的CGAL程序时,出现invalid conversion from ‘mpfr_srcptr’ {aka ‘const __mpfr_struct*’} to ‘mpfr_ptr’ {aka ‘__mpfr_struct*’}编译错误。MPFR版本在Debian稳定版和Sid中一致。

程序代码:

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Delaunay_triangulation_2<K> Delaunay;

typedef K::Point_2 Point_2;
typedef std::vector<Point_2> Points;

int main()
{
    Points points;
    points.push_back(Point_2(0,0));
    points.push_back(Point_2(0,7));
    points.push_back(Point_2(7,0));
    points.push_back(Point_2(7,7));

    Delaunay dt(points.begin(), points.end());

    // 打印2D Delaunay三角剖分中三角形顶点的笛卡尔坐标
    for (Delaunay::Finite_faces_iterator it = dt.finite_faces_begin(); it != dt.finite_faces_end(); it++)
    {
        std::cout << " " << dt.triangle(it)[0].x() << " " << dt.triangle(it)[0].y() << " ";
        std::cout << " " << dt.triangle(it)[1].x() << " " << dt.triangle(it)[1].y() << " ";
        std::cout << " " << dt.triangle(it)[2].x() << " " << dt.triangle(it)[2].y() << " ";
        std::cout << std::endl << "-------------------" << std::endl;
    }
    return 0;
}

编译命令及错误信息:

$ g++ -Wall test_cgal.cc -o test_cgal -lcgal
In file included from /usr/include/CGAL/boost_mp.h:54,
                 from /usr/include/CGAL/Number_types/internal/Exact_type_selector.h:27,
                 from /usr/include/CGAL/Exact_kernel_selector.h:26,
                 from /usr/include/CGAL/Filtered_kernel.h:22,
                 from /usr/include/CGAL/Exact_predicates_inexact_constructions_kernel.h:21,
                 from hola.cc:1:
/usr/include/CGAL/GMP/Gmpfr_type.h: In constructor ‘CGAL::Gmpfr::Gmpfr(mpfr_srcptr)’:
/usr/include/CGAL/GMP/Gmpfr_type.h:199:25: error: invalid conversion from ‘mpfr_srcptr’ {aka ‘const __mpfr_struct*’} to ‘mpfr_ptr’ {aka ‘__mpfr_struct*’} [-fpermissive]
  199 |                         mpfr_custom_get_kind(f),
      |                         ^
      |                         |
      |                         mpfr_srcptr {aka const __mpfr_struct*}
/usr/include/CGAL/GMP/Gmpfr_type.h: In constructor ‘CGAL::Gmpfr::Gmpfr(mpfr_srcptr, std::float_round_style, Precision_type)’:
/usr/include/CGAL/GMP/Gmpfr_type.h:216:33: error: invalid conversion from ‘mpfr_srcptr’ {aka ‘const __mpfr_struct*’} to ‘mpfr_ptr’ {aka ‘__mpfr_struct*’} [-fpermissive]
  216 |                                 mpfr_custom_get_kind(f),
      |                                 ^
      |                                 |
      |                                 mpfr_srcptr {aka const __mpfr_struct*}
/usr/include/CGAL/GMP/Gmpfr_type.h: In constructor ‘CGAL::Gmpfr::Gmpfr(mpfr_srcptr, Precision_type)’:
/usr/include/CGAL/GMP/Gmpfr_type.h:237:33: error: invalid conversion from ‘mpfr_srcptr’ {aka ‘const __mpfr_struct*’} to ‘mpfr_ptr’ {aka ‘__mpfr_struct*’} [-fpermissive]
  237 |                                 mpfr_custom_get_kind(f),
      |                                 ^
      |                                 |
      |                                 mpfr_srcptr {aka const __mpfr_struct*}
报错原因及解决办法

原因

这个错误是CGAL 5.5.1版本与MPFR库的API兼容性问题:CGAL的Gmpfr_type.h中调用mpfr_custom_get_kind函数时,传入了const类型的指针(mpfr_srcptr),但该函数在当前Debian Sid的MPFR库中要求的是非const指针(mpfr_ptr),导致类型转换失败。虽然MPFR版本号是4.1.1,但Debian Sid可能对MPFR打了补丁或启用了特定编译选项,改变了该函数的参数签名;或者CGAL 5.5.1的代码未适配MPFR的某些API细节。

另外,你使用的Exact_predicates_inexact_constructions_kernel内核默认基于double类型,本不需要依赖MPFR,出现MPFR相关错误可能是编译时未正确获取CGAL的编译参数,导致链接了不必要的MPFR依赖。

解决办法

  • 使用正确的编译命令:用pkg-config自动获取CGAL的编译和链接参数,避免手动指定库导致的依赖问题:
    g++ -Wall test_cgal.cc -o test_cgal `pkg-config --cflags --libs cgal`
    
  • 升级CGAL版本:CGAL 5.6及以后版本修复了该MPFR兼容性问题,可通过第三方源或源码编译升级到更高版本。
  • 手动修改CGAL头文件:如果无法升级,可编辑/usr/include/CGAL/GMP/Gmpfr_type.h,将报错行中的mpfr_custom_get_kind(f)改为mpfr_custom_get_kind(const_cast<mpfr_ptr>(f)),强制转换指针类型。
  • 更换内核类型:如果不需要精确计算,继续使用Exact_predicates_inexact_constructions_kernel,确保编译时不引入MPFR依赖;或者改用Exact_predicates_exact_constructions_kernel并确认依赖配置正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 20:25:54