【问题标题】:visual studio Dll and Matlab视觉工作室 Dll 和 Matlab
【发布时间】:2013-08-27 02:41:50
【问题描述】:

我正在尝试使用 Visual Studio 构建一个 dll,以便可以在 matlab 中使用它... 我尝试了 thausand 代码但没有解决方案!我正在研究 matlab 2013(32and64) 和 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);
}

visual studio 构建它并制作 th dll 文件,但 matlab 总是看不到函数... 我该怎么办/*我真的被卡住了:(*/ 在此先感谢...

【问题讨论】:

  • 您是尝试使用loadlibrary 的DLL,还是尝试编写MEX-function?无论哪种方式,文档都是一个很好的起点..
  • 感谢您的回复...是的,我正在使用 loadlibrary,它加载它没有问题,但是当我使用 calllib 时它说“找不到方法”所以我很困惑我应该写什么视觉工作室使它我应该使用什么语法......我上面的语法是正确的还是???再次感谢...
  • 您可以发布您在 MATLAB 中尝试加载 DLL 并调用库的代码吗?它应该是直截了当的
  • 哦,您可能想在头文件中执行通常的 dllexport/dllimport。例如,请参阅我的另一个答案中的 helper.h 文件,以及它是如何从库头文件和实现 C 代码中包含的:stackoverflow.com/a/18071239/97160
  • 我只是打开 matlab 并输入 loadlibrary('mylibdll','SimpleH') 然后我写 calllib('mylibdll','sq',3) 它说“找不到方法” ...我会尝试阅读您的代码并回复您...非常感谢您...

标签: c++ visual-studio-2010 matlab dll loadlibrary


【解决方案1】:

示例:获取以下文件,并在 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

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

【讨论】:

  • 有关 dllimport/dllexport 的说明,请参见 hereThis one 描述混合 C 和 C++
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多