Calllib error: Array must be numeric or logical or a pointer to one
显示 更早的评论
I need to use a function available in Fortran by a private library. Then, I created a DLL in Fortran with a function calling the private function and a header file in C++, so I can load my library to Matlab by loadlibrary. I was able to load it, but I get the following error when I try to call my function:
"Error using calllib
Array must be numeric or logical or a pointer to one."
I tried to change the format of the variables in my codes at Fortran and C++, but with no success. I receive the same error. My function receives another function as an input (it is a function_handle in Matlab), e.g.,
func = @(x) x^2 + x - 2
And, I specified it as a float in the header, which I don't know if it is the right way to do it.
My codes are the following:
DLL (Fortran 90, Visual Studio 2019, Intel Parallel XE Studio 2019):
DOUBLE PRECISION function rizzo_function(func, a, b, c, d, e)
!DEC$ ATTRIBUTES DLLEXPORT, ALIAS : "rizzo_function" :: rizzo_function
include 'library.h'
DOUBLE PRECISION a, b, c, d, e
EXTERNAL func, private_function
CALL private_function(func, a, b, c, d, e)
rizzo_function = c !c is one of the outputs (and inputs) of private_function.
END FUNCTION
Header File (C++):
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) double rizzo_function(float *func, double* a, double* b, double* c, double* d, double* e);
#ifdef __cplusplus
}
#endif
I set func as float because func(x), in private_function, is defined as:
real function func(x)
real, intent(in) :: x
end function
I am thinking the error is the wrong specification of func at C++ (or Fortran), but I don't know what should be its format.
Any help and suggestions would be gratefully received.
Thanks in advance,
Rafaela Rizzo.
回答(1 个)
James Tursa
2020-4-13
编辑:James Tursa
2020-4-13
0 个投票
This sounds very similar to the following post, where someone was trying to call a function that had a function pointer as one of its arguments (that "func" in your Fortran code is a function pointer). Read through the solution framework in this link and see if you can modify it for your needs. You will not be able to pass a MATLAB function handle into that Fortran function pointer spot, but maybe you can use an interface function like the one proposed in this post to call that function handle:
类别
在 帮助中心 和 File Exchange 中查找有关 Fortran with MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!