【发布时间】:2014-03-24 21:08:42
【问题描述】:
我需要在 Fortran 中绑定一个 C 函数。 C 函数的原型类似于
/* If argv[i] is NULL, then i is the length of argv[] */
int foo(char* argv[]);
我想在 Fortran 子程序 bar 中调用 foo
! If argv(i) is a blank string, then i is the length of argv.
! Constant MAX_STRING_LEN is the maximal length of any possible element in argv.
subroutine bar(argv)
character(len=*), intent(in) :: argv(*)
character(kind=c_char), dimension(*), allocatable :: argv_c(*) !???
! convert argv to argv_c element by element, something like
! allocate(argv_c(len_trim(argv(i)) + 1) ! Plus 1 to put C_NULL_CHAR
! assign argv(i) to argv_c(i)
ierror = foo(argv_c)
! deallocate each argv_c(i)
end subroutine bar
我想知道如何声明 foo 的原型。我设计了这个,但我不确定它是否正确。这个 argv 可以和 char** 互操作吗?
function foo(argv) bind(c, name="foo")
character(kind=c_char), dimension(*), intent(in) :: argv(*)
end function
【问题讨论】:
-
因此,从您的问题的上下文来看,您似乎在询问如何将 C 样式函数翻译 为 FORTRAN 样式函数。这对吗?
-
你有两次声明
argv的维度。 -
不是将 C 翻译成 Fortran,而是在 Fortran 中调用 C。
-
@JunchaoZhang - 是的,我明白了。我的错误,我已经在下面编辑了我的答案,并包含了一些我希望对您有所帮助的链接。