Loading the 'C' DLL in matlab.

22 views (last 30 days)
sanjay Ramagiri
sanjay Ramagiri on 13 Jul 2020
Answered: Walter Roberson on 13 Jul 2020
Suppose I am haing a DLL Compiled in C Language. How to I supposed to Include those DLL's into standard path, So that I can able to compile a c application file and execute in Matlab.
Assumed Dll consists of :
<mathlib.h>
// MathLibrary.h - Contains declarations of math functions
#pragma once
#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif
extern "C" MATHLIBRARY_API int add(int, int);
extern "C" MATHLIBRARY_API int sub(int, int);
extern "C" MATHLIBRARY_API int mul(int, int);
<Mathlibrary.cpp>
// MathLibrary.cpp : Defines the exported functions for the DLL.
#include "pch.h" // use stdafx.h in Visual Studio 2017 and earlier
#include <utility>
#include <limits.h>
#include "mathlib.h"
int add(int x, int y) {
return x + y;
}
int sub(int x, int y) {
return x - y;
}
int mul(int x, int y) {
return x * y;
}
<Mathclient - Application Program>
// MathClient.cpp : Client app for MathLibrary DLL.
// #include "pch.h" Uncomment for Visual Studio 2017 and earlier
//#include <iostream>
#include <stdio.h>
#include "mathlib.h"
int main()
{
printf("The addition is %d\n", add(4,5));
}

Answers (1)

Walter Roberson
Walter Roberson on 13 Jul 2020
Otherwise addpath() the directory the dll is in, and use loadlibrary() https://www.mathworks.com/help/matlab/ref/loadlibrary.html
If you are compiling an exe then you should use either a prototype file or a thunk file for loadlibrary(), and you would add the dll as a resource to be bundled into the executable.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!