Matlab mex代码未使用Makefile编译-未定义引用
我有一个matlab mex扩展,我想用一个Makefile编译它。链接器似乎找不到mex库。以下是生成文件:Matlab mex代码未使用Makefile编译-未定义引用,matlab,makefile,mex,Matlab,Makefile,Mex,我有一个matlab mex扩展,我想用一个Makefile编译它。链接器似乎找不到mex库。以下是生成文件: 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-omi
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
以下是错误:
$ 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
使用mex函数从matlab编译。cpp
运行良好
编辑:
谢谢你的提示!
已解决(但未理解):
将residualfm:residualfm/functions.o
中的顺序更改为:
$(MEX) $(MEXFLAGS) $(LIBS) -o $(MEXDIR)/$@.$(MEXSUFFIX) $^
到
解决了这个问题。有人能解释一下吗?(在评论和编辑中解决。请参阅)
@阿姆罗写道:
链接库必须位于在命令行中编译的文件之后
OP写道:
将residualfm:residualfm/functions.o
中的顺序更改为:
$(MEX) $(MEXFLAGS) $(LIBS) -o $(MEXDIR)/$@.$(MEXSUFFIX) $^
$(MEX)$(MEXFLAGS)$(LIBS)-o$(MEXDIR)/$@.$(MEXSUFFIX)$^
到
$(MEX)-o$(MEXDIR)/$@.$(MEXSUFFIX)$^$()$(MEXFLAGS)$(LIBS)
解决了这个问题
在matlab中,尝试使用
mex-v-largearydims-O
进行编译,看看需要添加到MEXFLAGS
的include路径和其他标志是什么。顺便问一下,您的cpp文件中是否有#include“mex.h”
?错误表明您没有正确链接到libmx/libmex。您可以尝试g++-v
,以便在日志中检查是否找到它们。@Elrond1337:链接库必须位于命令行中编译的文件之后:,,以及其他许多文件。@Elrond1337我正在尝试使用mex文件将C代码转换为mex代码。我想知道您是否可以查看我的mex文件:
$(MEX) -o $(MEXDIR)/$@.$(MEXSUFFIX) $^ $() $(MEXFLAGS) $(LIBS)