安靖 发表于 2017-8-31 15:13:18

Fortran与C混编的接口问题

本帖最后由 安靖 于 2017-9-1 10:10 编辑

Interface
         Subroutine ALLC (pmem,size,message)
!DEC$ Attributes C, Alias : '_allc' :: ALLC
         integer pmem
         Integer size
         character(*) message
         end subroutine ALLCend interface


/* Function allc */
/* This function allcs memory for 'mem' of size 'size' */
/* In case of failure it outputs a message 'message' */

void allc (int **mem , int *size, char message)

{
if (*size < 0)
{
printf (" allc - STORAGE ALLOCATION FAILED: %s \n", message);
printf (" Number of words requested: %d\n", *size);
printf (" Length is <= 0\n");
exit (1);
}
*mem = (int *) calloc (((*size)+1) * sizeof (int),1);
if (*mem == 0)
{
printf (" allc - STORAGE ALLOCATION FAILED: %s \n", message);
printf (" Number of words requested: %d\n", *size);
exit (1);
}
}

以上是一段Fortran与C混编,Fortran调用C来分配内存的接口,以及C的实现

问题1:
integer pmem 中 什么意思? 没见过这种用法

问题2:
按照这种接口,32位的情况下没有问题,但是64位的时候会出现链接错误。
error LNK2001: 无法解析的外部符号 _allc




fcode 发表于 2017-8-31 20:19:36

我也不知道什么意思,这不是标准语法。

我猜测,应该是声明该参数为“引用传递”。

这种非标准的写法,没什么意思。建议你改用 ISO_C_Binding 方式混编。此外,fortran也不需要 C 来分配。我估计你这个代码是 F77 时代的产物。

pasuka 发表于 2017-9-1 09:29:47

本帖最后由 pasuka 于 2017-9-1 09:30 编辑

网上找了找,原来是VC6时代fortran与C混合编程的遗迹
C/C++ and Fortran Defaults for Passing Parameters

Fortran Calls to C

安靖 发表于 2017-9-1 10:19:41

本帖最后由 安靖 于 2017-9-1 10:21 编辑

pasuka 发表于 2017-9-1 09:29
网上找了找,原来是VC6时代fortran与C混合编程的遗迹
C/C++ and Fortran Defaults for Passing Parameters
...
感谢解答了我的疑惑,也验证了2楼的猜测。 谢谢2位。

另外,引起第二个问题的原因是 32位和64位下编译出来的函数修饰名(Decoration name)是不同的。
原来一直不知道还有这区别呢

kyra 发表于 2017-9-1 17:53:28

乍一看,还以为是 Coarray {:4_91:}

真的假的 发表于 2018-1-16 20:41:30

Fortran与C混编的接口问题. 你好,为什么我用Fortran调用c的时候32位也会出现问题。说找不到那个子程序,我是按照彭国伦老师那里面的算例做的。我的qq是283158126.可以方便交流一下吗?谢谢
页: [1]
查看完整版本: Fortran与C混编的接口问题