Compiling C++11 code as part of a MATLAB mex file

2019-03-28 15:58发布

问题:

I have a piece of code written in C++11, which I want to compile as part of a MATLAB MEX file for GNU/Linux.

The problem is that MATLAB on Linux supports GCC 4.3 (and earlier) only, and does not support GCC 4.7 which is required to compile my C++11 code.

Is it possible to work-around the problem?

Would it be possible to work-around this by compiling some object files using GCC 4.7 and link them into the MEX file using GCC 4.3?

Thanks in advance!

回答1:

If you can write any code in your 4.3 extension and compile it, then just write code to dlopen a shared object that you wrote and compiled in 4.7. Use the 4.7 .so to do all of your c++11 work, and simply pass your information to it through a C interface. The 4.3 extionsion you write can access all the MATLAB interop stuff.

You could do this a variety of other ways as well, but this is the cleanest. You shouldn't try linking an object file to your 4.3 extension, as you will be accessing two different version of the standard library (quite different), and you can't have multiple defnitions of the same classes with different layouts/methods/etc. You'd be fighting the One Definition Rule (ODR) of c++.