0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

colaboratoryでgfortranを使う

Last updated at Posted at 2022-02-15

環境構築がうまくできない,或いは趣味,その他,様々向け。
強いて言うならスマホやタブレットから簡単に同一のファイルを編集できてコンパイル・実行できる。
!を使えばコマンドは一通り(apt-getでさえ!)できそうなのでcppとか任意言語にも応用が利くだろう。
この記事は学科民のために作成されている。

#まずはColabortoryを開く
開いてください。新規ファイルでよいです。

#サンプルコード

sample.ipynb
!ls -la
#出力例
#total 16
#drwxr-xr-x 1 root root 4096 Feb  1 14:32 .
#drwxr-xr-x 1 root root 4096 Feb 15 13:20 ..
#drwxr-xr-x 4 root root 4096 Feb  1 14:31 .config
#drwxr-xr-x 1 root root 4096 Feb  1 14:32 sample_data

!touch sample.f90
f = open("sample.f90","w") #appendしたいなら"a"を指定,wは中身がすでにあれば全消去して上書きする
f.write( \
"""program sample
  implicit none
  write (*,*) "Hello! This is sample f90 code."
end program sample
""")
f.close()

!cat sample.f90
#program sample
#  implicit none
#  write (*,*) "Hello! This is sample f90 code."
#end program sample

ファイルの書き込み部は以下のマジックコマンドを用いたほうが見やすいかもしれない。同じセル内のものが書き込まれるため他のセルと分ける必要あり。

def def writefile(line, cell)
%writefile [-a] filename
Write the contents of the cell to a file.

The file will be overwritten unless the -a (--append) flag is specified.

positional arguments:
filename file to write

optional arguments:
-a, --append Append contents of the cell to an existing file. The file will be created if it does not exist.

sample.ipynb
%%writefile sample.f90 #appendしたいなら%%writefile -a sample.f90
program sample
  implicit none
  write (*,*) "Hello! This is sample f90 code."
end program sample
sample.ipynb
!gfortran sample.f90 -o sample.o
!./sample.o
#Hello! This is sample f90 code.
0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?