0

除了以下问题,我还需要用 CMake 解决更多问题,但这是我仍然无法解决的第一个也是最简单的问题。我已经搜索了互联网,甚至从朋友那里借了“掌握 CMake ”一书,但我仍然度过了最艰难的时光......关于 CMake、Fortran 和 MinGW 甚至组合的很多东西都存在于网上一次两个。但是这三者加起来似乎几乎不存在。

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

...而且我是 CMake n00b。

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

CMakeLists.txt:

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

测试.f90:

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

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

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

然后我打开 CMake-GUI,加载上面的 CMakeLists,点击配置,将项目的生成器指定为“MinGW Makefiles”,选择“使用默认的本机编译器”,得到以下输出:

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 中运行 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,它会杀死它并返回到正常的 MSYS2 提示符。

所以我无法弄清楚如何实际制作它,假设我什至正确地完成了 CMake 部分。

问题:在我列出的约束条件下,如何让这个示例代码构建/编译/运行?

(一旦我通过了这部分,我实际上更愿意做的是让它与 Visual Studio 13 一起工作,因为我有一个用 CMake 构建的 C++ 项目(主要由我对问题的访问权限有限的其他人编写我希望能够从中调用我的 Fortran库。我知道从 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

版本信息:

  • GNU Fortran (GCC) 5.3.0
  • Windows 7 企业版
  • Cmake 3.5.2
  • MSYS2 - 我不知道如何找到这个版本
  • MinGW - 我不知道如何找到这个版本

在搜索过程中,我在网上浏览了很多不同的页面,我并没有费心去跟踪它们,但是这个特别是我经常遇到的一个,因为它似乎与标题非常相关,但是实际问题和解决方案完全不是:

4

2 回答 2

3

这是一个快速的 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
于 2016-09-28T14:59:46.613 回答
0

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

感谢大卫格雷森对原始问题的评论,我找到了部分解决方案。“部分”,因为它使用 f95 而不是 gfortran。我发布它是因为它可能对其他人有用,如果我能够弄清楚如何让它与 gfortran 一起工作,我会更新它。

原来主要问题是一个非常简单的错误:我使用的是“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.

然后这工作(我再次单击配置,单击生成,然后make通过目录中的 MSYS2 终端运行/build),但正如您在以下输出中看到的那样,它使用 f95 而不是 gfortran:

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

我仍在努力让它使用 gfortran,如果我弄明白了,我会更新这个解决方案。

编辑:

好的,这显然更像是一种黑客攻击,我确信有更好的解决方案。我重命名C:\msys64\usr\bin\f95.exe为别的东西(这样 MSYS2 在找到 gfortran 之前就不会发现它是另一个 Fortran 编译器)。我还必须清除 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

努力弄清楚如何以“正确”的方式做到这一点。

编辑:

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

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

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

我还设置了以下内容,但这只是为了验证它是否正确调用了 gfortran,如下面的输出所示:

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

于 2016-09-28T15:16:52.850 回答