Error when building with MEX

조회 수: 12 (최근 30일)
VENKATESAN SEETHARAMAN
VENKATESAN SEETHARAMAN 2018년 3월 18일
댓글: Harsha Nimje 2020년 3월 5일
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;
}

답변 (1개)

Walter Roberson
Walter Roberson 2018년 3월 18일
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
James Tursa
James Tursa 2018년 3월 19일
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
Harsha Nimje 2020년 3월 5일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Compiler SDK에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by