fortran invoke c

2023-12-13 06:39:20

a.f90

program main
  use iso_c_binding, only : C_CHAR, C_NULL_CHAR
  implicit none
  interface
    subroutine print_c ( string ) bind ( C, name = "print_C" )
      use iso_c_binding, only : C_CHAR
      character ( kind = C_CHAR ) :: string ( * )
    end subroutine print_c
  end interface

  call print_c ( C_CHAR_"Hello World!" // C_NULL_CHAR )
end program

b.c

# include <stdlib.h>
# include <stdio.h>

void print_C ( char *text )
{
  printf ( "%s\n", text );

  return;
}

Makefile

all:
        gcc -c b.c
        gfortran a.f90 b.o -o a.out

Run
在这里插入图片描述

文章来源:https://blog.csdn.net/qq_38963393/article/details/134878652
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。