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

1

6

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!

Counterstatement answered 29/3, 2012 at 14:35 Comment(2)
What do you mean by not supported? In the end mex files are just shared libraries with a simple C interface. So you may have to compile it manually and might have to edit some headers but you should be able to get it working.Resuscitator
Please see this answer : #25649449. Basically adding the -std=c++11 to the CFLAGS defined in the mexopts.sh works fine.Stiltner
F
5

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++.

Facula answered 29/3, 2012 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.