我有一些简单的Fortran .f90代码。我想将其编译为DLL。一切都成功。我将.DLL更改为.PYC。通过python导入pyc,出现错误。该怎么办?(我在互联网上进行了大量搜索,但所有搜索都是关于c或c ++而不是Fortran的。我同时安装了Python 2和3。并安装了Intel Fortran编译器和Visual已安装Studio 2019和MinGW。
math03.f90:
program example
implicit none
integer (kind=8), parameter :: n = 5000
integer (kind=8), dimension(0:n-1) :: primes
call get_primes(n,primes)
end program
subroutine get_primes(n,primes)
implicit none
integer (kind=8) :: n
integer (kind=8), dimension(0:n-1) :: primes
integer (kind=8) :: primes_found, prime_cand, ind
!primes(0) = 2
!primes_found = 1
!prime_cand = 3
do while (primes_found < n)
ind = 0
do while (ind < primes_found)
if (mod(prime_cand,primes(ind)) == 0) then
exit
else
ind = ind + 1
end if
end do
if (ind == primes_found) then
primes(primes_found) = prime_cand
primes_found = primes_found + 1
end if
prime_cand = prime_cand + 1
end do
end subroutine
intel fortran编译器:
更新:
我使用此命令
ifort /dll math03.f90
所以结果:
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 19.0.4.228 Build 20190417
Copyright (C) 1985-2019 Intel Corporation. All rights reserved.
Microsoft (R) Incremental Linker Version 14.21.27702.2
Copyright (C) Microsoft Corporation. All rights reserved.
-out:math03.dll
-dll
-implib:math03.lib
math03.obj
将.dll更改为.pyc
python:
Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import math03
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: Bad magic number in math03.pyc
我用这个的另一种方式:
f2py -c math03.f90 -m primemod
我收到此错误:
running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
build_src
building extension "primemod" sources
f2py options: []
f2py:> c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7\primemodmodule.c
creating c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7
Reading fortran codes...
Reading file 'math03.f90' (format:free)
Post-processing...
Block: primemod
Block: example
Block: get_primes
Post-processing (stage 2)...
Building modules...
Building module "primemod"...
Constructing wrapper function "get_primes"...
get_primes(primes,[n])
Wrote C/API module "primemod" to file "c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7\primemodmodule.c"
adding 'c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7\fortranobject.c' to sources.
adding 'c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7' to include_dirs.
copying c:\python27amd64\lib\site-packages\numpy\f2py\src\fortranobject.c -> c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7
copying c:\python27amd64\lib\site-packages\numpy\f2py\src\fortranobject.h -> c:\users\admin\appdata\local\temp\tmpyf3zf3\src.win-amd64-2.7
build_src: building npy-pkg config files
running build_ext
customize MSVCCompiler
customize MSVCCompiler using build_ext
get_default_fcompiler: matching types: '['gnu', 'intelv', 'absoft', 'compaqv', 'intelev', 'gnu95', 'g95', 'intelvem', 'intelem', 'flang']'
customize GnuFCompiler
Could not locate executable g77
Could not locate executable f77
customize IntelVisualFCompiler
Could not locate executable ifort
Could not locate executable ifl
customize AbsoftFCompiler
Could not locate executable f90
customize CompaqVisualFCompiler
Could not locate executable DF
customize IntelItaniumVisualFCompiler
Could not locate executable efl
customize Gnu95FCompiler
Found executable C:\MinGW\bin\gfortran.exe
Using built-in specs.
COLLECT_GCC=C:\MinGW\bin\gfortran.exe
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --target=mingw32 --with-arch=i586 --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --with-tune=generic --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
customize Gnu95FCompiler
Using built-in specs.
COLLECT_GCC=C:\MinGW\bin\gfortran.exe
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --target=mingw32 --with-arch=i586 --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --with-tune=generic --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
customize Gnu95FCompiler using build_ext
building 'primemod' extension
compiling C sources
error: Unable to find vcvarsall.bat
如前所述得到了这个错误:
error: Unable to find vcvarsall.bat
更新:
我还必须说https://symbols.hotell.kau.se/2016/12/15/python-fortran/ 对我不起作用。
更新#2
我进行了一些更改,将Visual Studio 2019重新安装到2017年,重新安装ifort
的Intel Parallel Studio 2019并将vcvarsall.bat
添加到PATH,卸载MINGW gfortran
编译器,还编辑了我的Fortran文件和CMD命令有点。
python -m numpy.f2py -c fib1.f -m fib1
所以我得到这个错误:
error: extension 'fib1' has Fortran sources but no Fortran compiler found
详细信息:
C:\Users\admin\Documents\GitHub\scikits.scattpy\src\math03\FIB>python -m numpy.f2py -c fib1.f -m fib1
running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
build_src
building extension "fib1" sources
f2py options: []
f2py:> c:\users\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7\fib1module.c
creating c:\users\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7
Reading fortran codes...
Reading file 'fib1.f' (format:fix,strict)
Post-processing...
Block: fib1
Block: fib
Post-processing (stage 2)...
Building modules...
Building module "fib1"...
Constructing wrapper function "fib"...
fib(a,[n])
Wrote C/API module "fib1" to file "c:\users\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7\fib1module.c"
adding 'c:\users\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7\fortranobject.c' to sources.
adding 'c:\users\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7' to include_dirs.
copying C:\Python27\lib\site-packages\numpy\f2py\src\fortranobject.c -> c:\users\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7
copying C:\Python27\lib\site-packages\numpy\f2py\src\fortranobject.h -> c:\users\admin\appdata\local\temp\tmp5ah42b\src.win-amd64-2.7
build_src: building npy-pkg config files
running build_ext
customize MSVCCompiler
customize MSVCCompiler using build_ext
get_default_fcompiler: matching types: '['gnu', 'intelv', 'absoft', 'compaqv', 'intelev', 'gnu95', 'g95', 'intelvem', 'intelem', 'flang']'
customize GnuFCompiler
Could not locate executable g77
Could not locate executable f77
customize IntelVisualFCompiler
Could not locate executable ifort
Could not locate executable ifl
customize AbsoftFCompiler
Could not locate executable f90
customize CompaqVisualFCompiler
Could not locate executable DF
customize IntelItaniumVisualFCompiler
Could not locate executable efl
customize Gnu95FCompiler
Could not locate executable gfortran
Could not locate executable f95
customize G95FCompiler
Could not locate executable g95
customize IntelEM64VisualFCompiler
customize IntelEM64TFCompiler
Could not locate executable efort
Could not locate executable efc
customize PGroupFlangCompiler
don't know how to compile Fortran code on platform 'nt'
warning: build_ext: f77_compiler=None is not available.
building 'fib1' extension
error: extension 'fib1' has Fortran sources but no Fortran compiler found