MEX compile type error

조회 수: 1 (최근 30일)
Alex Coningham
Alex Coningham 2014년 5월 26일
댓글: Alex Coningham 2014년 5월 26일
Hi guys,
MATLAB Version 7.12.0.635 (R2011a) 32-bit Windows
I am working with some (very cool!) code for texture synthesis and have it working OK, though MATLAB is asking for a .c file to be compiled. I have researched the process, and after pointing MATLAB to the correct folder, I ran the "mex innerProd.c" command, and it threw two errors, first it was looking for "strings.h", which I fixed by manually changing that to "string.h" in the .c file, but there is a second error that I cannot work out. Below is the error and the code I think it is pointing to.
EDU>> mex innerProd.c
Error innerProd.c: 36 type error in argument 2 to `sprintf'; found `int' expected `pointer to const char'
1 errors, 0 warnings
C:\PROGRA~2\MATLAB\R2011A~1\BIN\MEX.PL: Error: Compile of 'innerProd.c' failed.
??? Error using ==> mex at 208
Unable to complete successfully.
Here are lines 35-37 of innerprod.c
if (plhs[0] == NULL)
mexErrMsgTxt(sprintf("Error allocating %dx%d result matrix",wid,wid));
res = mxGetPr(plhs[0]);
Any help would be greatly appreciated.
Cheers, Alex
p.s. The texture synthesis package comes from this website

채택된 답변

James Tursa
James Tursa 2014년 5월 26일
편집: James Tursa 2014년 5월 26일
I don't know how this could have ever worked. In the first place, the arguments to sprintf are wrong. E.g. the 1st argument needs to point to a writable buffer to hold a printed result and you've got a constant character string there. 2nd argument is wrong also. In the second place, the function result of sprintf is an integer value giving the number of characters written, while mexErrMsgTxt is expecting a string argument. So I will give you one simple way to deal with this, and then point out that this code probably isn't even necessary in a mex routine.
You could change this:
if (plhs[0] == NULL)
mexErrMsgTxt(sprintf("Error allocating %dx%d result matrix",wid,wid));
To something like this:
if (plhs[0] == NULL) {
mexPrintf("Error allocating %dx%d result matrix",wid,wid);
mexErrMsgTxt("plhs[0] is NULL");
}
However, in a mex routine, if a memory allocation fails you don't drop down to the next line ... the mex routine simply terminates control back to the caller with an error. So if plhs[0] is the result of some mxCreateWatever call, and that call fails because of a memory allocation error, you WILL NOT drop down to the next line where you can test the plhs[0] value for NULL. So in all likelihood you do not need that if-test at all. If you got there, plhs[0] is guaranteed to not be NULL and that test will never be true (again assuming that it was the result of a mxCreateWhatever call).
  댓글 수: 1
Alex Coningham
Alex Coningham 2014년 5월 26일
This solved my issue, and the file compiled without error, many thanks for your response.
And thank you for your explanation, after implementing your solution to resolve the issue I went back to understand why it was not working and indeed unnecessary.
Many thanks, Alex

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

추가 답변 (0개)

카테고리

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