How Do I Use clib to Call a C++ Function with a float Argument?
Show older comments
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 Comments
Vivek
on 20 Mar 2020
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
Peter Hristov
on 24 Aug 2021
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++.
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!