转载:http://blog.csdn.net/wak0408/article/details/38272163
这个文章的流程是,在vs里面生成matlab可以读懂的文件(mexw**),当然也可以在matlab里面编译 .c/.cpp,直接生成(mexw**),matlab的.m文件就可以直接调用了。
vs&matlab混合编程,例如文章最后的两个参考文献,但是参考文献都是旧版本的matlab和vs,有些新的特新没有体现出来,所以我还是总结一下搭建方法。
首先说明一下mexw64,最简单的理解,mexw64就是dll。matlab较旧的版本可以直接调用dll,到了新版本好像不行了,经过各种百度,matlab2011需要调用mexwXX,这里的XX可以是32和64,分别代表32位和64位。matlab可以将这个dll当作一个函数调用,如图:
这个是matlab调用的效果图,接下来一步一步的说一下如何搭建。
在搭建之前,先详细说明一下我的环境。
matlab版本:
安装目录:D:\Program Files\MATLAB
VS版本:
VS安装目录:C:\Program Files (x86)\Microsoft Visual Studio 12.0
1)创建VS工程
2)配置工程,选择应用程序类型为DLL,附加选项中先选择导出符号后选择空项目。
3)为工程添加test.h test.cpp test.def三个文件。
test.h
- #include "matrix.h"
- #include "mex.h"
- #define TESTMATLAB_EXPORTS
- #ifdef TESTMATLAB_EXPORTS
- #define MEX_FUNCTION_API __declspec(dllexport)
- #else
- #define MEX_FUNCTION_API __declspec(dllimport)
- #endif
- MEX_FUNCTION_API void mexFunction(int nlhs, mxArray* plhs[], int nrhs, mxArray* prhs[]);
#include "matrix.h"
#include "mex.h"
#define TESTMATLAB_EXPORTS
#ifdef TESTMATLAB_EXPORTS
#define MEX_FUNCTION_API __declspec(dllexport)
#else
#define MEX_FUNCTION_API __declspec(dllimport)
#endif
MEX_FUNCTION_API void mexFunction(int nlhs, mxArray* plhs[], int nrhs, mxArray* prhs[]);
test.cpp
- #include "test.h"
- #include <string>
- #include "stdlib.h"
- #include <iostream>
- using namespace std;
- void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[])
- {
- double *Encoder_Decoder_db = NULL;
- string Path_Str = ""; // the path of the bands
- unsigned int bufferlength = mxGetM(prhs[1])*mxGetN(prhs[1]) + 1;
- char *Path_Str_ch = new char[bufferlength];
- short Encoder_Decoder; // 0: encoder, 1: decoder
- Encoder_Decoder_db = mxGetPr(prhs[0]);
- mxGetString(prhs[1], Path_Str_ch, bufferlength);
- Encoder_Decoder = (short)*Encoder_Decoder_db;
- Path_Str = Path_Str + Path_Str_ch;
- mexPrintf("\nBegin of Test pagekpang 2014-7-29 \n");
- mexPrintf("%d, %s\n", Encoder_Decoder, Path_Str_ch);
- mexPrintf("End of Test pagekpang 2014-7-29\n");
- }
#include "test.h"
#include <string>
#include "stdlib.h"
#include <iostream>
using namespace std;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[])
{
double *Encoder_Decoder_db = NULL;
string Path_Str = ""; // the path of the bands
unsigned int bufferlength = mxGetM(prhs[1])*mxGetN(prhs[1]) + 1;
char *Path_Str_ch = new char[bufferlength];
short Encoder_Decoder; // 0: encoder, 1: decoder
Encoder_Decoder_db = mxGetPr(prhs[0]);
mxGetString(prhs[1], Path_Str_ch, bufferlength);
Encoder_Decoder = (short)*Encoder_Decoder_db;
Path_Str = Path_Str + Path_Str_ch;
mexPrintf("\nBegin of Test pagekpang 2014-7-29 \n");
mexPrintf("%d, %s\n", Encoder_Decoder, Path_Str_ch);
mexPrintf("End of Test pagekpang 2014-7-29\n");
}
LIBRARY "testp"
EXPORTS
mexFunction
这时候会发现,整个项目红了一大票,到处报错。主要是因为没有添加include和lib,接下来就需要对工程进行配置。
4)在自己的项目中右键-》属性。找到 配置属性 底下的 VC++ 目录
分别设置包含目录和库目录,将matlab的函数导入到VS。
(1)包含目录
(2)库目录
5)设置运行库
6)添加lib
7)模块定义文件
8)添加x64平台
(1)选择配置管理器
(2)选择新建
(3)输入参数如下
9)编译
10)拷贝工程目录下的x64\Debug\TestMatlab.dll到Matlab的工作目录中,并且更名为TestMatlab.mexw64,就可以运行了。
文中大部分工作我也是参考了《Visual Studio创建Matlab mex(dll)函数》,只是有些地方配置不太一样,改变了一下参数。下一步要完善的是hackrf的操作函数,将libhackrf植入到Matlab中。
参考文献
http://blog.sina.com.cn/s/blog_6ec1700e0100pgtd.html
http://www.360doc.com/content/14/0411/17/1944636_368074271.shtml