MATLAB not finding its own library

11 views (last 30 days)
Scott
Scott on 7 Sep 2023
Answered: Madheswaran on 17 Feb 2025
I have a C program that I am trying to compile with mex. I am able to compile the program in Visual Studio if I keep the main() function, but if I substitute in mexFunction() for main() and try to compile in MATLAB I get the following errors:
Error using mex
Creating library main_tirtos.lib and object main_tirtos.exp
Clock_HAL.obj : error LNK2019: unresolved external symbol engEvalString referenced in function
Clock_start
engine_init.obj : error LNK2001: unresolved external symbol engEvalString
RTOS_HAL.obj : error LNK2019: unresolved external symbol engOpen referenced in function BIOS_start
engine_init.obj : error LNK2001: unresolved external symbol engOpen
PIN_HAL.obj : error LNK2019: unresolved external symbol engGetVariable referenced in function
PIN_getOutputValue
engine_init.obj : error LNK2001: unresolved external symbol engGetVariable
PIN_HAL.obj : error LNK2019: unresolved external symbol engPutVariable referenced in function
PIN_setOutputValue
main_tirtos.mexw64 : fatal error LNK1120: 4 unresolved externals
It seems that MATLAB is not finding its own "engine.h" library, though I am sure I have the necessary #include as I am able to compile with Visual Studio. Any help would be appreciated!
  1 Comment
Scott
Scott on 7 Sep 2023
To simplify debugging this issue I wrote a very small program:
#include "mex.h"
#include "engine.h"
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
Engine *ep;
printf("MEX!\n");
engEvalString(ep,"disp(\'done\')");
}
If the engEvalString line is commented out, the program compiles and runs as expected. As soon as engEvalString is added back in, I get the same error:
Error using mex
Creating library mex_engTest.lib and object mex_engTest.exp
mex_engTest.obj : error LNK2019: unresolved external symbol engEvalString referenced in function
mexFunction
mex_engTest.mexw64 : fatal error LNK1120: 1 unresolved externals

Sign in to comment.

Answers (1)

Madheswaran
Madheswaran on 17 Feb 2025
Hi Scott,
To build an engine application using MEX, you need to specify the '-client engine' flag when calling the 'mex' command. This informs MATLAB that you are building an engine application, rather than a traditional MEX file. You can find more details in the official documentation: https://mathworks.com/help/matlab/matlab_external/build-windows-engine-example.html
Without these flags, MATLAB will attempt to create a standard MEX file, which will lead to errors because your code is intended for an engine application, not a typical MEX source file.
Additionally, consider using "int main(int argc, char *argv[])" instead of "void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])" to properly define the entry point for a standalone application when working outside the MEX environment.
For more information on using MATLAB with C++, refer to the following documentation: https://mathworks.com/help/matlab/calling-matlab-engine-from-cpp-programs.html
Hope this helps!

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!