如何解决如何将 NULL 字符连接到 Fortran 中的字符数组以调用 c 函数?
#include <stdlib.h>
#include <stdio.h>
void print_string(char *text);
void print_string(char *text) {
printf("---\n%s\n---\n",text);
}
以及以下模块,该模块使用 iso c 绑定封装了 Fortran 子例程的调用:
module test_c_lib
use iso_c_binding
implicit none
contains
subroutine test(text)
use iso_c_binding
character,intent(in) :: text(:)
! Interface to C function
interface
subroutine c_print_string(t) bind(C,name="print_string")
import
character(kind=c_char) :: t(:)
end subroutine
end interface
! Call C function
print *,"AAAAA"
print *,text
print *,"AAAAA"
call c_print_string(text // C_NULL_CHAR)
end subroutine
end module
使用 ISO C BINDINGS 在 Fortran 中定义子例程的方法摘自本文档 link。我进一步封装了一下,因为 f2py
不支持 iso c 绑定。
我通过 makefile
编译所有内容:
$ cat makefile
f_mod.so: f_mod.f90 c_lib.o
f2py -c f_mod.f90 c_lib.o -m f_mod
c_lib.o: c_lib.c
gcc -c -fpic c_lib.c -o c_lib.o
它编译但是:
- 我收到以下警告:
150 | call c_print_string(text // C_NULL_CHAR)
| 1
Warning: Character length mismatch (2/1) between actual argument and assumed-shape dummy argument 't' at (1) [-Wargument-mismatch]
AAAAA
Foo
AAAAA
---
8v$
---
所以 f2py
正在工作,但是当将 text // C_NULL_CHAR
作为参数传递时,它似乎不起作用,因为我从 C 函数输出中得到了垃圾。
解决方法
你有两个错误:
- Fortran 假定形状字符数组是 not interoperable,带有 C 字符指针
-
//
被处理 elementally
要使字符伪参数与 char *
C 参数可互操作,t
应为 assumed size array:
character(kind=c_char) :: t(*) ! Assumed size,not assumed shape
您还可以使用 CFI_cdesc_t
C 类型使 t
成为假定的形状数组或假定的长度标量,但这要高级得多。
即使假设 t
的大小,您也没有工作程序,因为下一个问题://
的元素性质。
因为 text
是一个(假定形状)数组,所以连接 text // C_NULL_CHAR
是按元素完成的,给出长度为 2 的数组 1 和 text
的每个元素与 C 空字符连接。然后 C 函数看到输入看起来像 [text(1),C_NULL_CHAR,text(2),...]
。
要附加一个长度为 1 的字符数组并附加 C_NULL_CHAR
,您需要使用数组构造函数:
call c_print_string([text,C_NULL_CHAR])
1 参数长度为 2 是关于“字符长度不匹配”警告的原因。
,@francescalus 的回答是正确的。但是因为我在看到他的回答之前解决了它,所以我在这里发布了完整的功能代码:
module test_c_lib
use iso_c_binding
implicit none
contains
subroutine test(text)
use iso_c_binding
character(*),intent(in) :: text
! Interface to C function
interface
subroutine c_print_string(t) bind(C,name="print_string")
import
character(kind=c_char) :: t(*)
end subroutine
end interface
! Call C function
print *,"Inside Fortran"
print *,text
print *,"Inside Fortran"
call c_print_string(text // C_NULL_CHAR)
end subroutine
end module test_c_lib
program test_prog
use test_c_lib
call test(text = "Hello C World")
end program test_prog
注意对 subroutine test(text)
的接口和 C 函数接口 print_string()
所做的更改。通过以下命令编译代码,
icl print_string.c -c
ifort test_c_lib.f90 -c
ifort *.obj /exe:main.exe
我目前不确定这是英特尔编译器中的错误还是标准符合行为不需要@francecalus 的最后更正来转换
text // C_NULL_CHAR
到 [text,C_NULL_CHAR]
。当然 ifort
不需要它。这是输出:
> main.exe
Inside Fortran
Hello C World
Inside Fortran
---
Hello C World
---
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。