MATLAB Answers

0

MATLAB engdemo.c VS Prof - Windows error compiling

Asked by Big dream on 11 May 2017
Latest activity Commented on by Big dream on 16 May 2017
Hallo Everyone,
I'm new with mex environment and right now i do multiple exercises from MATLAB External Application Handouts, such as the "engdemo.c". But the problem i get stuck since its compile always shows error.
>> mex engdemo.c
Building with 'Microsoft Visual C++ 2013 Professional (C)'.
Error using mex
LINK: error LNK2001: Unresolved external symbol "MexFunction".
Engdemo.lib: fatal error LNK1120: 1 non-resolved external
and then, i change the main() function with void mexFunction(), then come this error,
Error using mex
Library "engdemo.lib" and object "engdemo.exp"
created.
Engdemo.obj: error LNK2019: Reference to non-resolved external Symbol "engEvalString" in function "mexFunction".
Engdemo.obj: error LNK2019: Reference to non-resolved external
Symbol "engOpen" in function "mexFunction".
Engdemo.obj: error LNK2019: Reference to non-resolved external
Symbol "engClose" in function "mexFunction".
Engdemo.obj: error LNK2019: Reference to non-resolved external
Symbol "engGetVariable" in function "mexFunction".
Engdemo.obj: error LNK2019: Reference to non-resolved external
Symbol "engPutVariable" in function "mexFunction".
Engdemo.obj: error LNK2019: Reference to non-resolved external
Symbol "engOutputBuffer" in function "mexFunction".
Engdemo.mexw64: fatal error LNK1120: 6 non-resolved external
I've searched about this topic, and found several pages,such as i have to put the engmatopts.bat , since i use Windows. But i can't find this files.
http://148.204.81.206/matlab/matlab_external/compiling-engine-applications-with-the-mex-command.html#bsq78t5-1
Did anyone ever have the same problem and has the solution?

  0 Comments

Sign in to comment.

1 Answer

Answer by Nagarjuna Manchineni on 15 May 2017

The MATLAB C/C++ engine library contain routines that allow you to call MATLAB from your own programs, using MATLAB as a computation engine.
The first error received is a linker error which indicates that the C/C++ source file that is being MEXed does not contain the MEX gateway function "mexFunction". That is the reason why you are able to proceed once you change the function signature of 'main()' to the following signature:
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
However, you can build the executables using mex command by specifying the flag '-client engine'. For example, the following command will create an executable (using main() method in engdemo.c not the mexFunction)
>> mex engdemo.c -client engine
If you want to create a 'mex' file for the same example, then change the function signature to 'mexFunction'. The linker errors that you are encountering now is due to the functions used in 'engdemo.c'. The functions 'engOpen', 'engClose', .., 'engOutputBuffer' are appearing in the 'engdemo.c' but the compiler/linker is not able to recognize them. So, you have to specify the proper headers and libraries while building with mex (C:\Program Files\MATLAB\R2016b is my 'matlabroot').
mex '-IC:\Program Files\MATLAB\R2016b\extern\include' '-LC:\Program Files\MATLAB\R2016b\extern\lib\win64\microsoft' -llibeng.lib -llibmx.lib engdemo.c

  1 Comment

Hallo Nagarjuna,
Thanks for your advices.
I've typed this code in command MATLAB and tried to compile 'engdemo.c' again with using 'void mexFunction()'
mex '-IC:\Program Files\MATLAB\R2016b\extern\include' '-LC:\Program Files\MATLAB\R2016b\extern\lib\win64\microsoft' -llibeng.lib -llibmx.lib engdemo.c
Building with 'Microsoft Visual C++ 2013 Professional (C)'.
MEX completed successfully.
but again i got this error
Library "engdemo.lib" and object "engdemo.exp" are created.
Engdemo.obj: error LNK2019: Reference to non-resolved external symbol "engEvalString" in function "mexFunction".
Engdemo.obj: error LNK2019: Reference to unresolved external symbol "engOpen" in function "mexFunction".
Engdemo.obj: error LNK2019: Reference to unresolved external symbol "engClose" in function "mexFunction".
Engdemo.obj: error LNK2019: Reference to non-resolved external symbol "engGetVariable" in function "mexFunction".
Engdemo.obj: error LNK2019: Reference to non-resolved external symbol "engPutVariable" in function "mexFunction".
Engdemo.obj: error LNK2019: Reference to non-resolved external symbol "engOutputBuffer" in function "mexFunction".
Engdemo.mexw64: fatal error LNK1120: 6 non-resolved external
Then when i used the function 'main()' and then compiled it, it works
>> mex engdemo.c -client engine
Building with 'Microsoft Visual C++ 2013 Professional (C)'.
MEX completed successfully.
But when i called this programm 'engdemo', it didn't show any results as expected. So, do we get the same result, if we compile engdemo.c with 'mex engdemo.c -client engine' and 'mex engdemo.c'?

Sign in to comment.