How to do it

The corresponding CMakeLists.txt contains the following building blocks:

  1. We define the minimum CMake version, the project name and supported languages:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(recipe-04 LANGUAGES CXX C Fortran)
  1. We require the C++11 standard:
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
  1. Further, we verify whether Fortran and C/C++ compilers work together and generate the header file which will take care of name mangling. Both functions are provided by the FortranCInterface module:
include(FortranCInterface)

FortranCInterface_VERIFY(CXX)

FortranCInterface_HEADER(
fc_mangle.h
MACRO_NAMESPACE "FC_"
SYMBOLS DSCAL DGESV
)
  1. We then ask CMake to find BLAS and LAPACK. These are required dependencies:
find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)
  1. Next, we add a library with our sources for the BLAS and LAPACK wrappers and link against LAPACK_LIBRARIES which brings in also BLAS_LIBRARIES:
add_library(math "")

target_sources(math
PRIVATE
CxxBLAS.cpp
CxxLAPACK.cpp
)

target_include_directories(math
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
)

target_link_libraries(math
PUBLIC
${LAPACK_LIBRARIES}
)
  1. Notice that the include directories and link libraries for this target are declared as PUBLIC and therefore any additional target depending on the math library will also set these directories in its include directories.
  2. Finally, we add an executable target and link to math:
add_executable(linear-algebra "")

target_sources(linear-algebra
PRIVATE
linear-algebra.cpp
)

target_link_libraries(linear-algebra
PRIVATE
math
)
  1. In the configuration step we can focus on the relevant output:
$ mkdir -p build
$ cd build
$ cmake ..

...
-- Detecting Fortran/C Interface
-- Detecting Fortran/C Interface - Found GLOBAL and MODULE mangling
-- Verifying Fortran/C Compiler Compatibility
-- Verifying Fortran/C Compiler Compatibility - Success
...
-- Found BLAS: /usr/lib/libblas.so
...
-- A library with LAPACK API found.
...
  1. Finally, we build and test the executable:
$ cmake --build .
$ ./linear-algebra 1000

C_DSCAL done
C_DGESV done
info is 0
check is 1.54284e-10