Clear Filters
Clear Filters

Call .dll into MATLAB function/code and simulink

11 views (last 30 days)
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)_
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
Philip Borghesani on 2 Aug 2017
DLLs are most definitely still supported by current versions of MATLAB that is not the issue here.

Sign in to comment.

Answers (1)

Philip Borghesani
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.
  1 Comment
Robert Evans
Robert Evans on 2 Aug 2017
Is there any way to convert the C++ code to be called into the matlab function? I have a previously constructed simulink and matlab function code that I am not skilled enough to put all into C.

Sign in to comment.


Find more on MATLAB Compiler 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!