问题描述
我已经举办了这条大约一个星期,现在,和论坛后,已经搜索论坛,如何从C发送一个char * FORTRAN到一个明确的解释。为了使事情更令人沮丧的,从FORTRAN发送一个char *参数到C呈直线前进......
I've been held up on this for about a week, now, and have searched forum after forum for a clear explanation of how to send a char* from C to FORTRAN. To make the matter more frustrating, sending a char* argument from FORTRAN to C was straight-forward...
发送从FORTRAN到C一个char *参数(这工作正常):
Sending a char* argument from FORTRAN to C (this works fine):
// The C header declaration (using __cdecl in a def file):
extern "C" double GetLoggingValue(char* name);
和来自FORTRAN:
And from FORTRAN:
! The FORTRAN interface:
INTERFACE
REAL(8) FUNCTION GetLoggingValue [C, ALIAS: '_GetLoggingValue'] (name)
USE ISO_C_BINDING
CHARACTER(LEN=1, KIND=C_CHAR), DIMENSION(*), INTENT(IN) :: name
END FUNCTION GetLoggingValue
END INTERFACE
! Calling the function:
GetLoggingValue(user_name)
当尝试使用类似的逻辑从C返回一个char *,我的问题后,得到的问题。我觉得应该工作的一个尝试是:
When trying to use analogous logic to return a char* from C, I get problem after problem. One attempt that I felt should work is:
// The C declaration header (using __cdecl in a def file):
extern "C" const char* GetLastErrorMessage();
和FORTRAN的接口:
And the FORTRAN interface:
INTERFACE
FUNCTION GetLastErrorMessage [C, ALIAS: '_GetLastErrorMessage'] ()
USE ISO_C_BINDING
CHARACTER(LEN=1, KIND=C_CHAR), DIMENSION(255), :: GetLastErrorMessage
END FUNCTION GetLastErrorMessage
END INTERFACE
(我不能随便使用维度(*),所以我已经过大到255)。
(I can't literally use the DIMENSION(*), so I've gone oversize to 255.)
此的应的返回一个指针到255 C风格的字符数组 - 但如果这样做,我已经无法将其转换为有意义的字符串。在实践中,它返回一个随机的字符集,从任何地方宋体的钟字...
This should return a pointer to an array of 255 C-style characters - but if it does, I've been unable to convert this to a meaningful string. In practice, it returns a random set of characters, anywhere from Wingdings to the 'bell' character...
我也试图返回:
- 指向字符(LEN = 255,KIND = C_CHAR)。
- 字面上看CHARACTER(LEN = 255,KIND = C_CHAR)。
- 一个整数(C_SIZE_T),并试图巧妙的成为一个指向字符串数组。
- 一个字符。
- 等
如果任何人都可以给我如何做到这一点的例子,我将非常感激......
If anybody can give me an example of how to do this, I would be very grateful...
最好的问候,
迈克
推荐答案
动态长度的字符串总是有点棘手与C相互作用。一个可能的解决方案是使用指针。
Strings of dynamic length are always a bit tricky with the C interaction. A possible solution is to use pointers.
首先一个简单的例子,在那里你必须交出一个空字符结尾的字符串的C函数。如果你真的把这个字符串只在,你必须确保与c_null_char来完成它,所以这个方向是pretty直线前进。下面是由的例子:
First a simple case, where you have to hand over a null-character terminated string to a C-Function. If you really pass the string only in, you have to ensure to finalize it with the c_null_char, thus this direction is pretty straight forward. Here are examples from a LuaFortran Interface:
subroutine flu_getfield(L, index, k)
type(flu_State) :: L
integer :: index
character(len=*) :: k
integer(kind=c_int) :: c_index
character(len=len_trim(k)+1) :: c_k
c_k = trim(k) // c_null_char
c_index = index
call lua_getfield(L%state, c_index, c_k)
end subroutine flu_getfield
和lua_getfield的:
function lua_tolstring(L, index, len) bind(c, name="lua_tolstring")
use, intrinsic :: iso_c_binding
type(c_ptr), value :: L
integer(kind=c_int), value :: index
integer(kind=c_size_t) :: len
type(c_ptr) :: lua_tolstring
end function lua_tolstring
最后,这里是澄清c_ptr如何能PTED的Fortran字符串间$ P $的尝试:
假设你有指向字符串c_ptr:
Finally, here is an attempt to clarify how a c_ptr can be interpreted as a Fortran character string:Assume you got a c_ptr pointing to the string:
type(c_ptr) :: a_c_string
和它的长度由与下面类型的len变量给出:
And the length of it is given by a len variable with the following type:
integer(kind=c_size_t) :: stringlen
您想获得此字符串的指针,在Fortran语言的字符串:
You want to get this string in a pointer to a character string in Fortran:
character,pointer,dimension(:) :: string
所以,你做的映射:
So you do the mapping:
call c_f_pointer(a_c_string, string, [ stringlen ])
这篇关于创建一个FORTRAN接口的C函数返回一个char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!