2016-08-01 69 views
1

當Fortran代碼包含打印或寫入函數調用時,我遇到了將Fortran編譯爲Python擴展模塊的問題。F2PY無法通過打印或寫入編譯Fortran

我在Windows 8.1上使用gfortran(通過mingw-w64)和安裝了Python 2.7的MSVC編譯器。使用的Python發行版是Anaconda。

test.f

subroutine test (a) 

    integer, intent(out) :: a 

    print *,"Output from test" 

    a = 10 

end subroutine test 

運行f2py -c -m --fcompiler=gnu95 test test.f90我看到這些錯誤:

test.o : error LNK2019: unresolved external symbol _gfortran_st_write referenced in function test_ 
test.o : error LNK2019: unresolved external symbol _gfortran_transfer_character_write referenced in function test_ 
test.o : error LNK2019: unresolved external symbol _gfortran_st_write_done referenced in function test_ 
.\test.pyd : fatal error LNK1120: 3 unresolved externals 

但是當我註釋掉打印(或寫)語句,它工作正常。

我注意到一件奇怪的事情,它似乎是使用Python for ArcGIS。

compile options: '-Ic:\users\[username]\appdata\local\temp\tmpqiq6ay\src.win-amd64- 
2.7 -IC:\Python27\ArcGISx6410.3\lib\site-packages\numpy\core\include -IC:\Python 
27\ArcGISx6410.3\include -IC:\Python27\ArcGISx6410.3\PC -c' 
gfortran.exe:f90: test.f90 

任何幫助將不勝感激。

+0

您沒有正確鏈接gfortran運行時庫(libgfortran)。你爲什麼使用'--fcompiler = gnu95'? –

回答

0

回答我自己的問題。

步驟來解決:

  1. 忽略或擦洗的MinGW-W64安裝。這是沒有必要的蟒蛇 自帶的MinGW
  2. 添加C_INCLUDE_PATH到Windows系統環境變量和指向的是位於森蚺路徑中的包含文件夾(如C:\userdata\[user]\Miniconda\include
  3. 變化--compiler=msvc--compiler=mingw32
  4. 在我情況下,它試圖從ArcGIS安裝中使用numpy。要解決,我只好conda uninstall numpy,注意到所有它刪除的軟件包,然後conda install [list of packages that were previously removed]

的Fortran代碼編譯現在完全可以和在Python調用。