简体   繁体   中英

Compiling and Running CGAL Triangulation Demo

I am trying to use the CGAL library to display a 2D Delaunay triangulation, like in the example here

My code looks like this:

#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;
}

When I try to compile this code with g++ -o cgalTest.exe cgalTest.cpp -lCGAL -lgmp the program compiles successfully, but on runtime I get Impossible to draw because CGAL_USE_BASIC_VIEWER is not defined

By searching on Google, I found someone that suggested using g++ -o cgalTest.exe cgalTest.cpp -lCGAL -lgmp -DCGAL_USE_BASIC_VIEWER which produces the following error on compile time: /usr/include/CGAL/Qt/Basic_viewer_qt.h:30:10: fatal error: QApplication: No such file or directory #include <QApplication>

I am using ubuntu 19.04, so I installed CGAl using sudo apt-get install libcgal-dev and sudo apt-get install libcgal-qt5-dev

I tried to install sudo apt-get install libqt5svg5-dev libqt5opengl5-dev as well to solve the error, but to no avail.

Do I need to install additional libraries? Maybe the compilation must be done differently?

Thank you

Ok, for anyone facing the same problem, here is how I solved it:

First, I used the locate QApplication command to find the location of the QApplication header file on my system. Be sure to run sudo updatedb before using locate . If locate doesn't find the location of QApplication then you are missing qt libraries. Try sudo apt-get install qt5-default and the other libraries I mentioned in my question, run sudo updatedb and try locate QApplication again.

When you find the path to QApplication just use the -I option to instruct the compiler to use it. Here is an example g++ -o delaunayTest delaunayTest.cpp -lCGAL -lgmp -lCGAL_Qt5 -DCGAL_USE_BASIC_VIEWER -I/usr/include/x86_64-linux-gnu/qt5/QtWidgets/ (because in my case, QApplication was inside the directory /usr/include/x86_64-linux-gnu/qt5/QtWidgets/ )

Trying to compile with this, you will probably get another header file error. Repeat the same process using locate until you get no more header file errors.

At that point, you will likely encounter an undefined reference to symbol error.

To solve this, use locate again to find the location of the file that caused the error (for example libQt5OpenGL.so.5 ) and add the path to the compilation command as is (for example g++ -o delaunayTest delaunayTest.cpp /usr/lib/x86_64-linux-gnu/libQt5OpenGL.so.5 ) along with all the previous options.

You will probably get several undefined reference to symbol errors as well. Just keep using the same method until you don't get any.

At this point the program should compile properly and run properly.

Note that if you have multiple versions of qt installed, then the above might not work properly (If for example you have software that uses qt like MATLAB or anaconda installed in your system. You will know because locate will produce many paths for each file on the steps above). In such a case, I suggest building a Virtual Machine, downloading the CGAL libraries and qt5-default and following the above steps there, since it is very likely this won't work in a system with multiple qt installations.

Another option (maybe the easiest), using CMake, is to generate the file using the builtin script:

  1. From the source file directory, run cgal_create_CMakeLists -c Qt5
  2. Edit the generated CMakeLists.txt adding the line add_definitions(-DCGAL_USE_BASIC_VIEWER)

My generated and edited 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. Create a build directory: mkdir build
  2. Change directory: cd build
  3. Generate the build files: cmake ..
  4. Build: make
  5. Run the binary file. For me it's ./b because my source file was b.cpp

Environment:

These instructions should work for an Ubuntu 20.04.1 (or similar) with the packages libcgal-dev , libcgal-qt5-dev and qtbase5-dev installed (and, of course, cmake , make and g++ ).

Main references:

doc1 and doc2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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