Undefined reference to mexPrintf for simple helloworld program

I've been trying to get started using mex to compile c code for use in simulink s functions. The code below throws an undefined reference error upon compiling.
#include "mex.h"
#include <math.h>
#include <matrix.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello World!\n");
}
undefined reference to `mexPrintf'
collect2.exe: error: ld returned 1 exit status
mex -setup displays
MEX configured to use 'MinGW64 Compiler (C)' for C language compilation.
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the
new API. You can find more information about this at:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
To choose a different language, select one from the following:
mex -setup C++
mex -setup FORTRAN
Please let me know if you see the issue.

답변 (1개)

James Tursa
James Tursa 2018년 9월 4일
편집: James Tursa 2018년 9월 4일
Your code is a standard mex routine that can be compiled and run at the MATLAB command line prompt as follows:
mex helloworld.c % or whatever the actual filename is
helloworld
s-function mex code has a different interface from what is in your code above that involves input & output ports etc. Maybe you could start with the s-function mex examples in the doc and then adapt it to what you need.

댓글 수: 1

Yes that is how I've been compiling it. The s function builder and s function examples all fail to compile as well (undefined reference to mxCalloc among other errors). I expect this error is related to the other one and was trying to solve it in the simpler case first.
Maybe another good thing to note is that if I replace
#include "mex.h"
with
include <stdio.h>
and mexPrintf with printf, it compiles but then gives this error on execution.
Invalid MEX-file 'R:\path\helloworld.mexw64':
R:\path\helloworld.mexw64 is not a valid Win32
application.
I am running 64 bit matlab on a 64 bit machine so this error is also perplexing.

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

카테고리

도움말 센터File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

제품

릴리스

R2016a

질문:

2018년 9월 4일

편집:

2018년 9월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by