Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 312
Yoav Avatar asked Mar 29 '12 14:03

Yoav


People also ask

How do you make a MEX file in MATLAB?

Create Source FileOpen MATLAB Editor, create a file, and document the MEX file with the following information. Add the C/C++ header file, mex. h , containing the MATLAB API function declarations. Save the file on your MATLAB path, for example, in c:\work , and name it arrayProduct.

What is C MEX MATLAB?

A MEX file is a function, created in MATLAB®, that calls a C/C++ program. A MEX file is a function, created in MATLAB, that calls a C/C++ program or a Fortran subroutine. A MEX function behaves just like a MATLAB script or function.

Can MATLAB compile C++?

In MATLAB®, you can extend your C and C++ code with a MEX function and call it like any MATLAB built-in function. That means you can use existing C and C++ code without rewriting your algorithms in MATLAB. MEX functions enable C and C++ code to create and modify MATLAB arrays in the MATLAB workspace.


1 Answers

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

like image 80
ex0du5 Avatar answered Sep 28 '22 06:09

ex0du5