trying to compile a c file using mexprintf and getting an undefined reference to ‘mexPrintf’
이전 댓글 표시
I am trying to compile some c source code that includes a command like:
mexPrintf( "pPath: %s\n", pPath );
but I get a compilation error that says:
Undefined reference to ‘mexPrintf’
Why is my compiler gcc (using TDM-GCC 64) not recognizing mexPrintf?
Anybody have any thoughts?
댓글 수: 7
James Tursa
2015년 11월 25일
How are you compiling and linking? The message indicates you are not linking in the MATLAB API libraries.
macheson
2015년 11월 27일
Adithya Addanki
2015년 12월 1일
You may find the MATLAB API("mex.h") under the MATLAB installation directory (type "matlabroot" at the MATLAB command prompt). And then please navigate to the directory "extern/include".
BHUSHAN MUTHIYAN
2017년 10월 30일
Hello,
I have included "mex.h" file in my directory still it gives me error:
*/tmp/cchNm1kq.o: In function `main':
main_method2.c:(.text+0xdb): undefined reference to `mexPrintf'
collect2: error: ld returned 1 exit status*
Can you please let me know about it.
Thank you.
OCDER
2017년 10월 30일
Did you include mex.h in your c code? Also, ensure your code name is .cpp for c++ code, and .c for c code, as compilers behave differently.
#include "mex.h"
void func(...) {
}
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
func(...);
...
}
BHUSHAN MUTHIYAN
2017년 10월 30일
편집: Walter Roberson
2017년 10월 31일
Hello Donald,
Thank you for your reply.
Below is my c-code:
-------------------------------
#include "rt_nonfinite.h"
#include "addition_fixpoint.h"
#include "addition_fixpoint_terminate.h"
#include "addition_fixpoint_initialize.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "rtwtypes.h"
#include "addition_fixpoint_types.h"
#include <stdio.h>
#include "mex.h"
int32_T addition_fixpoint(int16_T a, int16_T b)
{
int32_T add;
int32_T i0;
int32_T i1;
int32_T i2;
int32_T i3;
i0 = a << 2;
i1 = b;
if ((i0 & 262144) != 0) {
i2 = i0 | -262144;
} else {
i2 = i0 & 262143;
}
if ((i1 & 262144) != 0) {
i3 = i1 | -262144;
} else {
i3 = i1 & 262143;
}
i0 = i2 + i3;
if ((i0 & 262144) != 0) {
add = i0 | -262144;
} else {
add = i0 & 262143;
}
return add;
}
int main()
{
int16_T a = 804;
int16_T b = 5;
int32_T add = addition_fixpoint(a,b);
printf("addition = %d \n",(int)add);
return 0;
}
---------------------
It gives me error as:
----------------------------
/tmp/cchNm1kq.o: In function `main':
main_method2.c:(.text+0xdb): undefined reference to `mexPrintf'
collect2: error: ld returned 1 exit status
------------------------------
Can you please let me know about it.
Thank you.
Jan
2017년 10월 31일
Try to move #include "mex.h" before the other includes.
답변 (1개)
James Tursa
2017년 10월 31일
This is somewhat confusing. Including "mex.h" means that the code is intended to be a mex function (compiled into a dll) with the interface routine "mexFunction" present. You do not have "mexFunction" present in this code, and in fact have "main()" in your code. This indicates that the code is intended to be a standalone program, and not a dll. So you have a contradiction in your code. It can't simultaneously be a dll and a standalone program.
For starters, maybe get rid of this line since you are apparently not building a mex function:
#include "mex.h"
And don't use any of the API functions that start with "mex" in your code, like mexPrintf, since they are intended specifically for mex functions and not for standalone code.
댓글 수: 3
BHUSHAN MUTHIYAN
2017년 10월 31일
편집: BHUSHAN MUTHIYAN
2017년 10월 31일
Hello James,
Thank you for your response. As you can see I haven't used any mex API in my code.
"It can't simultaneously be a dll and a standalone program." I did not get this ?
Can you help me running with code, because, this would solve my many more doubts in future.
I have just used printf() statement in last to print the integer value.
PLease, let me know about it.
I have commented the line
//#include "mex.h"
But, still getting same error.
Thank you for your kind help.
Jan
2017년 10월 31일
No, do not comment the required line away. Try to include mex.h at first before the other headers.
James Tursa
2017년 11월 1일
@Jan: I do not understand why "mex.h" would ever need to be included in a standalone program. What is the purpose of having prototypes of mex functions that can only be used in a mex dll in a standalone program? I could see including "matrix.h", and maybe that is the only reason for including "mex.h" since it includes "matrix.h". But in this case why not just inlcude "matrix.h" directly?
카테고리
도움말 센터 및 File 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!