Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cmake with fortran

Tags:

cmake

fortran

As good as cmake is claimed to be, its documentation seems to leave a lot to be desired (unless I'm totally failing at understanding the basics).

I'm trying to write a cmake file for a set of fortran programs which also depend on some other libraries (which are already compiled for my system, but I will want to make a cmake file for these libraries too).

A lot of the commands which I've found in examples online don't appear in the official docs, which is a bit disconcerting.. for example, http://www.vtk.org/Wiki/CMakeForFortranExample contains the line

get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)

yet when I include this in my own CMakeLists.txt I get an error

Missing variable is:
CMAKE_fortran_COMPILER

What am I missing?!

Edit: current CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

enable_language(fortran)
project(fortranTest)
get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)

Edit 2

I still haven't worked out how to include dependent libraries.

like image 843
ChrisW Avatar asked Oct 03 '12 09:10

ChrisW


1 Answers

The language names are case sensitive. As posted, I get:

~/cmt> cmake ./
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_fortran_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_fortran_COMPILER
CMake Error: Could not find cmake module file:/home/tgallagher/cmt/CMakeFiles/CMakefortranCompiler.cmake
CMake Error: Could not find cmake module file:CMakefortranInformation.cmake
CMake Error: CMAKE_fortran_COMPILER not set, after EnableLanguage
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
^C

However, by changing the file to (note it is Fortran and not fortran in enable_language():

cmake_minimum_required(VERSION 2.6)

enable_language(Fortran)
project(fortranTest)
get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)

You get:

~/cmt> cmake ./
-- The Fortran compiler identification is GNU
-- Check for working Fortran compiler: /usr/bin/gfortran
-- Check for working Fortran compiler: /usr/bin/gfortran  -- works
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Checking whether /usr/bin/gfortran supports Fortran 90
-- Checking whether /usr/bin/gfortran supports Fortran 90 -- yes
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
^C

Which, if permitted to finish, would work as expected. However, there are better ways to identify the compiler for the purposes of determining compiler flags. The method used in the cited example only works on systems where the compiler is not wrapped, which means systems that don't invoke the compiler through something like mpif90 or ftn on Cray. A better method is to check:

if(${CMAKE_Fortran_COMPILER_ID} STREQUAL "Intel")
...
endif()

The list of possible names can be found by inspecting the file in the CMake Module directory Modules/CMakeFortranCompilerId.F.in

like image 122
tpg2114 Avatar answered Sep 21 '22 13:09

tpg2114