Loading the 'C' DLL in matlab.

15 次查看(过去 30 天)
sanjay Ramagiri
sanjay Ramagiri 2020-7-13
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));
}

回答(1 个)

Walter Roberson
Walter Roberson 2020-7-13
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.

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by