声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1920|回复: 0

[混合编程] 在Visual C++的IDE环境中编译MATLAB MEX程序

[复制链接]
发表于 2016-3-14 14:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
1. 目的

    一般情况下,我们都是在MATLAB命令行或DOS命令行下编译MEX程序。
    所用的命令就是:mex filename.c

    这有很多不方便的地方:
        a. 虽然mex也可以编译C++的mex程序,但是它的主框架仍是C的
        a. 当程序有多个模块时,需要多次使用mex命令,操作很麻烦
        b. 不能利用VC特有的ClassWizard自动创建和维护类
        c. 不能用MFC类库编写应用程序
        d. 不能方便地进行类似VC的项目管理
        e. 等等...
    这样一个命令行的编程环境可能会大大束缚你的想象力...
    其实解决问题的方法很简单,下面以VC6环境为例详细解说如何在IDE中编译MEX程序。
2. 步骤

   (1)准备工作 (这里假设MATLAB的路径为:C:\MATLAB)

      a. 由matlab.def创建库文件matlab.lib
        进入c:\matlab\extern\include目录,用lib命令生成matlib.lib
        C:\>cd \matlab\extern\include

        C:\MATLAB\extern\include>lib /def:matlab.def /out:matlab.lib
        Microsoft (R) Library Manager Version 6.00.8447
        Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

        LIB : warning LNK4068: /MACHINE not specified; defaulting to IX86
           Creating library matlab.lib and object matlab.exp

        C:\MATLAB\extern\include>
        警告信息不用理它。

      b. 修改mex.h
        该文件也在C:\MATLAB\extern\include目录
        找到这一行:void mexFunction(
        替换为    : __declspec( dllexport ) void mexFunction(
        另保存为  :mex_vc.h

    (2)启动VC,创建MFC AppWizard(dll)程序框架

    (3)设置系统选项

      a. 设定头文件和库文件路径
           菜单Tools->Options->Directories
                Include files:添加c:\matlab\extern\include
                Library files:添加c:\matlab\extern\include

      b. 设置编译连接选项
            菜单Project->Settings
            C/C++->Preprocessor definitions: 添加MATLAB_MEX_FILE
            Link->Object/Library modules:    添加matlab.lib
        注1. 其它优化、调试以及头文件、库文件等选项看情况调整
        注2. Release版在General->Microsoft Foundation Classes选项中,
             必须设置为Use MFC in a Static Library. 原因不明:(

    (4)编写DLL主程序

        a. 文件名要用.cpp为扩展名,头两行必须是:
                #include "stdafx.h"
                #include "mex_vc.h"

        b. 编写mexFunction函数,用如下格式声明:
                void mexFunction(
                         int nlhs,       mxArray *plhs[],
                         int nrhs, const mxArray *prhs[]
                         )
                {
                        ...
                }

    (5) 象普通VC的DLL程序一样继续编写其它部分程序,加入其它模块

    (6) 调试错误,用F7编译,生成DLL。

我的例子:
--------------------Configuration: MatWave - Win32 Release--------------------
Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
MatWave.cpp
sample.cpp
mex.cpp
Generating Code...
Linking...
   Creating library Release/MatWave.lib and object Release/MatWave.exp
LINK : warning LNK4089: all references to "SHELL32.dll" discarded by /OPT:REF
LINK : warning LNK4089: all references to "comdlg32.dll" discarded by /OPT:REF

MatWave.dll - 0 error(s), 2 warning(s)

        大功告成,有两个警告,不理它。
        拿到MATLAB里面试试吧,不过要有非法操作和异常退出的心理准备哦:P

        另外:如果要在MATLAB运行中重新编译DLL,需先执行: clear mex
转自:http://blog.sina.com.cn/s/blog_68f34ea50100msq2.html

回复
分享到:

使用道具 举报

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-3-29 19:54 , Processed in 0.061726 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表