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!