Ale*_*ska 3 c fortran uint32 fortran-iso-c-binding
嗨,我使用Fortran 90代码来调用C函数.由于我在操作地址,因此应该在Fortran中正确匹配C函数的参数.我正在使用ifort和icc来编译代码并在64位机器上工作.
一些测试显示,这也将起作用int32_t,虽然为了防止最终的陷阱,我想保留uint32_t
我调用的C函数有以下原型
uint32_t encode_(uint32_t x, uint32_t y)
uint32_t decode_(uint32_t dec)
Run Code Online (Sandbox Code Playgroud)
我不能仅仅通过做类似的事情来调用这些函数
integer :: cod,encode
cod = encode(i,j)
Run Code Online (Sandbox Code Playgroud)
这会产生胡言乱语.因此我使用了一种解决方法:
void code2d_(uint32_t j[] ){
uint32_t i;
i=encode_(j[0],j[1]);
// the underscore is due to the FORTRAN naming convention
printf("Coded %10d \n",i);
}
Run Code Online (Sandbox Code Playgroud)
随后在Fortran
integer :: cod,code2d
cod = code2d(i,j)
Run Code Online (Sandbox Code Playgroud)
显然我对参数类型的不匹配有一些问题.不幸的是我不知道如何解决这个问题.因为在我的解码/编码函数中完成二进制地址算术,所以保留它是非常重要的uint32_t.
当您使用标记时,您似乎知道iso_c_binding.研究Fortran 2003与C的互操作性.阅读标签描述和一些文档,如http://gcc.gnu.org/onlinedocs/gcc-4.9.0/gfortran/Interoperability-with-C.html.现代Fortran中没有地方可用于拖尾下划线和类似的东西.
Fortran没有任何未签名类型,您必须使用签名.只要签名值为正,就可以.如果需要更大的值,请使用更大的整数类型.transfer()如果需要,可以将较低的字节设置为int32.
第三,Fortran默认使用一些通过引用传递的变体,特别是对于bind(c)过程(它可能是对副本或其他变体的引用).您必须使用该value属性按值传递.
uint32_t encode(uint32_t x, uint32_t y)
uint32_t decode(uint32_t dec)
module c_procs
interface
function encode(x, y) bind(C, name="encode")
use iso_c_binding
integer(c_int32_t) :: encode
integer(c_int32_t), value :: x, y
end function
function decode(x, y) bind(C, name="decode")
use iso_c_binding
integer(c_int32_t) :: decode
integer(c_int32_t), value :: dec
end function
end interface
end module
...
use iso_c_binding
use c_procs
integer(c_int32_t) :: cod, i, j
cod = encode(i,j)
Run Code Online (Sandbox Code Playgroud)
最新版本的GCC能够检测到我们在链接时优化期间混合了有符号和无符号类型:
rng.f90:173:0: warning: type of 'sub' does not match original declaration [-Wlto-type-mismatch]
ival = sub(jz, jsr)
^
rng_c.c:2:10: note: return value type mismatch
uint32_t sub(uint32_t a, uint32_t b) {
^
/usr/include/stdint.h:51:23: note: type 'uint32_t' should match type 'int'
typedef unsigned int uint32_t;
^
rng_c.c:2:10: note: 'sub' was previously declared here
uint32_t sub(uint32_t a, uint32_t b) {
Run Code Online (Sandbox Code Playgroud)
如果您知道自己在做什么,可以忽略警告或禁用警告.