
Ubuntu日本語フォーラム

ログインしていません。
ご無沙汰しています、大山です。
現在、gfortran7.4.0 と gcc7.4.0を使って、fortranとC言語を共有するソフトの開発を勉強していますが、行き詰まったままです。OSはUbuntu 18.04.3 LTSです。
gfortranのガイドに従い、subroutine c_f_procpointerを使い、C言語のFunctionの値をfortranで出力しようと試みました。C言語のsourceを以下に示します。
/*** test for compiler : pi ***/
#include <stdio.h>
#include <math.h>
int snvcal(float an)
{
float pi,snv;
pi = 4.0*atan(1.0);
snv = sin(pi*an/180.0);
printf("pi = "); printf("%e\n",pi);
printf("sin(an) = "); printf("%e\n",snv);
return snv ;
}
Fortranのsourceは殆ど、gfortranのガイドに従っています。
program test1
use iso_c_binding
implicit none
abstract interface
function func(a)
import :: c_float
real(c_float), intent(in) :: a
real(c_float) :: func
end function
end interface
interface
function snvcalFunc() bind(c,name="snvcalFunc")
import :: c_funptr
type(c_funptr) :: snvcalFunc
end function
end interface
real :: a
type(c_funptr) :: cfunptr
procedure(func), pointer :: myFunc
cfunptr = snvcalFunc()
call c_f_procpointer(cfunptr, myFunc)
a=45.0
print*, myFunc(a)
end program test1
コンパイラーのコマンドは以下の通りにしました。
gcc -c snvcalFunc.c
gfortran -DLLIMPORT test1.f95 -static-libgfortran snvcalFunc.o
結果のエラーは以下のとおりです。
test1$ gfortran -DLLIMPORT test1.f95 -static-libgfortran snvcalFunc.o
/tmp/ccDfsf9l.o: 関数 `MAIN__' 内:
test1.f95:(.text+0xc): `snvcalFunc' に対する定義されていない参照です
collect2: error: ld returned 1 exit status
source、command、どこかおかしいと思うのですが、対処方法が分かりません。
どなたか、ご指導をいただけないでしょうか。
以上
オフライン