CGAL 6.1 - Manual
Loading...
Searching...
No Matches
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 Qt6

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

CMakeLists.txt

cmake_minimum_required(VERSION 3.12...3.29)
project(Surface_mesh_Examples)
#CGAL_Qt6 is needed for the drawing.
find_package(CGAL REQUIRED OPTIONAL_COMPONENTS Qt6)
#create the executable of the application
create_single_source_cgal_program("draw_surface_mesh.cpp")
if(CGAL_Qt6_FOUND)
#link it with the required CGAL libraries
target_link_libraries(draw_surface_mesh PRIVATE CGAL::CGAL_Basic_viewer)
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 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: " << filename << 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()); }
put(fcm, *(sm.faces().begin()), CGAL::IO::red());
// Draw!
return EXIT_SUCCESS;
}
std::pair< Property_map< I, T >, bool > add_property_map(std::string name=std::string(), const T t=T())
bool read_polygon_mesh(const std::string &fname, Graph &g, const NamedParameters &np=parameters::default_values())
void draw(const P &p, const GSOptions &gso)