将 returns int 的 C 函数写入 Fortran
Write C function that returns int to Fortran
最终,我正在尝试编写一个 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 client/server 部分已经完成,但目前我还在尝试编写一个程序,该程序在 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
C函数
int calc_(int *a, int *b ) {
return *a+*b;
}
我写了一个主程序来验证 int calc_() 在 C 中作为 calc_(1,1) 调用时确实确实 return 2,但是当我 运行 我得到的程序Fortran 的输出。
我正在使用这个生成文件
# 对 C 使用 gcc,对 Fortran 代码使用 gfortran。
CC=海湾合作委员会
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
最终,我正在尝试编写一个 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 client/server 部分已经完成,但目前我还在尝试编写一个程序,该程序在 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
C函数
int calc_(int *a, int *b ) {
return *a+*b;
}
我写了一个主程序来验证 int calc_() 在 C 中作为 calc_(1,1) 调用时确实确实 return 2,但是当我 运行 我得到的程序Fortran 的输出。
我正在使用这个生成文件 # 对 C 使用 gcc,对 Fortran 代码使用 gfortran。 CC=海湾合作委员会 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