[CMake] How to specify gfortran in an MSVC project

Thompson, KT kgt at lanl.gov
Sun Jan 13 23:05:32 EST 2019


Hi Stephen,


I am also interested in Kitware's response to your question.  I don't use Qt Creator, but I help maintain several projects that use Visual Studio and MinGW gfortran via the CMakeAddFortranSubdirectory feature.  This feature is somewhat limited and not really maintained (in my experience).


Background: In my case, I use a heavily modified version of CMakeAddFortranSubdirectory  that supports Xcode builds in addition to MSVC.  I also have dependencies of the form mycpp.exe -> myf90.lib -> mycpp.lib -> msmpi.lib that standard CMakeAddFortranSubdirectory doesn't know how to deal with.


When I run the 'build' target via Visual Studio, the Fortran compiler is discovered by CMakeAddFortranSubdirectory only if gfortran.exe is in %PATH% (via find_program -- I believe that the default code only supports the name 'gfortran.exe'.)  You might need to add C:/Qt/Tools/mingw730_64/bin/ to your local path before running cmake for the project.  I have found that CMakeAddFortranSubdirectory isn't flexible.  You may need modify it for your own needs.


-kt

________________________________
From: CMake <cmake-bounces at cmake.org> on behalf of Stephen Morris <s-morris at n-eos.com>
Sent: Sunday, January 13, 2019 6:14:33 PM
To: cmake at cmake.org
Subject: [CMake] How to specify gfortran in an MSVC project

I would like to try to use 64-bit gfortran to compile a project in which the rest of the code is compiled with Visual C++ 2017. I’ve read the netlib article (http://www.netlib.org/lapack/lawnspdf/lawn270.pdf) but it doesn’t address my problem, which is how to help CMake select the right Fortran compiler. The CMakeLists.txt file is launched from within Qt Creator.

My CMakeLists.txt file (which I’ve tested thoroughly using gfortran in Linux and MinGW gfortran on Windows) begins:

    cmake_minimum_required(VERSION 3.12)
    project(myProj LANGUAGES Fortran)

...and if I set Qt Creator to parse it with the MSVC compiler option selected, the output is:

    Starting to parse CMake project, using: "-DCMAKE_CXX_COMPILER:STRING=C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl.exe", "-DCMAKE_C_COMPILER:STRING=C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl.exe", "-DCMAKE_PREFIX_PATH:STRING=C:/Qt/5.9.7/msvc2017_64", "-DQT_QMAKE_EXECUTABLE:STRING=C:/Qt/5.9.7/msvc2017_64/bin/qmake.exe".
    The Fortran compiler identification is unknown
    CMake Error at CMakeLists.txt:2 (project):
      No CMAKE_Fortran_COMPILER could be found.

      Tell CMake where to find the compiler by setting either the environment
      variable "FC" or the CMake cache entry CMAKE_Fortran_COMPILER to the full
      path to the compiler, or to the compiler name if it is in the PATH.

Fair enough; here’s my second attempt:

    cmake_minimum_required(VERSION 3.12)
    set(CMAKE_Fortran_COMPILER "C:/Qt/Tools/mingw730_64/bin/gfortran.exe")
    project(myProj LANGUAGES Fortran)

...and this gives me:

    Starting to parse CMake project, using: "-DCMAKE_CXX_COMPILER:STRING=C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl.exe", "-DCMAKE_C_COMPILER:STRING=C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl.exe", "-DCMAKE_PREFIX_PATH:STRING=C:/Qt/5.9.7/msvc2017_64", "-DQT_QMAKE_EXECUTABLE:STRING=C:/Qt/5.9.7/msvc2017_64/bin/qmake.exe".
    The Fortran compiler identification is unknown
    Check for working Fortran compiler: C:/Qt/Tools/mingw730_64/bin/gfortran.exe
    CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
    Missing variable is:
    CMAKE_Fortran_PREPROCESS_SOURCE
    CMake Error at C:/Program Files/CMake/share/cmake-3.12/Modules/CMakeTestFortranCompiler.cmake:30 (try_compile):
      Failed to generate test project build system.
    Call Stack (most recent call first):
      CMakeLists.txt:3 (project)

In place of the “set(CMAKE_Fortran_COMPILER...” line I’ve also tried

    set($ENV{FC} "C:/Qt/Tools/mingw730_64/bin/gfortran.exe")

...which gave me the error message

    Could not find compiler set in environment variable FC:
         CMAKE_Fortran_COMPILER-NOTFOUND

..and
        set($ENV{FC} "C:/Qt/Tools/mingw730_64/bin")

... which gave me exactly the same error as I got in the first case.

So what am I doing wrong here?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190114/00b36550/attachment-0001.html>


More information about the CMake mailing list