Question

I have a C++ library with a particular function that returns a boost::any, whose value type is a particular enum defined in a different included library. This normally works fine.

But when I link to my library dynamically from a Matlab mex file, typeid(the_enum_t) seems to be different for the things made in my library and those made in the caller, in that it doesn't compare ==. Since I'm actually using flann, whose version of boost::any performs a check based on type_info::==, this makes everything break. Static linking works fine, but that's kind of a pain here, and I'd really rather have it work either way.

I thought type_info::== was supposed to work consistently across library boundaries. Does this have to do with how Matlab dynamically loads libraries from mex?


Here's some code to reproduce this (also available in easy-to-download form, with a makefile, in this gist).

First, a stand-in for the library that defines the enum (flann):

namespace library {
    enum the_enum_t { el_one, el_two, el_three };
}

Now a proxy for my library, stubby.hpp:

#include <boost/any.hpp>
#include "the_enum.hpp"
boost::any the_function();

And its implementation stubby.cpp:

#include "stubby.hpp"
boost::any the_function() {
    return boost::any(library::el_two);
}

Finally, test code test.cpp, which is compiled with -DNO_MEX for a standalone and not for a mex file:

#include "stubby.hpp"
#include <boost/any.hpp>

#ifdef NO_MEX
#include <cstdio>
using std::printf;
int main() {
#else
#include "mex.h"
void mexFunction(int nlhs, mxArray **plhs, int nrhs, const mxArray **prsh) {
#endif
    boost::any val_any = the_function();
    printf("%s (equal: %d)\n",
            val_any.type().name(),
            val_any.type() == typeid(library::the_enum_t));
}

I get the expected output

N5flann17flann_algorithm_tE (equal: 1)

from each of

$ g++ -o test{,.cpp} -DNO_MEX libstubby.a && ./test      
$ g++ -o test{_s,.cpp} -DNO_MEX libstubby.so && ./test_s
$ ln -sf test{,_s}.cpp && mex test_s.cpp libstubby.a && matlab -r test

But dynamically linking a mex file doesn't work:

$ mex test.cpp libstubby.so && matlab -r test
N5flann17flann_algorithm_tE (equal: 0)

I see this same behavior on

  • Matlab R2011b and R2011a, OSX 10.7, Apple gcc 4.2.1
  • Matlab R2011b, CentOS 5.7, gcc 4.1.2
  • Matlab R2010a, Ubuntu 11.04, gcc 4.4.5

The weird thing is that I could've sworn it worked a few months ago, but maybe I just did a bad job of testing.

Obviously I can get around this by static linking. But why is this happening? Does it have something to do with the way Matlab loads mex files and their libraries?

Was it helpful?

Solution

Boost has a workaround for this issue; see https://svn.boost.org/trac/boost/ticket/754

It's possible that boost is failing to enable the workaround. Try passing compiler flag -DBOOST_AUX_ANY_TYPE_ID_NAME per the patch on that ticket.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top