LoginSignup
3
1

More than 5 years have passed since last update.

Fortran からシステムコールを呼ぶ

Posted at

概要

Fortran で TCP のソケット通信がしたい!
と思ったので,とりあえずシステムコールを呼べるかを試してみた.
忘備録程度に.

呼び方

program main
    implicit none

    interface
        function socket(domain, type_, protocol) bind (c)
            integer, value :: domain, type_, protocol
            integer :: socket
        end function
    end interface

    integer :: sock

    sock = socket(2, 2, 0)
    write (*,*) sock
end program

bind (c) をつけないと, socket_とlinkしようとして死ぬ.
, value をつけないと,ポインタを渡してしまうので意図した引数を渡せない.

結果

$ gfortran hoge.f90
$ strace ./a.out
(中略)
socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 3
write(1, "           3\n", 13           3)          = 13
exit_group(0)                           = ?
+++ exited with 0 +++

呼べてる.
システムコール以外にも, C で書いた関数を Fortran から呼びたい場合も同様.

3
1
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
3
1