Call .dll into MATLAB function/code and simulink

조회 수: 12 (최근 30일)
Robert Evans
Robert Evans 2017년 8월 1일
댓글: Robert Evans 2017년 8월 2일
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
Rik
Rik 2017년 8월 1일
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 2017년 8월 2일
DLLs are most definitely still supported by current versions of MATLAB that is not the issue here.

댓글을 달려면 로그인하십시오.

답변 (1개)

Philip Borghesani
Philip Borghesani 2017년 8월 2일
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
Robert Evans
Robert Evans 2017년 8월 2일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by