LoginSignup
0
0

More than 3 years have passed since last update.

FortranからPythonを無理やり実行する

Posted at

Fortranなんてやってられるか!!!

大学の講義でFortranやっているんですけど、めちゃめちゃめんどくさい。こんなことやってないで、モダンに書きたい(Swift大好き人間)。この前、弊学ロボット系の教授が「授業でFortranやっているんですけど、使いますか?」っていう質問に「少なくともうちの研究室ではFortranはもう使わないよwww」って答えていたし。じゃ、少なくとも私のこれからの進路では使わないなぁ...

でも、Fortranの講義の課題では.f90ファイルを提出しなければならない。じゃあ、Fortranから好きな言語を呼び出せれば良いのでは????

というわけで、試しにPythonのコードをFortranのコードから呼び出してみる。

コード

これはsystem(シェルをFortranから叩けるやつ)を用いて無理やり同階層にあるPythonを実行しています。

callpython.f90
program callpython
  call system("python called_python.py")
end program callpython
called_python.py
#!/usr/bin/env python
print "message from python code!!"

実行方法は通常の.f90と同じく

$ gfortran callpython.f90
$ ./a.out 

出力結果はこれ

$ message from python code!!

次は...

値の受け渡しができたらいいですね

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0