CGAL 5.3.2 - 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)
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 Essential and Optional 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.20)
project(Surface_mesh_Examples)

#CGAL_Qt5 is needed for the drawing.
find_package(CGAL REQUIRED OPTIONAL_COMPONENTS Qt5)
if(CGAL_Qt5_FOUND)
#required to use basic_viewer
add_definitions(-DCGAL_USE_BASIC_VIEWER -DQT_NO_KEYWORDS)
endif()

#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_Qt5)
endif()
#end of the file

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 char* filename = (argc>1) ? argv[1] : "data/elephant.off";
Mesh sm;
if(!CGAL::IO::read_polygon_mesh(filename, sm))
{
std::cerr << "Invalid input file." << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}