我有一个使用结构的C函数。我开发了一个Fortran包装器,它调用C函数。两个库都已成功构建。我已经从PSCAD模型中传递了变量,并运行了该案例。PSCAD模型正在引发错误。
错误消息:
Type Id Component Namespace Description
dll_df FORTRA~1.LIB(dlltestx.obj) : error LNK2019: unresolved external symbol _test_cfun referenced in function _AUX_CFUN
C代码:
// This is an example of an exported variable
typedef struct _goose_c
{
int in1;
float in2;
}goose_c;
__declspec(dllexport) void test_cfunc(goose_c *V, double *out1)
{
//*out1 = (*V).in1 + (*V).in2;
*out1 = V->in1 + V->in2;
}
Fortran包装代码:
SUBROUTINE AUX_CFUN(ip1, ip2, out1)
use, intrinsic :: iso_c_binding
implicit none
integer, intent(in) :: ip1
real, intent(in) :: ip2
real, intent(out) :: out1
type, bind(C) :: goose_t
integer(c_int) :: in1
real(c_double) :: in2
end type goose_t
type(goose_t) :: goose_f
! Fortran 90 interface to a C procedure
INTERFACE
SUBROUTINE TEST_CFUN (goose_f,out1) bind (C)
use iso_c_binding
import :: goose_t
type(goose_t), intent (in) :: goose_f
real(c_double), intent (out) :: out1
END SUBROUTINE TEST_CFUN
END INTERFACE
goose_f%in1 = ip1
goose_f%in2 = ip2
! call of the C procedure
CALL TEST_CFUN(goose_f,out1)
RETURN
END SUBROUTINE AUX_CFUN
接口中的bind (C)
子句应该使用要接口的函数的确切(包括正确的大小写(C名称来完成;所以在你的例子中,它应该读成bind (C, name='test_cfunc')
。与C函数相关联的Fortran名称(在您的示例中为TEST_CFUN
(原则上可能不同,这是否是一个好主意由您决定,而且它在您的Fortran程序中与常见的Fortran规则一起使用,因此它不区分大小写。