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일

1 개 추천

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

VENKATESAN SEETHARAMAN
VENKATESAN SEETHARAMAN 2018년 3월 18일
편집: VENKATESAN SEETHARAMAN 2018년 3월 18일
I'm sorry, I didn't understand any of that. Thanks for the answer though.
Walter Roberson
Walter Roberson 2018년 3월 18일
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
VENKATESAN SEETHARAMAN 2018년 3월 19일
I have around seven .hpp files and one .cpp file. I was able to run them using GCC in Cygwin with the MAKE command (I didn't write the codes). But now, I have written a code in MATLAB whose output needs to be transferred to the .cpp file I have. Initially, I thought I would have to translate my MATLAB code into a C++ code but, later, while doing some research on the internet I found that there is a means to compile C++ codes in MATLAB using the mex command (or mex function, I don't know what it exactly is. Using 'mex' before a .cpp file compiles it. That's all I know). That's when I thought I should get to know how MEX works with a HelloWorld program of my own.
The intention of the HelloWorld code was to learn how MEX works. I didn't know what to do to get the code to run when I posted this question. I was going through the example files and observed that we need a '#include "mex.h"' and a "void mexFunction" defined in order for it to work. And then I realized that this is what you have said in the first line of your answer.
Yes, I am trying to invoke it as-if it were a built-in MATLAB function, and this is exactly what I also want to do with the seven .hpp and the one .cpp file(s) I have because I believe that then I wouldn't have to translate my MATLAB code into C++ (I know nothing about C++ as I don't have a Computer Science background).
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

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

카테고리

도움말 센터File Exchange에서 MATLAB Compiler SDK에 대해 자세히 알아보기

질문:

2018년 3월 18일

댓글:

2020년 3월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by