Error when building with MEX
Ältere Kommentare anzeigen
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;
}
Antworten (1)
Walter Roberson
am 18 Mär. 2018
1 Stimme
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 Kommentare
VENKATESAN SEETHARAMAN
am 18 Mär. 2018
Bearbeitet: VENKATESAN SEETHARAMAN
am 18 Mär. 2018
Walter Roberson
am 18 Mär. 2018
What is your intention with the code? Are you intending to be able to invoke it as-if it were a built-in MATLAB function? Or are you intending to be able to run it as an independent program outside of MATLAB?
VENKATESAN SEETHARAMAN
am 19 Mär. 2018
James Tursa
am 19 Mär. 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
am 5 Mär. 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
Kategorien
Mehr zu MATLAB Compiler SDK finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!