title: How do I build/compile Fortran with MinGW gfortran via CMake?
body: <p>I've got much more I've got to get figured out with CMake than just the following problem, but it's the first and simplest one which I still can't get past. I've scoured the interwebs and even borrowed the '<a href="https://rads.stackoverflow.com/amzn/click/com/1930934319" rel="nofollow noreferrer" rel="nofollow noreferrer">Mastering CMake</a>' book from a friend, but I'm still having the hardest time... A lot of stuff exists online with regards to CMake, Fortran, and MinGW, and even combinations of two at a time. But all three together seem to be almost non-existent.</p>
  
  <p>All I want to do (at this point) is get a simple Fortran program built and compiling using CMake on Windows, using MinGW's gfortran compiler.</p>
  
  <p>...And I'm a CMake n00b.</p>
  
  <p>This is what I've been working with so far:</p>
  
  <p><strong>CMakeLists.txt:</strong></p>
  
  <pre><code>project(cmake_test Fortran)
  add_executable(testf test.f90)
  </code></pre>
  
  <p><strong>test.f90:</strong></p>
  
  <pre><code>      program test
        write(*,*)"hello world"
        endprogram test
  </code></pre>
  
  <p>I've got the MSYS2 version of MinGW, since that's the only version that the code I'm eventually going to be compiling will compile with on Windows. (Ie. when I compile it with my own Makefile in the MSYS2 shell, it compiles.)</p>
  
  <p>I've got my Windows Path appended with <code>;C:\msys64\mingw64\bin</code>. (I've also tried <code>;C:\msys64\usr\bin</code>, but it complains about <code>sh.exe</code> being in the same directory, among other issues.)</p>
  
  <p>Then I pop open the CMake-GUI, load in the CMakeLists above, hit Configure, specify the generator for the project to be "MinGW Makefiles", select "Use default native compilers", and get the following output:</p>
  
  <pre><code>The Fortran compiler identification is GNU 5.4.0
  Check for working Fortran compiler: C:/msys64/mingw64/bin/gfortran.exe
  Check for working Fortran compiler: C:/msys64/mingw64/bin/gfortran.exe  -- works
  Detecting Fortran compiler ABI info
  Detecting Fortran compiler ABI info - done
  Checking whether C:/msys64/mingw64/bin/gfortran.exe supports Fortran 90
  Checking whether C:/msys64/mingw64/bin/gfortran.exe supports Fortran 90 -- yes
  Configuring done
  </code></pre>
  
  <p>Then I click Configure again and get:</p>
  
  <pre><code>Configuring done
  </code></pre>
  
  <p>Then Generate:</p>
  
  <pre><code>Generating done
  </code></pre>
  
  <p>In my build directory there is then a Makefile and a number of other files and directories.</p>
  
  <p>I try running make in the MSYS2 shell, and I get this:</p>
  
  <pre><code>myself@COMPUTER MSYS /c/users/myself/desktop/dll_test/with_fortran_cmake/build
  $ make
  Microsoft Windows [Version 6.1.7601]
  Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
  
  C:\users\myself\desktop\dll_test\with_fortran_cmake\build&gt;
  </code></pre>
  
  <p>That last line is a prompt. If I type stuff like <code>make</code> it seems to run it again and it just brings up the prompt again, within the prompt. If I hit Ctrl+C, it kills it and returns to the normal MSYS2 prompt.</p>
  
  <p>So I can't figure out how to actually make it, assuming I'm even doing the CMake part right.</p>
  
  <p><strong>Question: How do I get this example code to build/compile/run given the constraints I've listed?</strong></p>
  
  <p>(What I'd actually rather do, once I get past this part, is get it to work with Visual Studio 13, since I have a C++ project being built with CMake (written mostly by someone else to whom I have limited access for questions and aid) from which I want to be able to call my Fortran. Once I get the Fortran into a library of some sort which is callable by the C++ from Visual Studio, the Fortran can pretty much just be left alone as a pre-built library. I know that editing Fortran from VS is not really much of a possibility, and I'm not interested in doing it.)</p>
  
  <p>Here are the contents of the generated <strong><code>Makefile</code></strong> (note my editor replaced tabs with spaces when I copied it here):</p>
  
  <pre><code># CMAKE generated file: DO NOT EDIT!
  # Generated by "MinGW Makefiles" Generator, CMake Version 3.5
  
  # Default target executed when no arguments are given to make.
  default_target: all
  
  .PHONY : default_target
  
  # Allow only one "make -f Makefile2" at a time, but pass parallelism.
  .NOTPARALLEL:
  
  
  #=============================================================================
  # Special targets provided by cmake.
  
  # Disable implicit rules so canonical targets will work.
  .SUFFIXES:
  
  
  # Remove some rules from gmake that .SUFFIXES does not remove.
  SUFFIXES =
  
  .SUFFIXES: .hpux_make_needs_suffix_list
  
  
  # Suppress display of executed commands.
  $(VERBOSE).SILENT:
  
  
  # A target that is always out of date.
  cmake_force:
  
  .PHONY : cmake_force
  
  #=============================================================================
  # Set environment variables for the build.
  
  SHELL = cmd.exe
  
  # The CMake executable.
  CMAKE_COMMAND = "C:\Program Files (x86)\CMake\bin\cmake.exe"
  
  # The command to remove a file.
  RM = "C:\Program Files (x86)\CMake\bin\cmake.exe" -E remove -f
  
  # Escaping for special characters.
  EQUALS = =
  
  # The top-level source directory on which CMake was run.
  CMAKE_SOURCE_DIR = C:\Users\myself\Desktop\dll_test\with_fortran_cmake
  
  # The top-level build directory on which CMake was run.
  CMAKE_BINARY_DIR = C:\Users\myself\Desktop\dll_test\with_fortran_cmake\build
  
  #=============================================================================
  # Targets provided globally by CMake.
  
  # Special rule for the target edit_cache
  edit_cache:
      @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
      "C:\Program Files (x86)\CMake\bin\cmake-gui.exe" -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
  .PHONY : edit_cache
  
  # Special rule for the target edit_cache
  edit_cache/fast: edit_cache
  
  .PHONY : edit_cache/fast
  
  # Special rule for the target rebuild_cache
  rebuild_cache:
      @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
      "C:\Program Files (x86)\CMake\bin\cmake.exe" -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
  .PHONY : rebuild_cache
  
  # Special rule for the target rebuild_cache
  rebuild_cache/fast: rebuild_cache
  
  .PHONY : rebuild_cache/fast
  
  # The main all target
  all: cmake_check_build_system
      $(CMAKE_COMMAND) -E cmake_progress_start C:\Users\myself\Desktop\dll_test\with_fortran_cmake\build\CMakeFiles C:\Users\myself\Desktop\dll_test\with_fortran_cmake\build\CMakeFiles\progress.marks
      $(MAKE) -f CMakeFiles\Makefile2 all
      $(CMAKE_COMMAND) -E cmake_progress_start C:\Users\myself\Desktop\dll_test\with_fortran_cmake\build\CMakeFiles 0
  .PHONY : all
  
  # The main clean target
  clean:
      $(MAKE) -f CMakeFiles\Makefile2 clean
  .PHONY : clean
  
  # The main clean target
  clean/fast: clean
  
  .PHONY : clean/fast
  
  # Prepare targets for installation.
  preinstall: all
      $(MAKE) -f CMakeFiles\Makefile2 preinstall
  .PHONY : preinstall
  
  # Prepare targets for installation.
  preinstall/fast:
      $(MAKE) -f CMakeFiles\Makefile2 preinstall
  .PHONY : preinstall/fast
  
  # clear depends
  depend:
      $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles\Makefile.cmake 1
  .PHONY : depend
  
  #=============================================================================
  # Target rules for targets named testf
  
  # Build rule for target.
  testf: cmake_check_build_system
      $(MAKE) -f CMakeFiles\Makefile2 testf
  .PHONY : testf
  
  # fast build rule for target.
  testf/fast:
      $(MAKE) -f CMakeFiles\testf.dir\build.make CMakeFiles/testf.dir/build
  .PHONY : testf/fast
  
  test.obj: test.f90.obj
  
  .PHONY : test.obj
  
  # target to build an object file
  test.f90.obj:
      $(MAKE) -f CMakeFiles\testf.dir\build.make CMakeFiles/testf.dir/test.f90.obj
  .PHONY : test.f90.obj
  
  test.i: test.f90.i
  
  .PHONY : test.i
  
  # target to preprocess a source file
  test.f90.i:
      $(MAKE) -f CMakeFiles\testf.dir\build.make CMakeFiles/testf.dir/test.f90.i
  .PHONY : test.f90.i
  
  test.s: test.f90.s
  
  .PHONY : test.s
  
  # target to generate assembly for a file
  test.f90.s:
      $(MAKE) -f CMakeFiles\testf.dir\build.make CMakeFiles/testf.dir/test.f90.s
  .PHONY : test.f90.s
  
  # Help Target
  help:
      @echo The following are some of the valid targets for this Makefile:
      @echo ... all (the default if no target is provided)
      @echo ... clean
      @echo ... depend
      @echo ... testf
      @echo ... edit_cache
      @echo ... rebuild_cache
      @echo ... test.obj
      @echo ... test.i
      @echo ... test.s
  .PHONY : help
  
  
  
  #=============================================================================
  # Special targets to cleanup operation of make.
  
  # Special rule to run CMake to check the build system integrity.
  # No rule that depends on this can have commands that come from listfiles
  # because they might be regenerated.
  cmake_check_build_system:
      $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles\Makefile.cmake 0
  .PHONY : cmake_check_build_system
  </code></pre>
  
  <p><strong>Version information:</strong></p>
  
  <ul>
  <li>GNU Fortran (GCC) 5.3.0</li>
  <li>Windows 7 Enterprise</li>
  <li>Cmake 3.5.2</li>
  <li>MSYS2 - I'm not sure how to find the version for this </li>
  <li>MinGW - I'm not sure how to find the version for this</li>
  </ul>
  
  <p>I've been through a lot of different pages online during my search, and I didn't bother keeping track of them all, but this one in particular is one I keep coming across because it seems like it's very related from the title, but the actual issue and resolution are totally not:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/36831026/how-can-i-get-a-basic-fortran-file-to-compile-on-windows-mingw-using-cmake">How can I get a basic Fortran file to compile on Windows/MinGW using CMake?</a></li>
  </ul>