4
1

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 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 から呼びたい場合も同様.

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?