我如何通过 CMake build/compile Fortran 与 MinGW gfortran?

How do I build/compile Fortran with MinGW gfortran via CMake?

除了下面的问题,我还有很多我需要用 CMake 解决的问题,但这是第一个也是最简单的问题,但我仍然无法通过。我搜索了互联网,甚至从朋友那里借了“Mastering CMake”这本书,但我仍然度过了最艰难的时光……网上有很多关于 CMake、Fortran 和 MinGW 的东西,甚至一次组合两个。不过三个加起来好像差不多non-existent.

我(此时)想做的就是使用 MinGW 的 gfortran 编译器在 Windows 上使用 CMake 构建和编译一个简单的 Fortran 程序。

...我是 CMake n00b。

这是我到目前为止一直在使用的:

CMakeLists.txt:

project(cmake_test Fortran)
add_executable(testf test.f90)

test.f90:

      program test
      write(*,*)"hello world"
      endprogram test

我有 MSYS2 版本的 MinGW,因为这是我最终要编译的代码将在 Windows 上编译的唯一版本。 (即,当我在 MSYS2 shell 中用我自己的 Makefile 编译它时,它会编译。)

我的 Windows 路径附加了 ;C:\msys64\mingw64\bin。 (我也试过 ;C:\msys64\usr\bin,但它抱怨 sh.exe 在同一目录中,以及其他问题。)

然后我打开 CMake-GUI,加载上面的 CMakeLists,点击 Configure,将项目的生成器指定为 "MinGW Makefiles"、select "Use default native compilers",并获得以下输出:

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

然后我再次点击配置得到:

Configuring done

然后生成:

Generating done

在我的构建目录中有一个 Makefile 和许多其他文件和目录。

我在 MSYS2 shell 中尝试 运行ning make,我得到了这个:

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>

最后一行是提示符。如果我键入 make 之类的内容,它似乎会再次 运行 它并且它只会在提示中再次弹出提示。如果我按 Ctrl+C,它会杀死它并 returns 到正常的 MSYS2 提示符。

所以我不知道如何实际制作它,假设我什至正确地完成了 CMake 部分。

问题:根据我列出的约束条件,我如何将此示例代码设为 build/compile/run?

(我实际上更愿意做的是,一旦我通过了这部分,就让它与 Visual Studio 13 一起工作,因为我有一个用 CMake 构建的 C++ 项目(主要由其他人编写我对问题和帮助的访问权限有限),我希望能够从中调用我的 Fortran。一旦我将 Fortran 放入某种可由 C++ 从 Visual Studio 调用的库中,Fortran 就可以几乎只是作为一个 pre-built 库单独存在。我知道从 VS 编辑 Fortran 的可能性不大,我对此不感兴趣。)

下面是生成的Makefile的内容(注意我复制到这里时我的编辑器用空格替换了制表符):

# 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

版本信息:

在搜索过程中,我在网上浏览了很多不同的页面,但我没有费心去跟踪所有这些页面,但是这个页面尤其是我经常遇到的页面,因为它看起来非常相关从标题来看,但实际问题和解决方案完全不是:

这是一个快速 shell 会话,展示了我如何使用 MSYS2、cmake、make 和 gfortran 构建您的 Fortran 程序。您应该尝试 运行 我执行的相同命令,看看它们是否给出不同的输出,然后调查原因。

MSYSTEM 变量尤为重要;由你启动MSYS2时点击的快捷方式决定。

$ echo $MSYSTEM
MINGW64

$ which cmake
/mingw64/bin/cmake

$ which gfortran
/mingw64/bin/gfortran

$ which make
/usr/bin/make

$ ls
CMakeLists.txt  test.f90

$ cat CMakeLists.txt
project(cmake_test Fortran)
add_executable(testf test.f90)

$ cat test.f90
      program test
      write(*,*)"hello world"
      endprogram test

$ mkdir build && cd build

$ cmake -G"MSYS Makefiles" ..
-- The Fortran compiler identification is GNU 6.2.0
-- Check for working Fortran compiler: D:/msys64/mingw64/bin/gfortran.exe
-- Check for working Fortran compiler: D:/msys64/mingw64/bin/gfortran.exe  -- works
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Checking whether D:/msys64/mingw64/bin/gfortran.exe supports Fortran 90
-- Checking whether D:/msys64/mingw64/bin/gfortran.exe supports Fortran 90 -- yes
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/david/Documents/scraps/test_fortran/build

$ make
Scanning dependencies of target testf
[ 50%] Building Fortran object CMakeFiles/testf.dir/test.f90.obj
[100%] Linking Fortran executable testf.exe
[100%] Built target testf

$ ./testf.exe
 hello world

编辑:这里有一个可行的解决方案——读到最后!

感谢 David Grayson 对原始问题的评论,我找到了部分解决方案。 "Partial" 因为它使用 f95 而不是 gfort运行。我发布它是因为它可能对其他人有用,如果我能弄清楚如何让它与 gfort运行 一起工作,我会更新它。

原来主要问题是一个非常简单的错误:我使用的是 "MinGW Makefiles" 而不是 "MSYS Makefiles"。

然而,当我只更改它时,当我单击“配置”时,我在 CMake-GUI 中得到了以下输出:

CMake Error: CMake was unable to find a build program corresponding to "MSYS Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMake was unable to find a build program corresponding to "MSYS Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_Fortran_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_AR was not found, please set to archive program. Configuring incomplete, errors occurred!

为了解决这个问题,我再次更改了 Windows 路径。我一直在使用 ;C:\msys64\mingw64\bin,所以我将其切换为 ;C:\msys64\usr\bin

这就奏效了(我第二次单击“配置”,单击“生成”,然后通过 /build 目录中的 MSYS2 终端 运行 make),但是如您所见在以下输出中,它使用 f95 而不是 gfort运行:

The Fortran compiler identification is GNU 5.3.0
Check for working Fortran compiler: C:/msys64/usr/bin/f95.exe
Check for working Fortran compiler: C:/msys64/usr/bin/f95.exe  -- works
Detecting Fortran compiler ABI info
Detecting Fortran compiler ABI info - done
Checking whether C:/msys64/usr/bin/f95.exe supports Fortran 90
Checking whether C:/msys64/usr/bin/f95.exe supports Fortran 90 -- yes
Configuring done

我仍在努力让它使用 gfort运行,如果我想通了,我会更新这个解决方案。

编辑:

好吧,这显然更像是一个 hack,我相信有更好的解决方案。我将 C:\msys64\usr\bin\f95.exe 重命名为其他名称(这样 MSYS2 就不会在找到 gfort运行 之前发现它是另一个 Fort运行 编译器)。我还必须清除 CMake 的缓存并重新启动它。但现在可以了:

The Fortran compiler identification is GNU 5.3.0
Check for working Fortran compiler: C:/msys64/usr/bin/gfortran.exe
Check for working Fortran compiler: C:/msys64/usr/bin/gfortran.exe  -- works
Detecting Fortran compiler ABI info
Detecting Fortran compiler ABI info - done
Checking whether C:/msys64/usr/bin/gfortran.exe supports Fortran 90
Checking whether C:/msys64/usr/bin/gfortran.exe supports Fortran 90 -- yes
Configuring done

正在研究如何以 "correct" 的方式做到这一点。

编辑:

好吧,我猜这是更合适的方法,因为我认为它基本上与从那里调用 CMake 时在命令行上设置环境变量做同样的事情。

在 CMake-GUI 中,我按照之前的说明设置了所有内容,但在第一次单击“配置”之前,我单击了带有小加号的 "Add Entry" 按钮。然后我设置了两个新的缓存条目——尽管只有一个是真正必要的:

Name: CMAKE_Fortran_COMPILER
Type: FILEPATH
Value: C:\msys64\usr\bin\gfortran.exe

我还设置了以下内容,但这只是为了验证它是否正确调用了 gfort运行,正如您将在下面的输出中看到的那样:

Name: CMAKE_VERBOSE_MAKEFILE
Type: BOOL
Value: [True]

然后,运行 make 在 MSYS2 终端中,我得到以下信息:

$ make
"/C/Program Files (x86)/CMake/bin/cmake.exe" -H/C/Users/myself/Desktop/dll_test/with_fortran_cmake -B/C/Users/myself/Desktop/dll_test/with_fortran_cmake/build --check-build-system CMakeFiles/Makefile.cmake 0
"/C/Program Files (x86)/CMake/bin/cmake.exe" -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
make[1]: Entering directory '/c/Users/myself/Desktop/dll_test/with_fortran_cmake/build'
make -f CMakeFiles/testf.dir/build.make CMakeFiles/testf.dir/depend
make[2]: Entering directory '/c/Users/myself/Desktop/dll_test/with_fortran_cmake/build'
"/C/Program Files (x86)/CMake/bin/cmake.exe" -E cmake_depends "MSYS Makefiles" /C/Users/myself/Desktop/dll_test/with_fortran_cmake /C/Users/myself/Desktop/dll_test/with_fortran_cmake /C/Users/myself/Desktop/dll_test/with_fortran_cmake/build /C/Users/myself/Desktop/dll_test/with_fortran_cmake/build /C/Users/myself/Desktop/dll_test/with_fortran_cmake/build/CMakeFiles/testf.dir/DependInfo.cmake --color=
Scanning dependencies of target testf
make[2]: Leaving directory '/c/Users/myself/Desktop/dll_test/with_fortran_cmake/build'
make -f CMakeFiles/testf.dir/build.make CMakeFiles/testf.dir/requires
make[2]: Entering directory '/c/Users/myself/Desktop/dll_test/with_fortran_cmake/build'
make[2]: Nothing to be done for 'CMakeFiles/testf.dir/requires'.
make[2]: Leaving directory '/c/Users/myself/Desktop/dll_test/with_fortran_cmake/build'
make -f CMakeFiles/testf.dir/build.make CMakeFiles/testf.dir/build
make[2]: Entering directory '/c/Users/myself/Desktop/dll_test/with_fortran_cmake/build'
[ 50%] Building Fortran object CMakeFiles/testf.dir/test.f90.obj
/C/msys64/usr/bin/gfortran.exe     -c /C/Users/myself/Desktop/dll_test/with_fortran_cmake/test.f90 -o CMakeFiles/testf.dir/test.f90.obj
[100%] Linking Fortran executable testf.exe
"/C/Program Files (x86)/CMake/bin/cmake.exe" -E remove -f CMakeFiles/testf.dir/objects.a
/C/msys64/usr/bin/ar.exe cr CMakeFiles/testf.dir/objects.a @CMakeFiles/testf.dir/objects1.rsp
/C/msys64/usr/bin/gfortran.exe     -Wl,--whole-archive CMakeFiles/testf.dir/objects.a -Wl,--no-whole-archive  -o testf.exe -Wl,--out-implib,libtestf.dll.a -Wl,--major-image-version,0,--minor-image-version,0
make[2]: Leaving directory '/c/Users/myself/Desktop/dll_test/with_fortran_cmake/build'
[100%] Built target testf
make[1]: Leaving directory '/c/Users/myself/Desktop/dll_test/with_fortran_cmake/build'
"/C/Program Files (x86)/CMake/bin/cmake.exe" -E cmake_progress_start /C/Users/myself/Desktop/dll_test/with_fortran_cmake/build/CMakeFiles 0

生成的程序可通过 MSYS2 终端和 Windows 命令提示符运行。

...现在我需要弄清楚如何在 Visual Studio 中将这些与 C++ 结合在一起。请继续关注更多 SO 问题! :D