Help with creating a MEX file in MATLAB from multiple C++ Source codes

조회 수: 12 (최근 30일)
I am trying to create a MEX file in MATLAB with multiples C++ source codes in .cpp and .h extensions. The C++ source codes builds fine with no errors in Microsoft Visual C++ 2010 professional and i have created a static library of .lib file All source code files and static library of functions are located in my MATLAB current folder path In MATLAB i have selected my default compiler as MS Visual C++ Professional 2010 and i am using the following syntax in MATLAB mex -v [list of C++ source code files i.e. .cpp and .h files] -l[static library name] -L.
I get a link error in MATLAB as; LINK : error LNK2001: unresolved external symbol mexFunction C:\Users\abo09001\AppData\Local\Temp\mex_Ko9pXP\templib.x : fatal error LNK1120: 1 unresolved externals
I do not know how to program in C++ but i am relatively ok in MATLAB. Any help with the error will be appreciated.
  댓글 수: 3
Nathan Crosty
Nathan Crosty 2013년 9월 17일
Are your functions and or symbols that you are referencing externs?
Abigail Osei-Asamoah
Abigail Osei-Asamoah 2013년 9월 17일
I don't believe they reference externs

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

채택된 답변

Kaustubha Govind
Kaustubha Govind 2013년 9월 17일
All MEX-functions need to have an entry-point named mexFunction with a specific prototype. I would recommend that you read the section Creating a Source MEX-File to help you get started.

추가 답변 (1개)

Abigail Osei-Asamoah
Abigail Osei-Asamoah 2013년 9월 17일
I already tried to include the gateway function to the .cpp files and the #Include "mex.h" but i get errors when i try try to create the MEX file in MATLAB with the mex command after adding the gateway function and the #include "mex.h"
  댓글 수: 4
Abigail Osei-Asamoah
Abigail Osei-Asamoah 2013년 9월 18일
After adding #include "mex.h" and void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
/*sourcecode here */ }
to my main.cpp code and trying to create mex with my .cpp files and headers i get the following error in matlab :
main.cpp main.cpp(22) : error C2601: 'main' : local function definitions are illegal main.cpp(16): this line contains a '{' which has not yet been matched
I checked line 16 in the C++ code and the '{' is matched by '}' after the source code so i don't know what is wrong. How do i fix this error? Any help is appreciated. Thanks
Abigail Osei-Asamoah
Abigail Osei-Asamoah 2013년 9월 18일
Thank you all for the help but i have been able to create the MEX files. I was getting an error because i was placing another function in my source .cpp codes within the mex function used to create the gateway routine in my .cpp files. Once i separated the mex function from that function i.e. i didn't define a function within a function,MATLAB has successfully created my MEX file. Thanks.

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

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by