当前位置:首页 » 编程软件 » mex编译

mex编译

发布时间: 2022-06-01 08:31:44

‘壹’ 用mex编译.c文件怎么指定.c文件要包含的头文件

mex Compile mex-function

Usage:
mex [options ...] file [files ...]

Description:
mex compiles and links source files into a shared library called a
mex-file, executable from within MATLAB. It also builds executable
files for standalone MATLAB engine and MAT-file applications.

Command Line Options Available on All Platforms:
-c
Compile only. Creates an object file but not a mex-file.
-client engine
Build standalone MATLAB engine or MAT-file application.
-compatibleArrayDims
Build a mex-file using the MATLAB Version 7.2 array-handling
API, which limits arrays to 2^31-1 elements. This option is the
default, but in the future the -largeArrayDims option will be
the default.
-D<name>
Define a symbol name to the C preprocessor. Equivalent to a
"#define <name>" directive in the source. Do not add a space
after this switch.
-D<name>=<value>
Define a symbol name and value to the C preprocessor. Equivalent
to a "#define <name> <value>" directive in the source. Do not
add a space after this switch.
-f <optionsfile>
For advanced users. Specify location and name of the mex
configuration file to use. Overrides mex's default compiler
selection mechanism.
-g
Create a mex-file containing additional symbolic information for
use in debugging. This option disables mex's default behavior of
optimizing built object code (see the -O option).
-h[elp]
Display this message.
-I<pathname>
Add <pathname> to the list of directories to search for #include
files. Do not add a space after this switch.
-l<name>
Link with object library. On Windows, <name> expands to
"<name>.lib" or "lib<name>.lib". On Linux, to "lib<name>.so".
On Mac, to "lib<name>.dylib". Do not add a space after this
switch.
-L<folder>
Add <folder> to the list of folders to search for
libraries specified with the -l option. Do not add a space
after this switch.
-largeArrayDims
Build a mex-file using the MATLAB large-array-handling API. This
API can handle arrays with more than 2^31-1 elements when
compiled on 64-bit platforms. -compatibleArrayDims is the
default option.
-n
No execute mode. Display commands that mex would otherwise
have executed, but do not actually execute any of them.
-O
Optimize the object code. Optimization is enabled by default and
by including this option on the command line. If the -g option
appears without the -O option, optimization is disabled.
-outdir <dirname>
Place all output files in folder <dirname>.
-output <resultname>
Create mex-file named <resultname>. The appropriate mex-file
extension is automatically appended. Overrides mex's default
mex-file naming mechanism.
-setup <lang>
Change the default compiler to build <lang> language mex-files.
When this option is specified, no other command line
input is accepted.
-silent
Suppress informational messages. The mex function still reports
errors and warnings, even when you specify -silent.
-U<name>
Remove any initial definition of the C preprocessor symbol
<name>. (Inverse of the -D option.) Do not add a space after
this switch.
-v
Verbose mode. Display the values for important internal
variables after all command line arguments are considered.
Displays each compile step and final link step fully evaluated.
<name>=<value>
Override default setting for variable <name>. This option is
processed after all command line arguments are considered.

Command Line Options Available Only on Windows Platforms:
@<rspfile>
Include contents of the text file <rspfile> as command line
arguments to mex.

‘贰’ mex文件的MEX的编写

mex的编译结果实际上就是一个带输出函数mexFunction 的dll文件,所以写MEX程序其实就是写一个DLL程序。编写MEX程序的编辑器可以使用MATLAB的代码编辑器,也可使用自己的C++编辑器,如VS2008等。 #includemex.hvoidmexFunction(intnlhs,mxArray*plhs[],intnrhs,constmxArray*prhs[]){}四个参数分别用来输出和输入数据: nlhs 是输出参数个数,plhs 是输出参数指针;nrhs 是输入参数个数,prhs 是输入参数指针。
注意: 对输出和输入参数的操作都是通过指针的方式进行的。 对输入数据进行操作,需要通过MEX函数mxGetPr 得到数据的指针地址。 mxGetM 和 mxGetN 得到矩阵数据的行和列 (返回整数)。对于实矩阵,我们可以定义 double *M; 来对实矩阵数据操作。如: double*M;intm,n;//指针指向第一个参数的数据地址M=mxGetPr(prhs[0]);m=mxGetM(prhs[0]);n=mxGetN(prhs[0]);MATLAB矩阵数据的存储顺序是从上到下,从左到右的,这点和Fortran是一样的。也就是说对于MATLAB的m x n的矩阵A。 A(1,1) 就是 *M,A(2,1) 就是 *(M+1) ,以此类推,A(i,j) 就是 *(M + n*(j-1) + (i-1)).
注意: MATLAB的指标从1开始,C的指标从0开始。 创建文件 timestwoalt.c,其内容如下: #includemex.hvoidtimestwo_alt(double*y,doublex){*y=2.0*x;}voidmexFunction(intnlhs,mxArray*plhs[],intnrhs,constmxArray*prhs[]){double*M;intm,n;//指针指向第一个参数的数据地址M=mxGetPr(prhs[0]);m=mxGetM(prhs[0]);n=mxGetN(prhs[0]);plhs[0]=mxCreateDoubleMatrix(m,n,mxINT32_CLASS,mxREAL);//生成mxn的实矩阵,分配内存空间double*A;A=mxGetPr(plhs[0]);timestwo_alt(A,*M);//调用并直接赋值到指针指向的输出变量}

‘叁’ 什么是Mex文件

MEX文件是一种可在matlab环境中调用的C语言(或fortran)衍生程序,mex的编译结果实际上就是一个带输出函数mexFunction 的dll文件。

‘肆’ matlab 中mex -setup 编译器怎么弄

这个需要你的电脑上安装有C++6.0才能出来,mex -setup列出的编译器都是你电脑上的,你图上的[1] Lcc-win32是matlab自带的编译器。
由于你电脑上没安装其他的编译器,当然就检测不到C++6.0

‘伍’ matlab如何编译mex文件

通过MEX文件可以在MATLAB中像调用内嵌函数一样调用现有的使用C语言和Fortran等语言编写的函数,实现了代码重用,同时也能解决MATLAB循环效率低的缺点,提高MATLAB环境中数据处理的效率。
MEX文件的后缀名为 .mexw32
MEX文件的编写和编译需要两个基本条件:一是必须按照MATLAB应用程序接口组件和相关工具,二是要有C语言或Fortran语言的编译器。
需要对MATLAB系统进行设置,使MATLAB系统知道使用系统的哪一个C语言编译器,以及其参数和路径。
MEX文件系统设置:
>> mex –setup按照提示进行,最后出现Done…系统配置完毕。
C语言MEX文件的建立
C语言MEX文件的建立
1. MEX文件的结构
a) 计算子程序
b) 入口子程序,void mexFunction(int nlhs, mxArray *plhs[],int nrhs,const mxArray *prhs[]){ /*用户特定的代码….*/ }
2. 创建timestwoalt.c
#include "mex.h"
void timestwo_alt(double *y, double x)
{
*y = 2.0*x;
}
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
double *y;
doublex;
/* 检查参数 */
if (nrhs != 1) {
mexErrMsgTxt("One input argument required.");
} else if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments.");
} else if (!mxIsNumeric(prhs[0])) {
mexErrMsgTxt("Argument must be numeric.");
} else if (mxGetNumberOfElements(prhs[0]) != 1 || mxIsComplex(prhs[0])) {
mexErrMsgTxt("Argument must be non-complex scalar.");
}
/* 为输出参数创建变量 */
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
/*
为参数 x、y赋值,x为值,而y为指针
(由于MATLAB没有值传递,所以用指针才能得到修改后的y值,
不然修改的是y的一个副本,为临时变量,在函数返回时,y值没有变化,
不能得到希望的结果)
*/
x = mxGetScalar(prhs[0]);
y = mxGetPr(plhs[0]);
/* 调用timestwo_alt 子函数 */
timestwo_alt(y,x);
}
3. 编译链接C语言的MEX文件源程序,在MATLAB的控制窗口中输入:mex timestwoalt.c生成一个名为timestwoalt.mexw32的MEX文件
4. 运行:在MATLAB的控制窗口中输入
x=2;
y=timestwoalt(x)
输出:y=4
MEX文件实现了一种C语言与MATLAB的接口,其实际的计算功能仍在C语言形式的计算子程序中完成,而入口子程序的功能是检查参数以匹配C语言的参数规范(how to?)。
当有C语言编写的大型程序时,不必用MATLAB语言重新编写,只要将此C语言程序作为一个计算子程序,然后编写一个入口子程序,完成参数的匹配,然后编译成MEX文件即可。
MEX文件的另外一个功能是可以将MATLAB编程中的瓶颈问题,如多重循环等,将此类费时的指令用C语言实现,然后作必要的入口子程序,编译成MEX文件,可以有效地提高MATLAB的效率。
S-函数创建器限制了C语言S-函数的功能:只能有一个输入信号和一个输出信号,而且只能处理double类型的数据!所以,可用性不大。

‘陆’ 请教关于利用Matlab中“mex”命令编译C程序的问题

1.准备好C语言程序,清楚C语言的入口函数
2.编写mexfunction函数。mexfunction函数为C语言与MATLAB语言的接口函数。调用实例在mylinedetect.c文件中.在MATLAB中调用mex指令编译相关文件,将C语言编译为MEX文件。
3.编译完成后,生成mylinedetect.mexw32或mylinedetect.mexw64文件,此文件即mex文件,用于MATLAB与C语言接口函数.
4.编译完成之后,编写MATLAB函数,调用MEX文件。以MEX文件的形式调用编译完成的C语言函数[o1,o2]=mylinedetect(double(X).');......
5.输出结果,上述linedetect函数完成图像中直线检测功能,带入MATLAB中调用后形成结果。

‘柒’ MATLAB mex 找不到编译器怎么办

①选y后,MATLAB会列出当前机器上已经安装的、且与当前MATLAB版本兼容的所有C编译器,一般而言,MATLAB都会自带一个LCC编译器,然而LCC目前仅支持32位的MATLAB,所以你的机器上没有。

②选n后,MATLAB会列出所有与当前MATLAB版本兼容的C编译器类型(不管你是否已经安装,用于帮助用户选择合适的C编译器),你机器上已经安装的2个编译器,VC6.0:MATLAB从R2010b之后不再支持,所以选项里看不到;而SDK7.1选n时可以看到([11]),但选y时没有跳出,是因为MATLAB没有正确定位SDK。

③解决方法:

方法一:如果以缺省选项安装了SDK7.1(不改变默认安装路径),可以尝试输入n后,选择 [14] (注意:是14,不是11!),强制定位该编译器(网上有人用这种方法成功了:http://mlinking.blog.163.com/blog/static/185801922201331464626365/)

方法二:重新安装MATLAB,安装时选择32位进行安装,则自带LCC编译器。

方法三:选择适配的VS版本,下载安装后,重新配置MEX命令。

‘捌’ mex是什么意思

MEX从字面上是MATLAB和Executable 两个单词的缩写

MEX文件是一种可在matlab环境中调用的C语言(或fortran)衍生程序,mex的编译结果实际上就是一个带输出函数mexFunction 的dll文件。

MEX文件实现了一种其它语言与MATLAB的接口,通过MEX文件可以在MATLAB中像调用内嵌函数一样调用使用C语言和Fortran等语言编写的函数,实现了代码重用,同时也能提高MATLAB环境中数据处理的效率。

MEX的编写

mex的编译结果实际上就是一个带输出函数mexFunction 的dll文件,所以写MEX程序其实就是写一个DLL程序。编写MEX程序的编辑器可以使用MATLAB的代码编辑器,也可使用自己的C++编辑器,如VS2008等。

‘玖’ mex文件的MEX的编译

如编译链接C语言的MEX文件源程序,在MATLAB的控制窗口中输入:mex timestwoalt.c生成一个名为timestwoalt.mexw32的MEX文件

‘拾’ mex编译C++,能否使用cout等函数

不能。
mexPrinf是专门用于打印调试信息的接口。调试信息就用这个函数输出。
如果函数本身有I/O需求,比较好的方法是只用C写I/O无关的计算部分,I/O部分还是用matlab写。

热点内容
安卓网卡免驱动如何实现 发布:2024-05-18 15:25:15 浏览:859
8加6算法 发布:2024-05-18 15:04:25 浏览:737
名图16款尊享什么配置 发布:2024-05-18 14:55:37 浏览:584
我的世界怎样刷出32k服务器 发布:2024-05-18 14:32:32 浏览:565
c语言程序设计江宝钏 发布:2024-05-18 14:32:22 浏览:780
右击文件夹总是转圈圈 发布:2024-05-18 14:31:10 浏览:696
新建数据库phpmyadmin 发布:2024-05-18 14:22:38 浏览:736
安卓手机设备连接在哪里 发布:2024-05-18 14:08:28 浏览:820
路由器的密码最多是多少位 发布:2024-05-18 13:58:18 浏览:420
扫描服务器名称如何填 发布:2024-05-18 13:36:29 浏览:115