trying to compile a c file using mexprintf and getting an undefined reference to ‘mexPrintf’

13 views (last 30 days)
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 Comments
BHUSHAN MUTHIYAN
BHUSHAN MUTHIYAN on 30 Oct 2017
Edited: Walter Roberson on 31 Oct 2017
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.

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 31 Oct 2017
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 Comments
Jan
Jan on 31 Oct 2017
No, do not comment the required line away. Try to include mex.h at first before the other headers.
James Tursa
James Tursa on 1 Nov 2017
@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?

Sign in to comment.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!