CGAL 5.1.4 - 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

# Created by the script cgal_create_cmake_script
# This is the CMake script for compiling a CGAL application.
cmake_minimum_required(VERSION 3.1...3.15)
project( Surface_mesh_Examples )
if(POLICY CMP0053)
# Only set CMP0053 to OLD with CMake<3.10, otherwise there is a warning.
if(NOT POLICY CMP0070)
cmake_policy(SET CMP0053 OLD)
else()
cmake_policy(SET CMP0053 NEW)
endif()
endif()
if(POLICY CMP0071)
cmake_policy(SET CMP0071 NEW)
endif()
#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()
if ( CGAL_FOUND )
#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()
else()
message(STATUS "This program requires the CGAL library, and will not be compiled.")
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[])
{
Mesh sm1;
std::ifstream in1((argc>1)?argv[1]:"data/elephant.off");
in1 >> sm1;
CGAL::draw(sm1);
return EXIT_SUCCESS;
}