How to run a C++ code in Matlab

174 views (last 30 days)
MINA on 22 Feb 2016
Answered: Kestas Subacius on 13 Jan 2020
I have a code written in C++. How can I run this code in Matlab? Do I need to write some extra code to be able to run this code in matlab? Or do I need to change it completely? Thanks

Answers (3)

KSSV on 22 Feb 2016
You have to write wrappers. You must let MATLAB know that the code is c++; you have to write a code for input and output. The main code in c++ remains the same. You have to use mex compiler. You may read below documentation.

Walter Roberson
Walter Roberson on 22 Feb 2016
It depends on the form of the C++ code. If it is compatible with being a shared object (such as being a DLL) then you can use loadlibrary() to attach to it, and calllib() to call into it, without needing to change the code. You would need a .h file (or equivalent) to use this.
In most operating systems and compilers, you would need special flags when compiling the C++ source to generate a shared object. In some operating systems, though, you could technically get away with just a normal compile (but you would probably routinely include some special flags for the task anyhow so that you create a better shared object.)

Kestas Subacius
Kestas Subacius on 13 Jan 2020
We've introduced the capability to call C++ libraries directly from MATLAB as of R2019a. Please take a look:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!