Compiling a MEX file with Visual Studio

本文详细介绍了如何使用Microsoft Visual C++ IDE在Visual Studio中编译MEX文件,包括创建项目、添加源文件、配置项目属性等步骤,并提供了实际的代码示例和编译流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Compiling a MEX file with Visual Studio

 

 

 

1) If we are working on matrix multiplication, then after compiling the .br file we generate a .cpp like below. Modify the .cpp file directly to use the MEX API. The sections in bold are the minimum code changes necessary.

 

#include "common.h"

#include "Timer.h"

#include "mex.h"

 

static int retval = 0;

 

//int main(int argc, char** argv)

void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])

{

   

       ...

 

        float A<Height, Width>;

        float B<Width, Height>;

        float C<Height, Height>;

        Start(0);

        streamRead(A, inputA);

        streamRead(B, inputB);

       

        // Run the brook program here

        for (i = 0; i < cmd.Iterations; ++i)

        {

            simple_matmult((float)Width, A, B, C);

        }

 

        // Write data back from stream here

        streamWrite(C, output);

        Stop(0);

 

       ...

 

       mexPrintf("Time to execute matrix multiplication – fast \n");

}

 

 

2) Follow the direction below to build MEX-files with the Microsoft Visual C++ integrated development environment. These are based on:

http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_external/f24338.html&http://www.mathworks.com/support/tech-notes/1600/1605.html

Compiling MEX-Files with the Microsoft® Visual C++® IDE

1.      Create a project (matrix_multiply) and insert your MEX source files. (the .cpp file above)

2.      Add mexversion.rc from the MATLAB include directory, matlab\extern\include, to the project.

3.      Create a .def file to export the MEX entry point. On the Project menu, click Add New Item and select Module-Definition File (.def). For example:

4.     LIBRARY “matrix_multiply.mexw32”
5.     EXPORTS mexFunction          <-- for a C MEX-file
6.        or
EXPORTS _MEXFUNCTION@16       <-- for a Fortran MEX-file

7.      On the Project menu, click Properties for the project to open the property pages.

8.      Under C/C++ General properties, add the MATLAB include directory, matlab\extern\include, as an additional include directory.

9.      Under C/C++ Preprocessor properties, add MATLAB_MEX_FILE as a preprocessor definition.

10.  Under Linker General properties, change the output file extension to .mexw32 if you are building for a 32–bit platform or .mexw64 if you are building for a 64–bit platform.

11.  Locate the .lib files for the compiler you are using under matlabroot\extern\lib\win32\microsoft or matlabroot\extern\lib\win64\microsoft. Under Linker Input properties, add libmx.liblibmex.lib, andlibmat.lib as additional dependencies.

12.  Also under Linker General, add the path to those libraries under “Additional Library Directories”. My path was: "C:\Program Files\MATLAB\R2007b\extern\lib\win32\microsoft")

13.  Under Linker Input properties, add the module definition (.def) file you created if it is not already there).

14.  Under Linker Command Line, add the following as Additional options:

/export:mexFunction /dll

15.  Under Linker Debugging properties, if you intend to debug the MEX-file using the IDE, specify that the build should generate debugging information. For more information about debugging, see Debugging on the Microsoft® Windows®Platforms.

 

This should do it. Now you should have a matrix_multiply.mexw32 file created. You can execute that file in MATLAB by typing: matrix_multiply           in the folder with the file.

 

 转自:http://coachk.cs.ucf.edu/GPGPU/Compiling_a_MEX_file_with_Visual_Studio2.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值