Dec 24, 2008

Matlab 中编译,调试,调用 C 程序

1. 编译 c/c++ 程序
查找 Matlab 帮助 index:
  • Custom Building MEX-Files

Tutorial:
E:\matlab\mex
And use the VS project file to coding and compiling, NOT linking.

two files:
mex_basic
mex_mv

编译时添加包含文件和库文件:

例子:mex mex_mv.c -I'C:\Program Files\GnuWin32\include' -L'C:\Program Files\GnuWin32\lib\lib-version-kevin' -lgsl -lgslcblas

其他常用的库
-I'C:\Program Files\OpenCV\cv\include' -I'C:\Program Files\OpenCV\cvaux\include' -I'C:\Program Files\OpenCV\cxcore\include' -I'C:\Program Files\OpenCV\ml\include' -I'C:\Program Files\OpenCV\otherlibs\highgui' -L'C:\Program Files\OpenCV\lib' -lcxcore -lcv -lcvaux -lhighgui

-I'D:\Library\gsl-1.13'
-L'D:\Library\gsl-1.13'

注:mex can only recognition .lib file in windows.
I brutely change .a to .lib. It works for gnuwin32 gsl library. NOT SURE it works for every library.


如果 mex function 用到其他编译单元,要放在同一个 mex 语句

比如
mex mex_func.c other1.c other2.c


在 Matlab 中输出
  1. printf can be used to output message.
  2. mexErrMsgTxt stops the mex-file besides outputing message.

For linux
See example ~/project/mpi_ch2
Makefile

MATLABDIR=/opt/matlab
INCLUDES=-I$(MATLABDIR)/extern/include
LDIRS= -L$(MATLABDIR)/bin/glnx86

2. 调试
查找 Matlab 帮助 index:



3. 以前写的一个文档,其中最后一部分只针对 VC 6.0,新版本的 visual studio 要根据 “Debugging C Language MEX-Files”

Matlab 中C/C++ 函数调用和调试

0 comments: