[CMake] How to specify gfortran in an MSVC project

Stephen Morris s-morris at n-eos.com
Mon Jan 14 03:32:11 EST 2019


Thank you KT; let's see what they say.

Like in your case, I have a project structure where my Fortran library depends on various C libraries, and my C++ application depends on those same C libraries both indirectly (through the Fortran library) and directly. I'd like to be able to use the MSVC versions of those C libraries (and as the interfaces are C, not C++, this ought to be straightforward). Currently, however, the only way I can get my Fortran code to compile is if I call it from the command line with the "MinGW Makefiles" generator, in which case it uses MinGW versions for all the other libraries as well.

Warm regards,
Stephen.

From: Thompson, KT <kgt at lanl.gov>
Sent: 14 January 2019 04:06
To: Stephen Morris <s-morris at n-eos.com>; cmake at cmake.org
Subject: Re: How to specify gfortran in an MSVC project


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<mailto:cmake-bounces at cmake.org>> on behalf of Stephen Morris <s-morris at n-eos.com<mailto:s-morris at n-eos.com>>
Sent: Sunday, January 13, 2019 6:14:33 PM
To: cmake at cmake.org<mailto: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/fc94b962/attachment.html>


More information about the CMake mailing list