Error: No CMAKE_Fortran_COMPILER could be found for Visual Studio 2019 Fortran support

Viewed 5162

I am using CMAKE to build Open Source Projects (like those are available at GitHub etc.) and I also have installed Visual Studio 2019. There is a problem that CMAKE can not find Fortran compiler in my system whereas I've installed MinGW with Fortran compiler. The error is:

**The Fortran compiler identification is unknown** 
**No CMAKE_Fortran_COMPILER could be found.**

How can I solve this problem and make CMAKE be aware of Fortran compiler?

Note: I tried other projects that does not require Fortran compiler and those are built successfully.

Installed software:

CMAKE 3.18.5, Visual Studio 2019, MinGW

1 Answers

You need to show where your fortran executable file to CMakeList.txt like

set(CMAKE_Fortran_COMPILER "C:/MinGW/bin/gfortran.exe")

EDIT 2 for Visual studio 2019:

If you want to produce Visual Studio 2019 solution

  1. Download IntelĀ® oneAPI HPC Toolkit here.
  2. If cmake cannot find Fortran compiler add a cmake flag CMAKE_Fortran_COMPILER with the value of ifort.exe path that u installed above. Like %install_path%/Intel/oneAPI/compiler/2021.1.1/windows/bin/intel64/ifort.exe
  3. Configure and generate solution.

EDIT:

Alright, now I understand why you get this error. The Visual Studio generator does not support MinGW gfortran. They are totally separate ecosystems. Remove your build directory and create a fresh one. Then use cmake .. -G "MinGW Makefiles" instead. I tried from CLI prompt of msys and successfully obtained the libraries from Windows machine.

If you are using Intel processors, you should first set-up Fortran environment for Visual Studio 2019

Better to check compilation guide and some troubleshooting Fortran Integration Issues with visual studio

Related