【问题标题】:Compiling and Running CGAL Triangulation Demo编译和运行 CGAL 三角剖分演示
【发布时间】:2019-06-13 13:38:51
【问题描述】:

我正在尝试使用 CGAL 库来显示 2D Delaunay 三角剖分,如示例 here

我的代码如下所示:

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

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Delaunay_triangulation_2<K> Triangulation;
typedef Triangulation::Point Point;

int main(void){
    Point a(1,1), b(2,1), c(2,2), d(1,2);

    Triangulation T;
    T.insert(a);
    T.insert(b);
    T.insert(c);
    T.insert(d);

    CGAL::draw(T);

    return 0;
}

当我尝试使用g++ -o cgalTest.exe cgalTest.cpp -lCGAL -lgmp 编译此代码时,程序编译成功,但在运行时我得到Impossible to draw because CGAL_USE_BASIC_VIEWER is not defined

通过谷歌搜索,我发现有人建议使用g++ -o cgalTest.exe cgalTest.cpp -lCGAL -lgmp -DCGAL_USE_BASIC_VIEWER,这会在编译时产生以下错误:/usr/include/CGAL/Qt/Basic_viewer_qt.h:30:10: fatal error: QApplication: No such file or directory #include &lt;QApplication&gt;

我使用的是 ubuntu 19.04,所以我使用 sudo apt-get install libcgal-devsudo apt-get install libcgal-qt5-dev 安装了 CGAl

我也尝试安装sudo apt-get install libqt5svg5-dev libqt5opengl5-dev以解决错误,但无济于事。

我需要安装额外的库吗?也许编译必须以不同的方式进行?

谢谢

【问题讨论】:

  • Manu qt5 库是必需的:Core、Gui、Help、OpenGL、Script、ScriptTools、Svg、Widgets、qcollectiongenerator(带有 sqlite 驱动插件)和 Xml。参看。安装手册doc.cgal.org/latest/Manual/installation.html#title25。您还需要与 CGAL_Qt5 链接
  • 我检查过,我已经安装了 libqt5core5a libqt5gui5 libqt5help5 libqt5script5 libqt5scrpttools5 libqt5xml5libqt5widgets5。我也尝试链接 CGAL_Qt5 但错误仍然存​​在。
  • 您使用的是什么版本的 CGAL?图形示例是相对较新的(4.13 IIRC)。此外,您应该使用 cmake 而不是自己做所有事情。它将使收集所有依赖项变得更加容易,如果您缺少任何依赖项,则会警告您。
  • 我应该使用 4.14 版。不是 100% 确定,因为我使用 sudo apt-get install libcgal-dev libcgal-qt5-dev 安装了 cgal。有什么方法可以确认版本吗?您建议使用 cmake。您将如何使用 cmake 编译上述项目?

标签: c++ g++ cgal


【解决方案1】:

好的,对于遇到同样问题的任何人,这是我的解决方法:

首先,我使用locate QApplication 命令在我的系统上查找QApplication 头文件的位置。请务必在使用locate 之前运行sudo updatedb。如果locate 没有找到QApplication 的位置,那么您缺少qt 库。尝试sudo apt-get install qt5-default 和我在问题中提到的其他库,运行sudo updatedb 并再次尝试locate QApplication

当您找到QApplication 的路径时,只需使用-I 选项来指示编译器使用它。这是一个示例g++ -o delaunayTest delaunayTest.cpp -lCGAL -lgmp -lCGAL_Qt5 -DCGAL_USE_BASIC_VIEWER -I/usr/include/x86_64-linux-gnu/qt5/QtWidgets/(因为在我的情况下,QApplication 在目录/usr/include/x86_64-linux-gnu/qt5/QtWidgets/ 内)

试图用这个编译,你可能会得到另一个头文件错误。使用locate 重复相同的过程,直到不再出现头文件错误。

此时,您可能会遇到undefined reference to symbol 错误。

要解决这个问题,请再次使用locate 查找导致错误的文件的位置(例如libQt5OpenGL.so.5)并将编译命令的路径原样添加(例如g++ -o delaunayTest delaunayTest.cpp /usr/lib/x86_64-linux-gnu/libQt5OpenGL.so.5)以及所有以前的选项。

您可能还会遇到几个undefined reference to symbol 错误。继续使用相同的方法,直到你没有得到任何东西。

此时程序应该可以正常编译并正常运行了。

请注意,如果您安装了多个版本的 qt,那么上述可能无法正常工作(例如,如果您的系统中安装了使用 qt 的软件,例如 MATLAB 或 anaconda。您会知道,因为locate 会产生很多上述步骤中每个文件的路径)。在这种情况下,我建议构建一个虚拟机,下载 CGAL 库和qt5-default,然后按照上述步骤进行操作,因为这很可能在安装了多个 qt 的系统中不起作用。

【讨论】:

  • 这是使用 cmake 的主要原因之一:通过自动搜索您的库并设置正确的标志来简化编译和链接。例如,您可以复制 Triangulation_2 包示例的 CMakeLists.txt;并修改它以编译您的程序。然后你只需要运行cmake。见github.com/CGAL/cgal/wiki/…
【解决方案2】:

使用 CMake 的另一种选择(可能是最简单的)是使用内置脚本生成文件:

  1. 从源文件目录,运行cgal_create_CMakeLists -c Qt5
  2. 编辑生成的CMakeLists.txt 添加行add_definitions(-DCGAL_USE_BASIC_VIEWER)

我生成并编辑的CMakeLists.txt

# Created by the script cgal_create_CMakeLists
# This is the CMake script for compiling a set of CGAL applications.

cmake_minimum_required(VERSION 3.1...3.15)

project( tmp_cgal )

# CGAL and its components
find_package( CGAL QUIET COMPONENTS Qt5 )

if ( NOT CGAL_FOUND )

  message(STATUS "This project requires the CGAL library, and will not be compiled.")
  return()  

endif()

add_definitions(-DCGAL_USE_BASIC_VIEWER)  # <==== I've added this


# Boost and its components
find_package( Boost REQUIRED )

if ( NOT Boost_FOUND )

  message(STATUS "This project requires the Boost library, and will not be compiled.")

  return()  

endif()

# include for local directory

# include for local package


# Creating entries for all C++ files with "main" routine
# ##########################################################


create_single_source_cgal_program( "b.cpp" )


  1. 创建build目录:mkdir build
  2. 更改目录:cd build
  3. 生成构建文件:cmake ..
  4. 构建:make
  5. 运行二进制文件。对我来说是./b,因为我的源文件是b.cpp

环境:

这些说明应该适用于安装了包libcgal-devlibcgal-qt5-devqtbase5-dev(当然还有cmakemakeg++)的Ubuntu 20.04.1(或类似版本) .

主要参考文献:

doc1doc2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 2020-05-06
    • 1970-01-01
    • 2014-06-28
    • 2014-07-12
    • 2016-09-16
    • 1970-01-01
    相关资源
    最近更新 更多