[英]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-dev
和sudo 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 的另一個選項(可能是最簡單的)是使用內置腳本生成文件:
cgal_create_CMakeLists -c Qt5
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" )
build
目錄: mkdir build
cd build
cmake ..
make
./b
因為我的源文件是b.cpp
環境:
這些說明應該適用於安裝了libcgal-dev
、 libcgal-qt5-dev
和qtbase5-dev
(當然還有cmake
、 make
和g++
)的 Ubuntu 20.04.1(或類似版本)。
主要參考:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.