MATLAB not finding its own library

조회 수: 11 (최근 30일)
Scott
Scott 2023년 9월 7일
답변: Madheswaran 2025년 2월 17일
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
Scott
Scott 2023년 9월 7일
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

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

답변 (1개)

Madheswaran
Madheswaran 2025년 2월 17일
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!

카테고리

Help CenterFile Exchange에서 Call MATLAB from C에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by