How Do I Use clib to Call a C++ Function with a float Argument?

I am trying to publish a C++ library interface per this documentation: Publish Interface to Shared C++ Library. This process is also described in this blog post by Vivek Bhownani. I'm currently building a simple HelloWorld dll and the process works fine when my function only has integer arguments and return values. As soon as I add a function that takes float arguments, the library interface builds just fine, but calling the function with a float argument returns the following error in Matlab:
User Library threw non-standard exception with message:'Can't convert the Array to this TypedArray'.
This is a simple scalar argument and return value, after reviewing the list of unsupported C++ features I think that this simple library call should work. My library header and source are posted below, can anyone provide some advice? Would love to hear from Vivek!
DLL Header file:
#pragma once
#include "windows.h"
#ifdef TESTDLL_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif
// HelloWorld functions
MATHLIBRARY_API unsigned long square_int(unsigned long a);
MATHLIBRARY_API float square_float(const float a);
DLL Source file:
#include "my_test_dll.h"
unsigned long square_int(unsigned long a)
{
return a * a;
}
float square_float(const float a)
{
return a * a;
}
The script that generates the Matlab interface:
% Full path to files in the library
productPath = 'C:\git_checkouts\temp\test_dll';
% Header file name
hppFile = 'my_test_dll.h';
% Full path to folder containing all header files
hppPath = productPath;
% Full path to folder containing include files
iPath = hppPath;
% Full path to folder containing library files
libPath = fullfile(productPath,'x64','Release');
% Library file name
libFile = 'test_dll.lib';
myPkg = 'myPackage';
clibgen.generateLibraryDefinition(fullfile(hppPath,hppFile),...
'IncludePath', iPath,...
'Libraries', fullfile(libPath,libFile),...
'PackageName', myPkg,...
'Verbose',true)
%% Build the library interface from the just-generated definition file
build(definemyPackage);
%% Add the path to the dll to the system path. This is temporary for this instance of Matlab!
sysPath = getenv('PATH');
setenv('PATH',[sysPath ';' libPath])
getenv('PATH');
%% Finally, add the newly created Matlab interface file to the path
addpath('myPackage');
And finally, calling the int and the float functins from the Matlab command line gives the following:
>> clib.myPackage.square_int(4)
ans =
uint32
16
>> clib.myPackage.square_float(4.5)
Error using clib.myPackage.square_float
User Library threw non-standard exception with message:'Can't convert the Array to this TypedArray'.

2 commentaires

Hi,
I saw your post on my blog, but I thought I'd address this question right here. Thank you for reporting this behavior. I've made a note of it, and we'll be fixing it soon. As for the record floating point types are supported in the MATLAB C++ interface, beginning in 19a.
Vivek
Hi, Vivek!
Is there any progress on this issue? I am also getting this error and suspect it is to do with the 'const' modifier in C++.

Connectez-vous pour commenter.

 Réponse acceptée

Ok, a bit more tweaking and I've got this issue fixed. The issue was the const modifier on the floating point argument to square_float(), apparently Matlab doesn't like this. Could be a very useful thing to sort out in the next release. When I remove the const modifier and rebuild the dll and the Matlab interface, I get the following results at the command line:
>> clib.myPackage.square_int(4)
ans =
uint32
16
>> clib.myPackage.square_float(4.5)
ans =
single
20.2500
I hope this helps in case anyone else runs into this problem.

Plus de réponses (0)

Produits

Version

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by