gpt4 book ai didi

从 C 调用模块中的子例程

转载 作者:太空宇宙 更新时间:2023-11-04 01:55:10 25 4
gpt4 key购买 nike

我想知道如何让 C 程序调用包含在 Fortran 90 模块中的 Fortran 90 子例程。

This question deals with a similar problem ,并且我正在尝试实现该解决方案,但我仍然遇到问题。

这里有 testC.c 文件和模块文件 testF.f90 的玩具示例,其中包含主要功能,其中包含 Fortran 90 子例程。

测试C.c

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

extern void __testF_MOD_fortfunc(int *,float *);

int main() {
int ii=5;
float ff=5.5;

__testF_MOD_fortfunc(&ii, &ff);

return 0;
}

测试F.f90

module testF

contains

subroutine fortfunc(ii,ff)
implicit none

integer ii
real*4 ff

write(6,100) ii, ff
100 format('ii=',i2,' ff=',f6.3)

return
end subroutine fortfunc

end module testF

为了编译,我使用以下几行

gcc -c testC.c
gfortran -o testF.f90
gcc -o test testF.o testC.o -lgfortran

我收到错误信息

testC.o: In function `main':
testC.c:(.text+0x27): undefined reference to `__testF_MOD_fortfunc'
collect2: error: ld returned 1 exit status

最佳答案

您可以使用objdump -t testF.o 直接从对象中读取函数名。这揭示了以下行:

0000000000000000 g     F .text  00000000000000b4 __testf_MOD_fortfunc

那是你的函数名。可以看到是小写的testf。在 C 代码中使用它应该可以解决您的问题。

但是,这些命名约定依赖于编译器。您真的应该看看 ISO_C_binding 模块和现代 Fortran 改进的 C 互操作性。

关于从 C 调用模块中的子例程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35466124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com