1

I have a matlab mex extension which I want to compile using a Makefile. The Linker does not seem to find the mex libraries. Here is the Makefile:

MEXSUFFIX  = mexa64
IX  = mexa64
MATLABHOME = /usr/local/MATLAB/R2013b
MEX        = g++
MEXCXX     = echo
CXX        = g++

CFLAGS = -fPIC -pthread -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fno-omit-frame-pointer -pthread -O3 -DNDEBUG

LIBS      = -lm
INCLUDE   = -I$(MATLABHOME)/extern/include -Icommon
#g++
MEXFLAGS =  -shared -Wl,--no-undefined -Wl,-rpath-link,$(MATLABHOME)/bin/glnxa64 -L$(MATLABHOME)/bin/glnxa64 -lmx -lmex -lmat -lm

PROJECTS = residualfm
MEXDIR = ..

all: $(PROJECTS)

residualfm: residualfm/functions.o
    $(MEX) $(MEXFLAGS) $(LIBS) -o $(MEXDIR)/$@.$(MEXSUFFIX) $^

.cpp.o:
    $(CXX) -c -o $@ $< $(CFLAGS) $(INCLUDE)

clean:
    rm -f common/*.o
    for proj in $(PROJECTS); do \
        rm -f $$proj/*.o; \
        rm -f $(MEXDIR)/$$proj.$(MEXSUFFIX); \
    done

and here my filestructure:

$ ls *
Makefile  mex.kdev4  sfr_mex.sln  sfr_mex.suo  sfr_mex.v11.suo

residualfm:
DllMain.cpp             residualfm_variant.def           stdafx.h
functions.cpp           residualfm_variant.vcxproj       timing.cpp
functions.o             residualfm_variant.vcxproj.user
maxflow_classic_boykov  stdafx.cpp

Here is the error:

$ make
g++ -shared -Wl,--no-undefined -Wl,-rpath-link,/usr/local/MATLAB/R2013b/bin/glnxa64 -L/usr/local/MATLAB/R2013b/bin/glnxa64 -lmx -lmex -lmat -lm -lm -o ../residualfm.mexa64 residualfm/functions.o
residualfm/functions.o: In function `mexFunction':
functions.cpp:(.text+0x27b): undefined reference to `mexErrMsgTxt'
functions.cpp:(.text+0x2a0): undefined reference to `mexErrMsgTxt'
functions.cpp:(.text+0x2c7): undefined reference to `mxGetClassID'
functions.cpp:(.text+0x2e5): undefined reference to `mexErrMsgTxt'
functions.cpp:(.text+0x301): undefined reference to `mxGetNumberOfDimensions'
functions.cpp:(.text+0x320): undefined reference to `mexErrMsgTxt'
functions.cpp:(.text+0x341): undefined reference to `mxGetData'
functions.cpp:(.text+0x350): undefined reference to `mxGetDimensions'
functions.cpp:(.text+0x371): undefined reference to `mxGetNumberOfElements'
functions.cpp:(.text+0x390): undefined reference to `mexErrMsgTxt'
functions.cpp:(.text+0x3aa): undefined reference to `mxGetScalar'
functions.cpp:(.text+0x3c5): undefined reference to `mxGetNumberOfElements'
functions.cpp:(.text+0x3e4): undefined reference to `mexErrMsgTxt'
functions.cpp:(.text+0x3fe): undefined reference to `mxGetScalar'
functions.cpp:(.text+0x4f5): undefined reference to `mexErrMsgTxt'
functions.cpp:(.text+0xdd3): undefined reference to `mxCreateNumericArray'
functions.cpp:(.text+0xdde): undefined reference to `mxGetData'
functions.cpp:(.text+0xefe): undefined reference to `mxGetScalar'
functions.cpp:(.text+0xf9d): undefined reference to `mxCreateNumericArray'
functions.cpp:(.text+0xfa8): undefined reference to `mxGetData'
functions.cpp:(.text+0x11dc): undefined reference to `mxCreateSparse'
functions.cpp:(.text+0x11e7): undefined reference to `mxGetJc'
functions.cpp:(.text+0x11f2): undefined reference to `mxGetIr'
functions.cpp:(.text+0x11fd): undefined reference to `mxGetPr'
residualfm/functions.o: In function `visit_backwards(Node*, Node*, double*, int)':
functions.cpp:(.text+0x232): undefined reference to `mexErrMsgTxt'
collect2: error: ld returned 1 exit status
make: *** [residualfm] Error 1

Compiling from matlab using mex functions.cpp works fine

edit: Thanks for the hints! Solved (but not understood):

Changing the order in residualfm: residualfm/functions.o from:

$(MEX) $(MEXFLAGS) $(LIBS) -o $(MEXDIR)/$@.$(MEXSUFFIX) $^

to

$(MEX) -o $(MEXDIR)/$@.$(MEXSUFFIX) $^ $() $(MEXFLAGS) $(LIBS) 

solved the problem. Can someone explain this?

Elrond1337
  • 424
  • 1
  • 5
  • 16
  • in matlab try to compile with `mex -v -largeArrayDims -O` and look what are the include path and other flags you might need to add to `MEXFLAGS`. – Shai Jan 15 '14 at 09:54
  • BTW, do you have `#include "mex.h"` in your cpp file? – Shai Jan 15 '14 at 09:54
  • The errors suggest you're not properly linking to libmx/libmex. You could try `g++ -v` in order check in the log whether they are found or not. – sebastian Jan 15 '14 at 11:47
  • @Elrond1337: Linked libraries must come after the file being compiled in the command-line: http://stackoverflow.com/q/45135/97160, http://stackoverflow.com/q/6247926/97160, http://stackoverflow.com/q/7826448/97160, http://stackoverflow.com/q/14623415/97160, and many others.. – Amro Jan 16 '14 at 15:15
  • @Elrond1337 I am trying to convert C-code to mex code using mex-file. I am wondering if you could have a look at my mex file: [My mex-makefile on stackoverflow](http://stackoverflow.com/questions/28484824/makefile-converting-c-code-to-mex-code) – Garima Singh Feb 12 '15 at 18:38
  • @GarimaSingh I'm sorry but I haven't worked with mex nor matlab for the last 12 months, so I'm afraid I can't help. – Elrond1337 May 15 '15 at 09:31

1 Answers1

1

(Solved in the comments and edits. See Question with no answers, but issue solved in the comments (or extended in chat) )

@Amro wrote:

Linked libraries must come after the file being compiled in the command-line

The OP wrote:

Changing the order in residualfm: residualfm/functions.o from:

$(MEX) $(MEXFLAGS) $(LIBS) -o $(MEXDIR)/$@.$(MEXSUFFIX) $^ to

$(MEX) -o $(MEXDIR)/$@.$(MEXSUFFIX) $^ $() $(MEXFLAGS) $(LIBS)

solved the problem.

Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129