编写将int返回给Fortran的C函数 [英] Write C function that returns int to Fortran

查看:215
本文介绍了编写将int返回给Fortran的C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  Fortran程序传入输入 - >用C语言编写的客户端 - >用C语言编写的服务器 - > Fortran程序计算输入并传递回应

C客户端/服务器部分已完成,但在当我试图编写一个在Fortran程序中接受输入的程序时,我将它传递给计算答案的C程序。但是,我看到som的奇怪行为。



Fortran程序

 程序计算器
!隐式无

!类型声明语句
整数x
x = 1

!可执行语句
x = calc(1,1)
print *,x

结束程序计算器



C函数

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



$ b我写了一个主程序来验证int calc_()确实返回2在C中调用calc_(1,1)时,但是当我运行程序时,我得到Fortran输出。



我正在使用这个makefile
#使用gcc for 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代码是

 程序计算器
!隐式无

!类型声明语句
整数x,calc
x = 1

!可执行语句
x = calc(1,1)
print *,x

结束程序计算器


Ultimately I am trying to write an IPC calculator making use of Fortran to calculate and C to pass the data between the two Fortran programs. When I am done it will hopefully look like:

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

The C client/server part is done, but at the moment I am stuck trying to write a program that takes input in a Fortran program, passes it to a C program that calculates the answer. However, I see som weird behavior.

Fortran program

program calculator
    !implicit none

    ! type declaration statements
    integer x
    x = 1

    ! executable statements
    x = calc(1,1)
    print *, x

end program calculator

C function

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

I have written a main program that verifies that int calc_() does indeed return 2 when called as calc_(1,1) in C, but when I run the program I get the output from Fortran.

I am using this makefile # Use gcc for C and gfortran for Fortran code. 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

I cannot for the world figure out why this is the case, and it's driving me mad.

解决方案

Almost embarrassingly simple. You must declare calc as an integer in Fortran. The working Fortran code is thus

program calculator
    !implicit none

    ! type declaration statements
    integer x, calc
    x = 1

    ! executable statements
    x = calc(1,1)
    print *, x

end program calculator

这篇关于编写将int返回给Fortran的C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆