Call .dll into MATLAB function/code and simulink
9 views (last 30 days)
Show older comments
I am trying to access dynamic libraries inside of matlab that were written through Microsoft Visual Studio and cannot understand how to use the load library functions in order to do so. I have tried making a basic code in Microsoft and call it in MATLAB, but it inst working and is giving me errors all over. My Microsoft visual code looks like this:
// MathLibrary.h - Contains declaration of Function class #pragma once
#ifdef MATHLIBRARY_EXPORTS #define MATHLIBRARY_API __declspec(dllexport) #else #define MATHLIBRARY_API __declspec(dllimport) #endif
namespace MathLibrary { // This class is exported from the MathLibrary.dll class Functions { public: // Returns a + b static MATHLIBRARY_API double Add(double a, double b);
// Returns a * b static MATHLIBRARY_API double Multiply(double a, double b);
// Returns a + (a * b) static MATHLIBRARY_API double AddMultiply(double a, double b); }; }
Whenever I try to call this into matlab I end up receiving these errors:
_Warning: Message from C preprocessor: C:\Users\revans\Documents\Visual Studio 2017\Projects\MathLibraryAndClient\MathLibrary\MathLibrary.h:2:9: warning: #pragma once in main file #pragma once ^
> In loadlibrary In test1 (line 4) Error using loadlibrary Building libmx_thunk_pcwin64 failed. Compiler output is: C:\ProgramData\MATLAB\SupportPackages\R2017a\3P.instrset\mingw_492.instrset\bin\gcc -I"C:\Program Files\MATLAB\R2017a\extern\include" -fexceptions -fno-omit-frame-pointer -I"C:\Users\revans\Documents\Visual Studio 2017\Projects" -I"C:\Users\revans\Documents\Visual Studio 2017\Projects\MathLibraryAndClient\MathLibrary" "libmx_thunk_pcwin64.c" -o "libmx_thunk_pcwin64.dll" -shared In file included from libmx_thunk_pcwin64.c:27:0: C:\Users\revans\Documents\Visual Studio 2017\Projects\MathLibraryAndClient\MathLibrary/MathLibrary.h:10:1: error: unknown type name 'namespace' namespace MathLibrary ^ C:\Users\revans\Documents\Visual Studio 2017\Projects\MathLibraryAndClient\MathLibrary/MathLibrary.h:11:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token { ^
Error in test1 (line 4) loadlibrary('libmx',hfile)_
2 Comments
Rik
on 1 Aug 2017
As far as I understand, there is no longer a support for dll files in Matlab, however, you can compile your c/c++/fortran into a mex file for use in Matlab.
Philip Borghesani
on 2 Aug 2017
DLLs are most definitely still supported by current versions of MATLAB that is not the issue here.
Answers (1)
Philip Borghesani
on 2 Aug 2017
Your header file contains c++ code (namespaces for one). Loadlibrary has never supported c++ code in headers. You are free to use c++ in the implementation however the header must compile when included into a .c main program.
The best way to enforce this is to write a test program for your library in C. This also makes debugging the library much simpler and quicker then debugging the MATLAB process.
See Also
Categories
Find more on Software Development Tools 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!