visual studio Dll and Matlab

隐身守侯 提交于 2019-12-06 07:49:11

Example: Take the following files, and build a DLL in Visual Studio.

helper.h

#ifndef HELPER_H
#define HELPER_H

#ifdef _WIN32
#ifdef EXPORT_FCNS
#define EXPORTED_FUNCTION __declspec(dllexport)
#else
#define EXPORTED_FUNCTION __declspec(dllimport)
#endif
#else
#define EXPORTED_FUNCTION
#endif

#endif

simple.h

#ifndef SIMPLEH_H
#define SIMPLEH_H

#include "helper.h"

#ifdef  __cplusplus
extern "C" {
#endif

EXPORTED_FUNCTION int sq(int x);

#ifdef  __cplusplus
}
#endif

#endif

simple.cpp

#define EXPORT_FCNS
#include "helper.h"
#include "simple.h"

int sq(int x)
{
    return (x*x);
}

Copy the generated simple.dll and the header files simple.h and helper.h in the current directory. Then in MATLAB:

>> loadlibrary('./simple.dll', './simple.h')

>> libisloaded simple
ans =
     1

>> libfunctions simple -full
Functions in library simple:

int32 sq(int32)

>> calllib('simple', 'sq',3)
ans =
     9

Note: If you are running MATLAB 64-bit, you must build the DLL as such. The rule is that you cannot load a 32-bit library in a 64-bit process.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!