How to do it

Our CMakeLists.txt for the C++ and Fortran examples will follow a template that is largely similar between the two languages:

  1. Both define a minimum CMake version, project name, and language (CXX or Fortran; we will show the C++ version):
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)project(recipe-05 LANGUAGES CXX)
  1. For the C++ example, we require the C++11 standard:
set(CMAKE_CXX_STANDARD 11)set(CMAKE_CXX_EXTENSIONS OFF)set(CMAKE_CXX_STANDARD_REQUIRED ON)
  1. Both call find_package to search for OpenMP:
find_package(OpenMP REQUIRED)
  1. Finally, we define the executable target and link to the imported target provided by the FindOpenMP module (in the Fortran case, we link against OpenMP::OpenMP_Fortran):
add_executable(example ...

Get CMake Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.