visual studio Dll and Matlab

visual studio Dll and Matlab

本文关键字:Matlab and Dll studio visual      更新时间:2023-10-16

我正在尝试用visual studio构建一个dll,以便在matlab中使用它。。。我尝试了thausand代码,但没有解决方案!!!我正在开发matlab 2013(32和64)和VS2010!例如,我试着用这种方式编写代码。。。

        //The header
    #ifndef SIMPLEH_H
    #define SIMPLEH_H
    #ifdef  __cplusplus
    extern "C" {
    int sq(int x);  
    #endif
    #ifdef  __cplusplus
    }
    #endif
    #endif
//the Func(example)
#include "SimpleH.h"
int sq(int x)
{
return (x*x);
}

visualstudio构建了它并制作了dll文件,但matlab总是看不到它的功能。。。我该怎么办/*我真的很困惑:*/提前感谢。。。

示例:使用以下文件,在Visual Studio中构建DLL。

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

简单.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);
}

将生成的simple.dll和头文件simple.hhelper.h复制到当前目录中。然后在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

注意:如果你运行的是MATLAB 64位,你必须建立这样的DLL。规则是不能在64位进程中加载32位库。