除了以下问题,我还需要用 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 - 我不知道如何找到这个版本
在搜索过程中,我在网上浏览了很多不同的页面,我并没有费心去跟踪它们,但是这个特别是我经常遇到的一个,因为它似乎与标题非常相关,但是实际问题和解决方案完全不是: