Search code examples
c++g++cgal

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


Solution

  • 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.