2012-02-15 14 views
4

私はpythonでそれを使用するためにファイルを作りたいと思います。 fortranソースから共有ライブラリを作成するにはどうしたらいいですか?gfortranを使って共有ライブラリを作成できますか?

以下のコードをテストしました。

gfortran -c mod.f90 
#gfortran -c sub1.f90 
gfortran -c func.f90 
gfortran -shared -fPIC -o func.so func.f90 mod.o 

でも、Pythonでインポートできませんでした。 Fortranソースコードでモジュールファイルを使用しました。私はPythonからfortranソースコードをインポートしました。私は正しいかどうか分からない。

[===>14:47:59]f1:python 
Python 2.7.2+ (default, Oct 4 2011, 20:03:08) 
[GCC 4.6.1] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import func 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ImportError: dynamic module does not define init function (initfunc) 




func.f90 
----------------------------------------- 
program func 

use mod_test 

switch1 = .true. 
switch2 = .false. 

x = 1.2 
!call test(x, z) 
print *, b, str, z, switch1, switch2 

!print *, 'hello' 
end program func 
----------------------------------------- 
mod.f90 
----------------------------------------- 
module mod_test 
!implicit none 
integer a 
real x, y, z 
real*8 :: b = 3.4 
logical*2 switch1, switch2 
character*5, parameter :: str = 'good' 
end module mod_test 
----------------------------------------- 
sub1.f90 
----------------------------------------- 
subroutine test(input, output) 
real, intent(in) :: input 
real, intent(out) :: output 

output = (input + input) 
end subroutine 
----------------------------------------- 
+0

私もテストしました:fwrapc func.f90 --build --name = func --fcompiler = gnu95 --f90exec =/usr/bin/gfortran-4.6 -L/usr/lib/gcc/i686-linux-gnu /4.6 -lgfortranモジュールを見つけることができません。 – wonjun

答えて

9

FortranとPythonの間には「接着剤」が必要です。チェックアウト:F2PY - Fortran to Python interface generator

EDIT例:

f2py -c -m func func.f90 mod.f90 sub1.f90 
python 
>>> import func 
>>> dir(func) 
['__doc__', '__file__', '__name__', '__version__', 'mod_test', 'test'] 

EDIT 2.あなたは、Pythonからfunc.f90でコードを実行したい場合は、私はあなたがサブルーチンにプログラムからそれを変更しなければならないと思います。

+0

f2py -c mod.f90; f2py -c -m func func.f90;私はこれが好きでしたが、モジュールが見つかりませんでした。どうすればいいですか? – wonjun

+0

"モジュールが見つかりませんでした" - f2pyからのエラーメッセージですか? –

+0

f2py -c -m func func.f90 mod.f90 sub1.f90は、実行可能ファイルを作成することです。私はPythonからインポートするために共有ライブラリを作りたいと思っています。エラーメッセージはPythonから来ました:ImportError:./func.so:undefined symbol:__mod_test_MOD_b – wonjun

関連する問題