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 日

0 投票

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 件のコメント

Mitchell Hoffmann
Mitchell Hoffmann 2018 年 9 月 4 日
編集済み: Mitchell Hoffmann 2018 年 9 月 4 日
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 ExchangeWrite 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