簡體   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