编写将函数返回给Fortran的C函数

最后,我试图编写一个IPC计算器,利用Fortran计算和C来传递两个Fortran程序之间的数据。 当我完成它将希望看起来像:

Fortran program to pass input -> Client written in C -> Server written in C -> Fortran program to calculate input and pass ans back 

C客户端/服务器部分已完成,但目前我仍然试图编写一个在Fortran程序中输入的程序,将其传递给计算答案的C程序。 但是,我看到了一些奇怪的行为。

Fortran计划

 program calculator !implicit none ! type declaration statements integer x x = 1 ! executable statements x = calc(1,1) print *, x end program calculator 

Cfunction

 int calc_(int *a, int *b ) { return *a+*b; } 

我编写了一个主程序,在C中调用calc_(1,1)时validationint calc_()确实返回2,但是当我运行程序时,我从Fortran获得输出。

我正在使用这个makefile#使用gcc代替C和gfortran代替Fortran代码。 CC = gcc FC = gfortran

 calc : calcf.o calcc.o $(FC) -o calc calcf.o calcc.o calcc.o : calcc.c $(CC) -Wall -c calcc.c calcf.o: calcf.f90 $(FC) -c calcf.f90 

我不能让全世界弄清楚为什么会这样,这让我很生气。

几乎令人尴尬的简单。 您必须在Fortran中将calc声明为整数。 因此,工作的Fortran代码

 program calculator !implicit none ! type declaration statements integer x, calc x = 1 ! executable statements x = calc(1,1) print *, x end program calculator