[英]EXC_BAD_ACCESS when calling Fortran function from C
I've looked around, and though there are a lot of EXC_BAD_ACCESS issues, none of them helped. 我环顾四周,尽管存在很多EXC_BAD_ACCESS问题,但它们都无济于事。
I'm using Mountain Lion (OSX 10.8 I think?), and PGI 12. 我正在使用Mountain Lion(我认为是OSX 10.8?)和PGI 12。
I can't seem to call fortran functions from C, I've written a simplified case and it seems that I can't pass integers. 我似乎无法从C调用fortran函数,我编写了一个简化的案例,而且似乎无法传递整数。
My fortran function is: 我的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
And my C function is, 我的C函数是
int main() {
int ier=7;
ier = smallFortran_(8);
}
Build it.. 建..
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
(I hope that warning isn't what's causing my problems, the PGI user forum responds to this by saying they'll send a newer version of the file, but I haven't been replied to yet. And no idea why PGI requires so many extra libraries to be specified) (我希望警告不是引起我问题的原因, PGI用户论坛对此进行了回应,称他们将发送文件的较新版本,但我尚未得到答复。也不知道为什么PGI要求这样做需要指定许多额外的库)
When I run it in the debugger.. 当我在调试器中运行它时
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)
I'm totally lost, why can't I send an int? 我完全迷路了,为什么我不能发送整数? I've tried assigning a value to an integer and sending it, no dice. 我尝试将一个值分配给一个整数并发送它,没有骰子。 I've tried it as a subroutine, I've tried it without a return value.. nothing works. 我已经将其作为子例程进行了尝试,没有返回值就尝试了。
Here is an alternative solution that shows how to write Fortran to match the original C of the question. 这是一个替代解决方案,显示了如何编写Fortran以匹配问题的原始C。 The key is the value
qualifier on the declaration. 关键是声明上的value
限定符。 With the Fortran ISO C Binding you match various ways C passes arguments. 使用Fortran ISO C绑定,可以匹配C传递参数的各种方式。 You can also do away with underscores in routine names ... that's a purpose of the name
keyword of bind
. 您还可以取消例程名称中的下划线...这是bind
的name
关键字的目的。
The C code without the underscore in the call: 调用中不带下划线的C代码:
int main() {
int ier=7;
ier = smallFortran (8);
}
and the matching Fortran: 以及匹配的Fortran:
function smallFortran(a) result(res) bind(c,name='smallFortran')
use ISO_C_BINDING
implicit none
integer(kind=c_int), intent(IN), value :: a
integer(kind=c_int) :: res
print *,'A = ', a
res = a;
endfunction smallFortran
The error should make clear what's going wrong; 该错误应清楚说明出了什么问题。 smallFortran_
expects its argument to be passed by reference (as are all arguments in fortran -- note that I'm being slightly fast and loose here), and tries to access the data at the pointer 8
, which fails. smallFortran_
希望其参数通过引用传递(与fortran中的所有参数一样—请注意,这里我有点快而松散),并尝试访问指针8
处的数据,这会失败。 Fix is easy; 修复很容易; the function expects a pointer, so give it one: 该函数需要一个指针,因此给它一个:
int main() {
int ier = 7;
int arg = 8;
ier = smallFortran_(&arg);
}
This assumes that the fortran integer
type corresponds to a C int
with the compilers in question; 假设fortran integer
类型与所涉及的编译器的C int
对应; you may need to make arg
a long
otherwise. 否则您可能需要使arg
long
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.