我能够做一个比我原来文章中的例子简单得多的例子。我正在尝试在windows上使用gcc version 4.9.2 (x86_64-posix-seh-rev4, Built by MinGW-W64 project)
编译fortran代码。
下面是我用来成功编译代码的批处理脚本:
SET SRC_DIR=code
gfortran -fopenmp -g -fimplicit-none -cpp ^
%SRC_DIR%/globals/current_precision.f90^
%SRC_DIR%/string/string.f90^
...
%SRC_DIR%/user/MOONS.f90^
parametricStudy.f90^
-o main.exe
del *.mod
输出如下:
C:UserscharlDocumentsGitHubMOONS>gfortran -fopenmp -g -fimplicit-none -cpp
code/globals/current_precision.f90
code/string/string.f90
...
code/user/MOONS.f90
parametricStudy.f90 -o main.exe
C:UserscharlDocumentsGitHubMOONS>del *.mod
然后我尝试通过更改将源路径从本地更改为完全路径
SET SRC_DIR=code
至
SET SRC_DIR=C:/Users/charl/Documents/GitHub/MOONS/code
但现在的输出是:
C:UserscharlDocumentsGitHubMOONS>gfortran -fopenmp -g -fimplicit-none -cpp
C:/Users/charl/Documents/GitHub/MOONS/code/globals/current_precision.f90
...
C:/Users/charl/Documents/GitHub/MOONS/code/solvers/induction/init_Bfield.f90
C:/Users/charl/Documents/GitHub
gfortran: error:
C:/Users/charl/Documents/GitHubMOONS/code/solvers/induction/init_Sigma.f90: No such file or directory
我还尝试了使用变量而不是,结果发生了同样的事情,所以这似乎与完整路径有关,而不是与变量的使用有关。我有两个问题
1) 使用完整路径导致错误的原因是什么?
2) 为什么完整路径中有拼写错误?("GitHubMOONS"应读作"GitHub/MOONS")
错误的原因是SRC_DIR的设置。这必须是DOS格式,即
*SET SRC_DIR=C:/Users/charl/Documents/GitHub/MOONS/code*
应该是
*SET SRC_DIR=C:UserscharlDocumentsGitHubMOONScode*
这和完整路径中拼写错误的原因相同。
使用批处理文件中的以下命令,我可以成功编译代码:
gfortran-fopenmp-g-fimplicit none-cpp"%SRC_DIR%\globals\current_precision.f90"%SRC_DIR%\string.string.f90%SRC_DIR%\user\MOONS.f90 parametericStudy.f90-o main.exe
两种情况下,文件名加双引号和不加双引号的完整路径都应该有效。
我的解决方案是对源文件使用相对路径。这样做编译得很好,没有错误。如果这种情况在未来发生变化,那么我会更新这篇文章。
简而言之,只需使用
SET SRC_DIR = code