MATLAB Coder and MEX-file generation problem on Linux

13 views (last 30 days)
I am using Matlab R2014b on linux with gcc-4.7.
I tried to automatically generate a MEX file for a matlab code function with MATLAB coder. However I was getting a Build error and the Target Build Log was giving linking errors
_coder_WaveletFIR_idealFilter_mex.o: In function `mexFunction':
_coder_WaveletFIR_idealFilter_mex.c:(.text+0x30): undefined reference to `mexAtExit'
_coder_WaveletFIR_idealFilter_mex.c:(.text+0x57): undefined reference to `mxCalloc'
_coder_WaveletFIR_idealFilter_mex.c:(.text+0x124): undefined reference to `mxFree'
_coder_WaveletFIR_idealFilter_info.o: In function `b_emlrt_marshallOut':
_coder_WaveletFIR_idealFilter_info.c:(.text+0x5d): undefined reference to `mxGetData'
and the MEX was not generated. After some digging in the folders created I changed the following line in the makefile (i.e. WaveletFIR_idealFilter_mex.mk ) generated from Matlab-Coder
From:
$(TARGET): $(OBJLIST) $(MAKEFILE)
$(LD) $(EXPORTOPT) $(LINK_FLAGS) $(OBJLIST) $(SYS_LIBS)
To:
$(TARGET): $(OBJLIST) $(MAKEFILE)
$(LD) $(EXPORTOPT) $(OBJLIST) $(LINK_FLAGS) $(SYS_LIBS)
What I did is change the position of the $(LINK_FLAGS) and that worked.
And then I run manually the make wrapper(i.e. WaveletFIR_idealFilter_mex.sh ) that was available via command line. The MEX was correctly generated and I managed to use it. However the automated process in Matlab is still not working. This is because it is always re-writing the make file.
Is there a way I can solve it rather than doing it manually?
  2 Comments
Ryan Livingston
Ryan Livingston on 18 Nov 2014
Edited: Ryan Livingston on 18 Nov 2014
Curious, I just tried a simple example using R2014b with GCC 4.7 and the Makefile used the pattern you showed but there was no error. Does this happen with trivial MATLAB code?
What Linux distro are you using? I tried on Debian Wheezy.
Are there any suspicious directory or file names either with spaces or special characters (e.g. $, %, &, etc) involved?
In theory, you could use something like sed to automatically make the change to the Makefile. In a MATLAB script:
!sed 's/$(LINK_FLAGS) $(OBJLIST)/$(OBJLIST) $(LINK_FLAGS)/g' -i codegen/mex/foo/foo_mex.mk
should do it if you update the path to the Makefile as appropriate. It would be better to understand the issue though.
Athanasios Karapatis
Athanasios Karapatis on 19 Nov 2014
Edited: Athanasios Karapatis on 19 Nov 2014
Hi @Ryan Livingston,
Thank you for your reply. I am running R2014b on Ubuntu 14.04.1 LTS
:~$ /usr/bin/gcc -v
returns: gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-12ubuntu1)
The code is pretty trivial and mainly to see how MATLAB coder works:
function [resultADD] = WaveletFIR(Input)
resultADD = sqrt(Input.^2 + Input.^2);
end
The directory names don't have anything weird in them. Not even spaces. I run the test in a new folder and the problem still occurs.
I can write a script to change the makefile but the Build command in MATLAB coder won't stop giving an error.
Some more detail. When I run the wrapper via command line ( without changing the order of the LINK_FLAGS) I get:
/usr/bin/gcc -Wl,--version-script,WaveletFIR_mex_mex.map -pthread -Wl,--no-undefined -Wl,-rpath-link,/home/thanasis/Programs/matlab/bin/glnxa64 -shared -L/home/thanasis/Programs/matlab/bin/glnxa64 -lmx -lmex -lmat -lm -lstdc++ -o WaveletFIR_mex.mexa64 WaveletFIR_data.o WaveletFIR_initialize.o WaveletFIR_terminate.o WaveletFIR.o _coder_WaveletFIR_api.o _coder_WaveletFIR_mex.o _coder_WaveletFIR_info.o -lemlrt -lcovrt -lut -lmwmathutil -lmwblas
WaveletFIR.o: In function `WaveletFIR':
WaveletFIR.c:(.text+0x181): undefined reference to `mxGetNaN'
WaveletFIR.c:(.text+0x18f): undefined reference to `mxGetNaN'
WaveletFIR.c:(.text+0x1a6): undefined reference to `mxGetInf'
WaveletFIR.c:(.text+0x1d6): undefined reference to `mxGetInf'
WaveletFIR.c:(.text+0x1e6): undefined reference to `mxGetInf'
_coder_WaveletFIR_mex.o: In function `mexFunction':
_coder_WaveletFIR_mex.c:(.text+0x3d): undefined reference to `mexAtExit'
_coder_WaveletFIR_mex.c:(.text+0x73): undefined reference to `mxCalloc'
_coder_WaveletFIR_mex.c:(.text+0x15a): undefined reference to `mxFree'
_coder_WaveletFIR_info.o: In function `b_emlrt_marshallOut':
_coder_WaveletFIR_info.c:(.text+0x62): undefined reference to `mxGetData'
_coder_WaveletFIR_info.o: In function `emlrtMexFcnProperties':
_coder_WaveletFIR_info.c:(.text+0x3702): undefined reference to `mxCreateStructMatrix_700'
_coder_WaveletFIR_info.c:(.text+0x371d): undefined reference to `mxCreateStructMatrix_700'
_coder_WaveletFIR_info.c:(.text+0x372f): undefined reference to `mxCreateLogicalMatrix_700'
_coder_WaveletFIR_info.c:(.text+0x373e): undefined reference to `mxCreateString'
_coder_WaveletFIR_info.c:(.text+0x3753): undefined reference to `mxSetFieldByNumber_700'
_coder_WaveletFIR_info.c:(.text+0x3760): undefined reference to `mxCreateDoubleScalar'
_coder_WaveletFIR_info.c:(.text+0x3775): undefined reference to `mxSetFieldByNumber_700'
_coder_WaveletFIR_info.c:(.text+0x3782): undefined reference to `mxCreateDoubleScalar'
_coder_WaveletFIR_info.c:(.text+0x3797): undefined reference to `mxSetFieldByNumber_700'
_coder_WaveletFIR_info.c:(.text+0x37ac): undefined reference to `mxSetFieldByNumber_700'
_coder_WaveletFIR_info.c:(.text+0x37b8): undefined reference to `mxCreateString'
_coder_WaveletFIR_info.c:(.text+0x37cd): undefined reference to `mxSetFieldByNumber_700'
_coder_WaveletFIR_info.c:(.text+0x37e7): undefined reference to `mxSetFieldByNumber_700'
_coder_WaveletFIR_info.c:(.text+0x37fc): undefined reference to `mxSetFieldByNumber_700'
collect2: error: ld returned 1 exit status
But when I change the order of the LINK_FLAGS it compiles file:
/usr/bin/gcc -Wl,--version-script,WaveletFIR_mex_mex.map WaveletFIR_data.o WaveletFIR_initialize.o WaveletFIR_terminate.o WaveletFIR.o _coder_WaveletFIR_api.o _coder_WaveletFIR_mex.o _coder_WaveletFIR_info.o -pthread -Wl,--no-undefined -Wl,-rpath-link,/home/thanasis/Programs/matlab/bin/glnxa64 -shared -L/home/thanasis/Programs/matlab/bin/glnxa64 -lmx -lmex -lmat -lm -lstdc++ -o WaveletFIR_mex.mexa64 -lemlrt -lcovrt -lut -lmwmathutil -lmwblas
Also the path of the makefile is:
/home/thanasis/projects/testMex/codegen/mex/WaveletFIR

Sign in to comment.

Accepted Answer

Ryan Livingston
Ryan Livingston on 19 Nov 2014
Edited: Ryan Livingston on 24 Nov 2014
EDIT: If you hit this issue and have a MathWorks account you can now refer to:
for a patch rather than needing to manually apply it. Otherwise, see below for the manual steps.
Thanks for the clarification, Athanasios. The GCC shipped with Ubuntu is slightly modified from the "vanilla" GCC. One consequence of this is that the Ubuntu GCC is slightly more sensitive to argument order.
The Makefile you are seeing is from a template inside the MATLAB installation. If you change this template in the same way you changed the generated Makefile, then the changes should always be applied.
The file to edit is:
MATLABROOT/toolbox/coder/coder/mex/c/mex_unix.mk
where MATLABROOT is the output of the command matlabroot in MATLAB.
To be explicit, you can make the change you suggested to swap $(LINK_FLAGS) and $(OBJLIST) so:
$(TARGET): $(OBJLIST) $(MAKEFILE)
$(LD) $(EXPORTOPT) $(LINK_FLAGS) $(OBJLIST) $(SYS_LIBS)
becomes:
$(TARGET): $(OBJLIST) $(MAKEFILE)
$(LD) $(EXPORTOPT) $(OBJLIST) $(LINK_FLAGS) $(SYS_LIBS)
  3 Comments
Ryan Livingston
Ryan Livingston on 19 Nov 2014
Great to hear! We'll look into resolving this permanently for future releases of MATLAB Coder.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!