繁体   English   中英

编译和运行CGAL三角剖分演示

[英]Compiling and Running CGAL Triangulation Demo

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

我的代码如下所示:

#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

通过在 Google 上搜索,我发现有人建议使用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 <QApplication>

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

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

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

谢谢

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

首先,我使用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错误的undefined reference to symbol

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

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

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

请注意,如果您安装了多个版本的 qt,则上述内容可能无法正常工作(例如,如果您的系统中安装了使用 qt 的软件,如 MATLAB 或 anaconda。您会知道,因为locate将为每个文件生成许多路径)上面的步骤)。 在这种情况下,我建议构建一个虚拟机,下载 CGAL 库和qt5-default并在那里执行上述步骤,因为这很可能在具有多个 qt 安装的系统中不起作用。

使用 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM