Skip to content

How to use CGAL with CMake or your own build system

Mael Rouxel-Labbé edited this page Mar 28, 2024 · 1 revision

Draft

How to use CGAL with CMake

Since CGAL-4.12, linking with the CGAL library using the following syntax is sufficient:

find_package(CGAL)
add_executable(my_executable my_source_file.cpp)
target_link_libraries(my_executable CGAL::CGAL)

Other CGAL libraries are linked similarly. For example, with CGAL_Core:

find_package(CGAL REQUIRED COMPONENTS Core)
target_link_libraries(my_executable CGAL::CGAL CGAL::CGAL_Core)

There are also some cmake macros to link with CGAL dependency libraries. For example, to link with eigen, you can use

CGAL_target_use_Eigen(<target>)

see this page for a list of such macros.

Minimal Example Using Qt5:

CMakeLists.txt :

cmake_minimum_required(VERSION 3.1)
project(test_cgal)
#CGAL_Qt5 is needed for the drawing and CGAL_Core is needed for this special Kernel.
find_package(CGAL REQUIRED COMPONENTS Qt5 Core)
if(CGAL_FOUND AND CGAL_Qt5_FOUND)
  #required to use basic_viewer
  add_definitions(-DCGAL_USE_BASIC_VIEWER -DQT_NO_KEYWORDS)
  #create the executable of the application
  add_executable(test_ test.cpp)
  #link it with the required CGAL libraries
  target_link_libraries(test_ CGAL::CGAL CGAL::CGAL_Qt5 CGAL::CGAL_Core)
else()
  message("ERROR: this program requires CGAL and CGAL_Qt5 and will not be compiled.")
endif()

test.cpp:

#include <CGAL/Exact_predicates_exact_constructions_kernel_with_sqrt.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/boost/graph/helpers.h>
#include <CGAL/draw_surface_mesh.h>

typedef CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt K;
typedef K::Point_3 Point;
typedef CGAL::Surface_mesh<Point> Mesh;


int main()
{
  Mesh m;
  CGAL::make_icosahedron<Mesh, Point>(m);
  CGAL::draw(m);
  return 0;
}

How to use CGAL without CMake

  • Document -DCGAL_HEADER_ONLY

TODO

  • Document that we use special compiler flags:

    • -frounding-math with gcc
    • /fp:strict /fp:except- with MSVC

    Actually, we should probably try to switch to using pragmas instead of those flags.

Clone this wiki locally