重新编译后未更新Mex函数 [英] Mex function not updated after recompile

查看:97
本文介绍了重新编译后未更新Mex函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的mex函数,该函数从库中调用另一个C ++函数.我用

I have a simple mex function, which calls another C++ function from a library. I compile the source with

mex -cxx mymexfunction.cpp -I/some/include -L/some/lib -lmylib

mylib库是动态(.so),并与其他一些库(boost,OpenCV等)链接在一起.

The mylib library is dynamic (.so) and is linked itself against some other libraries (boost, OpenCV and some more).

我遇到的问题是,一旦我一次调用了mymexfunction函数,当我重新编译源代码时,它将不会被更新.我尝试过

The problem I am having is that once I have called the function mymexfunction once, it will not get updated when I recompile the source code. I have tried

clear
clear all
clear mex
clear functions
clear fun('mymexfunction')
munlock('mymexfunction')
unloadlibrary('mymexfunction')

...但是没有任何帮助!我必须重新启动Matlab才能看到更新的mexfunction.即使我删除了已编译的mex文件并重新编译,我仍然可以得到旧版本的mex函数(不在磁盘上,而是在内存中).

...but nothing helps! I have to restart Matlab in order to be able to see the updated mexfunction. Even if I delete the compiled mex file and recompile, I still get the old version of the mex function (not on disk, but in memory).

如果我不链接到mylib,一切都很好,但是我不知道阻止更新的罪魁祸首是什么.不幸的是,该库太大且交织在一起,无法一一删除单个模块.

All is fine if I don't link against mylib, but I have no idea what could be the culprit that prevents the update. The library is unfortunately too large and too interwoven to remove single modules one by one.

是否存在一些可能导致此类问题的已知条件?

Are there some known conditions which could cause such problems?

说明:

我只更新mex函数的内容,而不是库.

I only update the content of the mex function, not the library.

更新:

它可以在Ubuntu 11.04和Matlab R2011a下运行!我试图在我的OpenSUSE机器上重现相同的环境(R2011a,Boost 1.42,动态链接的OpenCV 2.2等),但还是没有运气.所以我得出结论,我的库实际上没有什么问题(否则在Ubuntu下将无法正常工作),但这一定是依赖项和Matlab内部库的冲突.我正式放弃. Praetorian和Amro,谢谢您的帮助!

It works under Ubuntu 11.04 with Matlab R2011a! I tried to reproduce the same environment on my OpenSUSE machine (R2011a, Boost 1.42, OpenCV 2.2 dynamically linked, ...) but still no luck. So I conclude that nothing is actually wrong with my library (otherwise it wouldn't work under Ubuntu) but it must be some collision of the dependencies and Matlab internal libraries. I officially give up. Praetorian and Amro, thank you for your help!

推荐答案

为重现该问题,我为您编写了一个最小的工作示例:一个链接到动态库的MEX文件,并使用其中的一个暴露的功能.我已经在32位WinXP上使用MATLAB R2010b和VS2010作为编译器(针对DLL和MEX)进行了测试.

Trying to reproduce the problem, I've written a minimum working example for your case: a MEX file that links against a dynamic library and use use one of its exposed functions. I've tested the following on WinXP 32-bit using MATLAB R2010b with VS2010 as compiler (for both the DLL and MEX).

该示例仅添加浮点数. MEX文件接受矩阵/向量,并遍历每对库中调用add()函数的元素.

The example simply adds floating-point numbers. The MEX file accepts matrices/vectors and loops over the elements calling the add() function from the library on each pair.

#ifndef ADDER_H
#define ADDER_H

#ifdef __cplusplus
extern "C" {
#endif

#ifdef _WIN32
#   ifdef BUILDING_DLL
#       define DLL_IMPORT_EXPORT __declspec(dllexport)
#   else
#       define DLL_IMPORT_EXPORT __declspec(dllimport)
#   endif
#else
#   define DLL_IMPORT_EXPORT
#endif

DLL_IMPORT_EXPORT double add(double x, double y);

#ifdef __cplusplus
}
#endif

#endif

Adder.c

#include "Adder.h"

double add(double x, double y)
{
    return x+y;
}

mymexfunction.c

#include "mex.h"
#include "Adder.h"

#define X_IN    input[0]
#define Y_IN    input[1]
#define Z_OUT   output[0]

void mexFunction(int output_size, mxArray *output[], int input_size, const mxArray *input[])
{
    double *inX, *inY, *outZ;
    mwSize m,n;
    int i;

    /* check for proper number of arguments */
    if (input_size != 2) {
        mexErrMsgTxt("Two input arguments required.");
    }
    if (output_size > 1) {
        mexErrMsgTxt("Too many output arguments.");
    }

    /* check input argument sizes */
    m = mxGetM(X_IN);
    n = mxGetN(X_IN);
    if ( !mxIsDouble(X_IN) || !mxIsDouble(Y_IN) ) {
        mexErrMsgTxt("Input arguments must be matrices/vectors of doubles.");
    }
    if ( mxGetM(Y_IN)!=m || mxGetN(Y_IN)!=n ) {
        mexErrMsgTxt("X and Y must be of same size.");
    }

    /* Create a matrix for the return argument */
    Z_OUT = mxCreateDoubleMatrix(m, n, mxREAL);

    // get pointers to data
    inX =  (double *) mxGetPr(X_IN);
    inY =  (double *) mxGetPr(Y_IN);
    outZ = (double *) mxGetPr(Z_OUT);

    // compute and store result
    for(i=0; i<m*n; ++i) {
        outZ[i] = add(inX[i], inY[i]);
    }

    return;
}

首先,我们构建了动态库,正如我提到的,我正在使用VC ++来完成这项工作.在具有GCC的基于Unix的系统上,我认为这一步类似于(如果我输入错了,请纠正我):

First we build the dynamic library, as I mentioned I am using VC++ for the job. On Unix-based systems with GCC, I think this step goes like (correct me if I am wrong):

gcc -c -DBUILDING_DLL Adder.c -o Adder.o -I.
gcc -shared -o libAdder.so Adder.o -Wl,--out-implib,libAdder.a

然后在MATLAB中,我们编译MEX文件:

then in MATLAB, we compile the MEX file:

>> mex mymexfunction.c -I. -L. -lAdder

(注意:我将所有内容都放在同一文件夹中,以避免处理路径问题.)

(Note: I put everything in the same folder to avoid dealing with path issues.)

接下来,我们可以在MATLAB中测试该函数:

Next, we can test the function in MATLAB:

>> mymexfunction([1 2;3 4], [5 6; 7 8])
ans =
     6     8
    10    12

使用Sysinternals的 Process Explorer 工具,我们可以查看已加载的通过MATLAB进程,MEX函数和我们的自定义动态库生成的DLL:

Using the Process Explorer tool from Sysinternals, we can view the loaded DLLs by the MATLAB process, the MEX function and our custom dynamic library:

如果我们发出命令clear mex,则将按预期方式卸载两个模块(已使用Process Explorer进行了验证). INMEM也已确认为 @Praetorian 显示:

If we issue the command clear mex then both modules are unloaded as expected (which is verified using Process Explorer). This is also confirmed by INMEM as @Praetorian showed:

clear mex
[~,m] = inmem('-completenames');
any( ismember(m,fullfile(pwd,['mymexfunction.' mexext])) )

最后,如果我们对mymexfunction.c进行一些更改:

Finally if we make some changes to mymexfunction.c:

// add 10 to all results
outZ[i] = add(inX[i], inY[i]) + 10.0;

重新编译MEX,然后再次进行测试(全部在同一会话中,没有重新启动).结果将反映出所做的更改,如您所见:

recompile the MEX, and test it again (all in the same session, no restart). The result will reflect the changes made as you can see:

>> mymexfunction([1 2;3 4], [5 6; 7 8])
ans =
    16    18
    20    22

请尝试在Mac/Linux计算机上重复上述操作.如果您仍然收到旧的总和,那么它一定是非Windows平台特有的bug,应该报告给MathWorks.否则,我怀疑您的代码中必须有一些未发布的资源,导致模块无法正常运行.留在记忆中吗?

Please try to repeat the above on your Mac/Linux machine. If you still receive the old sums, then it must be a bug specific to non-Windows platforms, and should be reported to MathWorks... Otherwise I suspect that in your code, there must be some un-released resources causing the module to remain in memory?

这篇关于重新编译后未更新Mex函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆