Error when building with MEX
12 views (last 30 days)
Show older comments
I'm learning how to run c++ codes using matlab. For starters I tried building the HelloWorld program in a .cpp file which I have written below. I also installed MingW-W64 using the TDM-GCC installer. I am now able to do the exact same things as mentioned in the example (Build C MEX file) here: https://nl.mathworks.com/help/matlab/ref/mex.html
Here is the problem. When I type the command 'mex HelloWorld.cpp' in MATLAB I get the following error:
Error using mex
Cannot export mexFunction: symbol not defined
collect2.exe: error: ld returned 1 exit status
However, a .obj file is generated when I type the command 'mex -c HelloWorld.cpp' (I'm unsure how to run this though).
HelloWorld.cpp code:
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
return 0;
}
0 Comments
Answers (1)
Walter Roberson
on 18 Mar 2018
The entry point for a mex file is not main(): it is mexFunction()
mex() is used to compile functions that use the mex api to create something that can be called as a built in MATLAB function. It is not designed to be used by default to compile standalone C or c++ programs and compiling a C or C++ program that does not use the mex api with mex will not make the program callable as a function from MATLAB.
Your helloworld is a complete program by itself, suitable to run as a process, not as a subroutine. If you wanted to invoke it from MATLAB you would compile it normally without mex and you would use system() to invoke it.
See also loadlibrary() to invoke C or C++ functions that have not been written using the mex api
5 Comments
James Tursa
on 19 Mar 2018
For your actual problem, what are the variables that you need to pass to your C++ code? I.e., how many variables do you need to pass, and what are their class and sizes? Same question for the outputs (i.e., what will you be passing back to MATLAB from your C++ code).
Harsha Nimje
on 5 Mar 2020
I am facing the same error even though I have included all the files and defination of all the variable. Could you please tell what should be ncluded in the mexfunction().
I have tried to run the "copyfile(fullfile(matlabroot,'extern','examples','mex','yprime.c'),'.','f')" coomand it generated the mex successfully. Thank you in advance
See Also
Categories
Find more on MATLAB Compiler SDK in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!