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 commentaires

How are you compiling and linking? The message indicates you are not linking in the MATLAB API libraries.
Hi, so I have a makefile and in the makefile I have a long list of include files. Then the gcc command is spelled out in the makefile with the includes. There must be some Matlab specific includes/dll that I probably should be specifying to get the mexprintf command recognized by the compiler. Any idea where the Matlab includes are?
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".
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.
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(...);
...
}
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.
Try to move #include "mex.h" before the other includes.

Connectez-vous pour commenter.

Réponses (1)

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 commentaires

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.
No, do not comment the required line away. Try to include mex.h at first before the other headers.
@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?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Centre d'aide et File Exchange

Question posée :

le 25 Nov 2015

Commenté :

le 1 Nov 2017

Community Treasure Hunt

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

Start Hunting!

Translated by