Run Matlab Coder Output Project on Visual Studio or g++

1.6k views Asked by At

I wanted to convert my simulation code in Matlab to C++ and run it with a C++ compiler such as Visual Studio or g++. I modified my code to successfully build a C++ project using Matlab Coder. The coder returns a .prj file in the top project directory, and tens or hundreds of c++ source/header/object files in the dll directory. I tried to open the .prj files with Visual Studio 2012 or 2013 (I tried with both versions) that was unsuccessful and they cannot even open a .prj file. I have made sure to configure the Visual Studio as the C++ language compiler in Matlab using mex -setup. Does anyone have any idea how to handle the .prj and the subsequent files with Visual Studio or g++? I don't want to make it executable at the moment; I just want to run it with either compilers and make sure it's working. Thanks

2

There are 2 answers

2
Severin Pappadeux On

Does anyone have any idea how to handle the .prj

you supposed to run "mcc" on .prj file, I believe

Quote

"Specify that mcc use settings contained in the specified project file. Use

-F project_name.prj to specify project_name as the project file name when calling mcc. This option enables the .prj file, along with all of its associated settings, to be fed back to mcc. Project files created using either mcc or deploytool are eligible to use this option. When using -F, no other arguments may be invoked against mcc."

0
Ryan Livingston On

The prj file that is generated is used for the MATLAB Coder GUI, rather than Visual Studio. If you double-click that file in MATLAB, the Coder GUI will open and you can generate code using it.

For a simple example of using the generated code in Visual Studio you can refer to the documentation.

If you are going to use the generated code on the same platform on which you are running MATLAB, I would recommend changing the output type to a static library (LIB) or a shared library (DLL). Doing so will cause the C or C++ code to be generated and then compiled into a library using the compiler that you specified. Then you can write code that calls the functions declared in the generated header file(s) and link in the generated library when compiling your hand-written code.

For Visual Studio, you'll need to add the codegen directory, codegen/lib/<proj_name> to the include path and add the generated LIB/DLL file as a dependency. With GCC, you'll need to add the same directory to the include path when compiling your code and link in the library.

The second half of my other answer has more information in it.

Alternatively, if you have a C main function written, you can set the output type to EXE and specify the main function to have MATLAB Coder generate code, compile it with your provided main and compile a standalone executable.