与MATLAB版本不同的是,在MATLAB MEX库中使用boost [英] Using boost in MATLAB MEX library, different from MATLAB's version

查看:347
本文介绍了与MATLAB版本不同的是,在MATLAB MEX库中使用boost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在创建许多使用我们的通信库的MATLAB MEX文件.该通信库大量使用Boost.现在,MATLAB在内部也使用boost,这意味着在标准设置中,我们不能使用不同于MATLAB随附的boost版本,也不能使用所有地狱.

We're creating a number of MATLAB MEX files that use our communications library. This communication library uses Boost a lot. Now, MATLAB also uses boost internally, which means that in a standard setup, we cannot use a boost version different from the one that comes with MATLAB or all hell ensues.

问题是,我们的Matlab参考版本(增强版1.40)随附的增强版本很旧,并且存在一些错误.我们非常想使用较新的版本.

Problem is, the boost version that comes with our reference version of matlab (boost 1.40) is quite old and has a few bugs. We'd very much like to use a newer version.

我看到的唯一解决方案是创建一个生活在不同名称空间中的自定义版本的boost.这样,名称改写应该可以防止命名冲突.该解决方案有些棘手,因为boost还会导出一些"C"符号,并且具有许多需要全部更改的宏.

The only solution I see is to create a custom version of boost that lives in a different namespace. The name mangling should then prevent naming conflicts. This solution is a bit tricky because boost also exports some "C" symbols and has a number of macro's that will all need to be changed.

是否有不需要创建自定义增强版本的推荐解决方案?

Are there any recommended solutions that don't require the creation of custom boost versions?

推荐答案

一种解决方案是通过编写一个小的加载程序mex文件来更改matlab打开插件的方式,该文件本身不依赖boost,将其称为 foo .mexglx

One solution is to change the way matlab opens your plugin, by writing a small loader mex file which itself has no dependency on boost, call it foo.mexglx

这只是mexFunction调用

It's mexFunction call simply does this

void mexFunction (int nlhs, mxArray * plhs[], int nrhs, mxArray * prhs[])
{
  gMexEntry (nlhs, plhs, nrhs, prhs);
}

其中gMexEntry变量是声明为

where the gMexEntry variable is a function pointer declared as

typedef void (*entryfunc_t)(int, mxArray**, int, const mxArray**);
entryfunc_t gMexEntry;

,并在加载模块时由静态构造函数填充(为简便起见,忽略所有错误检查).

and populated by a static constructor when the module is loaded (all error checking ignored for brevity).

fh = dlopen ('bar.mexglx', RTLD_NOW | RTLD_DEEPBIND );
void * p = dlsym (fh, "mexFunction");
gMexEntry = reinterpret_cast<entryfunc_t> (p);

事件链是,当Matlab调用您的函数时,不带boost依赖关系的精简包装将使用dlopen的 RTLD_DEEPBIND 选项打开带有boost依赖关系的函数,该函数将放置该库中符号的查找范围(使用您的boost版本)领先于全局范围(使用Matlab的旧boost).然后,实际的mexFunction调用将转发到bar.

The chain of events is that when Matlab calls your function, the thin wrapper with no boost dependency will open your function with the boost dependency using the RTLD_DEEPBIND option of dlopen, which will place the lookup scope of the symbols in this library (using your version of boost) ahead of the global scope (using Matlab's old boost). Then the actual mexFunction call will forward to bar.

如果您正确执行cmdline链接,则使用'ldd'应该会看到' foo.mexglx '与boost没有依赖性,而' bar.mexglx '具有所有通常的依赖关系.

If you do your cmdline linking correctly, using 'ldd' you should see that 'foo.mexglx' has no dependency on boost, and 'bar.mexglx' has all your usual dependencies.

我几个月来一直在大量使用这种技术,没有明显的失败迹象.我仍然有些担心,我不了解的地方可能出了问题,但是暂时这是我唯一的解决方案(除了编写完整的进程外执行引擎来复制mxArray接口和与管道进行通信,或静态链接一切,这对我来说是不实际的)

I've been using this technique heavily for months with no obvious signs of failure. I do still have some slight concerns that something I don't understand might go wrong, but for the time being this is the only solution I've got (other than writing a full out-of-process execution engine replicating the mxArray interface and communicating with pipes, or linking everything statically which isn't practical for my situation)

这篇关于与MATLAB版本不同的是,在MATLAB MEX库中使用boost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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