【发布时间】:2012-12-17 09:38:33
【问题描述】:
我环顾四周,虽然有很多 EXC_BAD_ACCESS 问题,但都没有帮助。
我正在使用 Mountain Lion(我认为是 OSX 10.8?)和 PGI 12。
我似乎无法从 C 中调用 fortran 函数,我写了一个简化的案例,似乎无法传递整数。
我的fortran函数是:
1 integer function smallFortran(a) result(res) bind(c,name='smallFortran_')
2 !integer function smallFortran(a) result(res)
3
4 use ISO_C_BINDING
5 implicit none
6
7 integer(kind=c_int), intent(IN) :: a
8 !integer, intent(IN) :: a
9
10 print *,'A = ', a
11 res = a;
12
13 endfunction smallFortran
而我的 C 函数是,
int main() {
int ier=7;
ier = smallFortran_(8);
}
构建它..
matt@pontus:diffFst$ make
pgcc -c cDoDiffFst.c
PGC-W-0267-#warning -- "Unsupported compiler detected" (/usr/include/sys/cdefs.h: 81)
PGC/x86-64 OSX 12.9-0: compilation completed with warnings
pgcc -g -O0 -traceback -o cDoDiffFst cDoDiffFst.o smallFortran.o -lpgf90 -lpghpf2 -lpgf90rtl -lpgftnrtl -lpghpf_rpm
(我希望警告不是导致我的问题的原因,PGI user forum 对此作出回应,称他们将发送文件的更新版本,但我还没有得到回复。不知道为什么PGI 需要指定很多额外的库)
当我在调试器中运行它时..
matt@pontus:diffFst$ gdb cDoDiffFst
(gdb) run
Starting program: /Users/matt/aurams/trunk/utils/diffFst/cDoDiffFst
Reading symbols for shared libraries +............................. done
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000008
0x0000000100001906 in _smallFortran_ (a=Cannot access memory at address 0x8
) at smallFortran.f90:10
10 print *,'A = ', a
(gdb)
我完全迷路了,为什么我不能发送一个 int?我试过给一个整数赋值并发送它,没有骰子。我已经将其作为子例程进行了尝试,我已经尝试过没有返回值.. 没有任何效果。
【问题讨论】:
-
有很多事情可能出错.... 一个是 C 和 Fortran 通常使用不同的参数传递协议 -- C 是按值传递的,而 Fortran 通常是(取决于编译器和其他问题)参考。
-
@HotLicks - 嗯,没想到那样。也许我可以尝试将 int 作为参考传递? (我的 C 不好,我会尝试作为指针,并通过引用)
标签: c fortran fortran-iso-c-binding