如何将 NULL 字符连接到 Fortran 中的字符数组以调用 c 函数?

How do I concatenate a NULL character to a character array in Fortran to call a c function?

我有以下测试功能:

#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

它编译但是:

  1. 我收到以下警告:
      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]
  1. 通过 import fmod; f_mod.test_c_lib.test("Foo") 调用时我得到以下输出:
     AAAAA
     Foo
     AAAAA
    ---
    8v$
    ---

所以 f2py 正在工作,但是当将 text // C_NULL_CHAR 作为参数传递时,它似乎不起作用,因为我从 C 函数输出中得到了垃圾。

你有两个错误:

  • Fortran 假定形状字符数组是 not interoperable,带有 C 字符指针
  • //被对待

要使字符虚拟参数与 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), C_NULL_CHAR, ...].

要在长度为 1 的字符数组后附加 C_NULL_CHAR,您需要使用数组构造函数:

call c_print_string([text,C_NULL_CHAR])

1 参数长度为 2 是关于“字符长度不匹配”警告的原因。

@francescalus 的回答是正确的。但是因为我在看到他的回答之前解决了它,所以我 post 这里的完整功能代码:

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
---