错误:(1)处的字符参数'a'必须是长度1,因为过程是BIND(C) [英] Error: Character argument 'a' at (1) must be length 1 because procedure is BIND(C)

查看:185
本文介绍了错误:(1)处的字符参数'a'必须是长度1,因为过程是BIND(C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 子程序func01(a)bind(C,name =func01)
隐式无
字符(len = 30),dimension(3) (in):: a

print *
print *,char length =,len(a(1)),len(a(2)),len(a(3 ))
print *,raw a(1):[,a(1),]
print *,raw a(2):[,a(2), ]
print *,raw a(3):[,a(3),]
print *,trim:[,trim(a(1)),] [,trim(a(2)),] [,trim(a(3)),]
end

当我尝试编译上述代码时,我收到以下消息:

  gfortran source3.f90 

source3.f90:1.20:

子程序func01(a)bind(C,name =func01)
1
警告:(1)处的变量'a'是BIND(C)过程'func01'的参数bu
t可能不是C可互操作的
source3.f90:1.20:

子程序func01(a)bind(C,name =func01)
1
错误:字符参数'a'在(1)mus因为程序'func01'
是BIND(C)

告诉我如何摆脱消息?

解决方案

Fortran 的一个字符参数bind C)可互操作的过程必须有长度为1。



但是可以使用与C中相同的方法,使用字符数组。 p>

如果您有

  char ch [5] 

使用

 字符(kind = c_char):: ch(5)

如果您有

  char * ch; 
int n;

ch = malloc(n);
fortran_sub(ch,n);

使用

 <$ (c,name =fortran_sub)
integer(c_int),value :: n
字符(kind = c_char):: ch(n)c $ c>子程序fortran_sub(ch,n)

请注意,字符数组为空分隔符。



如果你调用C并在那里传递字符数组,你有责任用 c_char_null 来分隔它。


subroutine func01( a ) bind(C,name="func01")
  implicit none
  character(len=30), dimension(3) , intent(in) :: a

    print *
    print *, "char length = ", len(a(1)), len(a(2)), len(a(3))
    print *, "raw a(1) : [", a(1), "]"
    print *, "raw a(2) : [", a(2), "]"
    print *, "raw a(3) : [", a(3), "]"
    print *, "trim     : [", trim(a(1)), "] [", trim(a(2)), "]  [", trim(a(3)), "]"
end

When I try to compile the above code I get the following messages:

gfortran source3.f90

source3.f90:1.20:

subroutine func01( a ) bind(C,name="func01")
                    1
Warning: Variable 'a' at (1) is a parameter to the BIND(C) procedure 'func01' bu
t may not be C interoperable
source3.f90:1.20:

subroutine func01( a ) bind(C,name="func01")
                    1
Error: Character argument 'a' at (1) must be length 1 because procedure 'func01'
 is BIND(C)

Can you please tell me on how to get rid of the messages?

解决方案

A character argument of a Fortran bind(C) interoperable procedure must have length 1.

But one can use the same approach as in C, use the arrays of characters.

If you have

char ch[5]

use

character(kind=c_char) :: ch(5)

If you have

char* ch;
int n;

ch = malloc(n);
fortran_sub(ch, n);

use

subroutine fortran_sub(ch, n) bind(C, name="fortran_sub")
  integer(c_int), value :: n
  character(kind = c_char) :: ch(n)

Note that the character array will be null delimited.

If you are calling C and passing the character array there, it is your responsibility to delimit it by c_char_null.

这篇关于错误:(1)处的字符参数'a'必须是长度1,因为过程是BIND(C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆