CGAL 5.6.1 - Manual
How to use CGAL with CMake

This page will explain how to manually create a CMakeLists.txt file to link a custom program with CGAL.

A base can be created using the script cgal_create_CMakeLists. Its usage is detailed in Section Creating a CMake Script for a Program Using CGAL.

Linking with CGAL

To link with the CGAL library, use the following:

find_package(CGAL REQUIRED)
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 imported targets to link with CGAL dependencies that can be found in the section Compilers and Third Party Dependencies.

Note
The CGAL targets define the following compiler flags:
  • -frounding-math with gcc
  • /fp:strict /fp:except- with MSVC

Minimal Example Using Qt5

This section describes a minimal example of a program that uses CGAL and Qt5 for some GUI features.

CMakeLists.txt

cmake_minimum_required(VERSION 3.1...3.23)
project(Surface_mesh_Examples)

#CGAL_Qt5 is needed for the drawing.
find_package(CGAL REQUIRED OPTIONAL_COMPONENTS Qt5)
create_single_source_cgal_program("sm_points.cpp")
create_single_source_cgal_program("sm_derivation.cpp")
create_single_source_cgal_program("sm_join.cpp")
create_single_source_cgal_program("sm_aabbtree.cpp")
create_single_source_cgal_program("sm_bgl.cpp")
create_single_source_cgal_program("sm_circulators.cpp")
create_single_source_cgal_program("sm_do_intersect.cpp")
create_single_source_cgal_program("sm_iterators.cpp")
create_single_source_cgal_program("sm_kruskal.cpp")
create_single_source_cgal_program("sm_memory.cpp")
create_single_source_cgal_program("sm_properties.cpp")
create_single_source_cgal_program("sm_draw_small_faces.cpp")
create_single_source_cgal_program("check_orientation.cpp")
#create the executable of the application
create_single_source_cgal_program("draw_surface_mesh.cpp")
if(CGAL_Qt5_FOUND)
#link it with the required CGAL libraries
target_link_libraries(draw_surface_mesh PUBLIC CGAL::CGAL_Basic_viewer)
target_link_libraries(sm_draw_small_faces PUBLIC CGAL::CGAL_Basic_viewer)
endif()

draw_surface_mesh.cpp

#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/draw_surface_mesh.h>
#include <fstream>
typedef Kernel::Point_3 Point;
int main(int argc, char* argv[])
{
const std::string filename = (argc>1) ? argv[1] : CGAL::data_file_path("meshes/elephant.off");
Mesh sm;
if(!CGAL::IO::read_polygon_mesh(filename, sm))
{
std::cerr << "Invalid input file." << std::endl;
return EXIT_FAILURE;
}
// Internal color property maps are used if they exist and are called "v:color", "e:color" and "f:color".
auto vcm = sm.add_property_map<Mesh::Vertex_index, CGAL::IO::Color>("v:color").first;
auto ecm = sm.add_property_map<Mesh::Edge_index, CGAL::IO::Color>("e:color").first;
auto fcm = sm.add_property_map<Mesh::Face_index>("f:color", CGAL::IO::white() /*default*/).first;
for(auto v : vertices(sm))
{
if(v.idx()%2)
put(vcm, v, CGAL::IO::black());
else
put(vcm, v, CGAL::IO::blue());
}
for(auto e : edges(sm))
put(ecm, e, CGAL::IO::gray());
CGAL_USE(fcm);
// Draw!
return EXIT_SUCCESS;
}