解决In function `mwException::mwException()': undefined reference to `mclcppCreateError'

在进行这个实验中,通过c++调用matlab中自己编写的函数(是通过将该函数在matlab中mcc编译成.so,然后c++中调用这个.so的方式),报错记录总结。

实验环境:Ubuntu 12.04.5 64bit, gcc 4.6, matlab 2012a

<span style="font-size:12px;">$ g++ -o main -I. -L. -L/opt/MATLAB/R2012a/runtime/glnxa64 main.cpp -lmyFunc

/usr/bin/ld: warning: libmwmclmcrrt.so.7.17, needed by ./libmyFunc.so, not found (try using -rpath or -rpath-link)
/tmp/cci4LSpW.o: In function `main':
main.cpp:(.text+0x18b): undefined reference to `mclTerminateApplication_proxy'
/tmp/cci4LSpW.o: In function `mwException::mwException()':
main.cpp:(.text._ZN11mwExceptionC2Ev[_ZN11mwExceptionC5Ev]+0x41): undefined reference to `mclcppCreateError'
/tmp/cci4LSpW.o: In function `mwException::mwException(char const*)':
main.cpp:(.text._ZN11mwExceptionC2EPKc[_ZN11mwExceptionC5EPKc]+0x55): undefined reference to `mclcppCreateError'
/tmp/cci4LSpW.o: In function `mwException::mwException(error_info*, bool)':
main.cpp:(.text._ZN11mwExceptionC2EP10error_infob[_ZN11mwExceptionC5EP10error_infob]+0x5e): undefined reference to `ref_count_obj_addref'
main.cpp:(.text._ZN11mwExceptionC2EP10error_infob[_ZN11mwExceptionC5EP10error_infob]+0x75): undefined reference to `mclcppCreateError'
/tmp/cci4LSpW.o: In function `mwException::~mwException()':
main.cpp:(.text._ZN11mwExceptionD2Ev[_ZN11mwExceptionD5Ev]+0x33): undefined reference to `ref_count_obj_release'
/tmp/cci4LSpW.o: In function `mwException::what() const':
main.cpp:(.text._ZNK11mwException4whatEv[mwException::what() const]+0x25): undefined reference to `error_info_get_message'
/tmp/cci4LSpW.o: In function `mwException::raise_error()':
main.cpp:(.text._ZN11mwException11raise_errorEv[mwException::raise_error()]+0x1b): undefined reference to `mclcppGetLastError'
/tmp/cci4LSpW.o: In function `mwArray::mwArray(unsigned long, unsigned long, mxClassID, mxComplexity)':
main.cpp:(.text._ZN7mwArrayC2Emm9mxClassID12mxComplexity[_ZN7mwArrayC5Emm9mxClassID12mxComplexity]+0x4f): undefined reference to `mclGetMatrix'
/tmp/cci4LSpW.o: In function `mwArray::~mwArray()':
main.cpp:(.text._ZN7mwArrayD2Ev[_ZN7mwArrayD5Ev]+0x23): undefined reference to `ref_count_obj_release'
/tmp/cci4LSpW.o: In function `mwArray::mwArray(array_ref*, bool)':
main.cpp:(.text._ZN7mwArrayC2EP9array_refb[_ZN7mwArrayC5EP9array_refb]+0x7d): undefined reference to `ref_count_obj_addref'
/tmp/cci4LSpW.o: In function `mwArray::GetPromoted(unsigned long, ...)':
main.cpp:(.text._ZN7mwArray11GetPromotedEmz[mwArray::GetPromoted(unsigned long, ...)]+0xa6): undefined reference to `array_ref_getV_int'
/tmp/cci4LSpW.o: In function `mwArray::SetData(double*, unsigned long)':
main.cpp:(.text._ZN7mwArray7SetDataEPdm[mwArray::SetData(double*, unsigned long)]+0x2b): undefined reference to `array_ref_set_numeric_mxDouble'
/tmp/cci4LSpW.o: In function `mwArray::operator double() const':
main.cpp:(.text._ZNK7mwArraycvdEv[mwArray::operator double() const]+0x24): undefined reference to `array_ref_get_numeric_mxDouble'
./libmyFunc.so: undefined reference to `mclFreeStackTrace_proxy'
./libmyFunc.so: undefined reference to `ref_count_obj_release_proxy'
./libmyFunc.so: undefined reference to `array_buffer_set_proxy'
./libmyFunc.so: undefined reference to `mclWrite_proxy'
./libmyFunc.so: undefined reference to `array_buffer_get_proxy'
./libmyFunc.so: undefined reference to `array_buffer_add_proxy'
./libmyFunc.so: undefined reference to `mclGetEmbeddedCtfStream_proxy'
./libmyFunc.so: undefined reference to `mclcppCreateError_proxy'
./libmyFunc.so: undefined reference to `mclcppGetArrayBuffer_proxy'
./libmyFunc.so: undefined reference to `mclDestroyStream_proxy'
./libmyFunc.so: undefined reference to `mclcppGetLastError_proxy'
./libmyFunc.so: undefined reference to `array_buffer_to_cell_proxy'
./libmyFunc.so: undefined reference to `mclGetStackTrace_proxy'
./libmyFunc.so: undefined reference to `array_ref_number_of_elements_proxy'
./libmyFunc.so: undefined reference to `mclFeval_proxy'
./libmyFunc.so: undefined reference to `mclmcrInitialize_proxy'
./libmyFunc.so: undefined reference to `mclTerminateInstance_proxy'
./libmyFunc.so: undefined reference to `ref_count_obj_addref_proxy'
./libmyFunc.so: undefined reference to `error_info_get_message_proxy'
./libmyFunc.so: undefined reference to `mclcppFeval_proxy'
./libmyFunc.so: undefined reference to `mclInitializeComponentInstanceEmbedded_proxy'
./libmyFunc.so: undefined reference to `array_ref_classID_proxy'
./libmyFunc.so: undefined reference to `array_ref_getV_int_proxy'
collect2: ld returned 1 exit status</span>

上述错误中后面一半是因为缺少库libmwmclmcrrt.so解决方法就是编译时加一个选项-lmwmclmcrrt,如下:

<span style="font-size:12px;">$ g++ -o main -I. -L. -L/opt/MATLAB/R2012a/runtime/glnxa64 main.cpp -lmyFunc -lmwmclmcrrt

/tmp/cc5szYKi.o: In function `mwException::mwException()':
main.cpp:(.text._ZN11mwExceptionC2Ev[_ZN11mwExceptionC5Ev]+0x41): undefined reference to `mclcppCreateError'
/tmp/cc5szYKi.o: In function `mwException::mwException(char const*)':
main.cpp:(.text._ZN11mwExceptionC2EPKc[_ZN11mwExceptionC5EPKc]+0x55): undefined reference to `mclcppCreateError'
/tmp/cc5szYKi.o: In function `mwException::mwException(error_info*, bool)':
main.cpp:(.text._ZN11mwExceptionC2EP10error_infob[_ZN11mwExceptionC5EP10error_infob]+0x5e): undefined reference to `ref_count_obj_addref'
main.cpp:(.text._ZN11mwExceptionC2EP10error_infob[_ZN11mwExceptionC5EP10error_infob]+0x75): undefined reference to `mclcppCreateError'
/tmp/cc5szYKi.o: In function `mwException::~mwException()':
main.cpp:(.text._ZN11mwExceptionD2Ev[_ZN11mwExceptionD5Ev]+0x33): undefined reference to `ref_count_obj_release'
/tmp/cc5szYKi.o: In function `mwException::what() const':
main.cpp:(.text._ZNK11mwException4whatEv[mwException::what() const]+0x25): undefined reference to `error_info_get_message'
/tmp/cc5szYKi.o: In function `mwException::raise_error()':
main.cpp:(.text._ZN11mwException11raise_errorEv[mwException::raise_error()]+0x1b): undefined reference to `mclcppGetLastError'
/tmp/cc5szYKi.o: In function `mwArray::mwArray(unsigned long, unsigned long, mxClassID, mxComplexity)':
main.cpp:(.text._ZN7mwArrayC2Emm9mxClassID12mxComplexity[_ZN7mwArrayC5Emm9mxClassID12mxComplexity]+0x4f): undefined reference to `mclGetMatrix'
/tmp/cc5szYKi.o: In function `mwArray::~mwArray()':
main.cpp:(.text._ZN7mwArrayD2Ev[_ZN7mwArrayD5Ev]+0x23): undefined reference to `ref_count_obj_release'
/tmp/cc5szYKi.o: In function `mwArray::mwArray(array_ref*, bool)':
main.cpp:(.text._ZN7mwArrayC2EP9array_refb[_ZN7mwArrayC5EP9array_refb]+0x7d): undefined reference to `ref_count_obj_addref'
/tmp/cc5szYKi.o: In function `mwArray::GetPromoted(unsigned long, ...)':
main.cpp:(.text._ZN7mwArray11GetPromotedEmz[mwArray::GetPromoted(unsigned long, ...)]+0xa6): undefined reference to `array_ref_getV_int'
/tmp/cc5szYKi.o: In function `mwArray::SetData(double*, unsigned long)':
main.cpp:(.text._ZN7mwArray7SetDataEPdm[mwArray::SetData(double*, unsigned long)]+0x2b): undefined reference to `array_ref_set_numeric_mxDouble'
/tmp/cc5szYKi.o: In function `mwArray::operator double() const':
main.cpp:(.text._ZNK7mwArraycvdEv[mwArray::operator double() const]+0x24): undefined reference to `array_ref_get_numeric_mxDouble'
collect2: ld returned 1 exit status</span>

可见错误少了一半,但是还有很多错误,这些余下的错误都是因为缺少一个包:libmwmclmcr.so,这个包和前面一个包名字很相似,别混了。解决方法就是编译时加选项:-lmwmclmcr,如下(虽然这个错误消失了,但是有一个新警告,并接着很多函数找不到,如下):

<span style="font-size:12px;">$ g++ -o main -I. -L. -L/opt/MATLAB/R2012a/runtime/glnxa64 main.cpp -lmyFunc -lmwmclmcrrt -lmwmclmcr

/usr/bin/ld: warning: libXm.so.3, needed by /opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so, not found (try using -rpath or -rpath-link)
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `_XmMsgTextIn_0000'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `_XmPrimitiveHelp'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `_XmGetAudibleWarning'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `XmFontListEntryGetTag'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `XmTransferValue'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `_XmTextIn_XmTextEventBindings1'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `XmImSetFocusValues'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `_XmMsgTextF_0000'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `XmListSelectPos'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `XmFontListNextEntry'
/opt/MATLAB/R2012a/bin/glnxa64/libmwhg.so: undefined reference to `XmCreateSeparatorGadget'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `_XmConvertComplete'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `XmMenuPosition'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `_XmTextIn_XmTextEventBindings2'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `XmCreatePulldownMenu'
/opt/MATLAB/R2012a/bin/glnxa64/libmwhg.so: undefined reference to `XmTextGetLastPosition'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `XmListDeselectAllItems'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `XmStringExtent'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `XmListItemPos'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `_XmMgrTraversal'
/opt/MATLAB/R2012a/bin/glnxa64/libmwuix.so: undefined reference to `XmQmotif'
......</span>

这个警告很好解决,只需要链接一下就行,如下:

<span style="font-size:12px;">$ locate libXm
/opt/MATLAB/R2012a/sys/os/glnxa64/libXm.so.3
/opt/MATLAB/R2012a/sys/os/glnxa64/libXm.so.3.0.2
/usr/lib/x86_64-linux-gnu/libXmu.a
/usr/lib/x86_64-linux-gnu/libXmu.so
/usr/lib/x86_64-linux-gnu/libXmu.so.6
/usr/lib/x86_64-linux-gnu/libXmu.so.6.2.0
/usr/lib/x86_64-linux-gnu/libXmuu.a
/usr/lib/x86_64-linux-gnu/libXmuu.so
/usr/lib/x86_64-linux-gnu/libXmuu.so.1
/usr/lib/x86_64-linux-gnu/libXmuu.so.1.0.0

$ ln -s /opt/MATLAB/R2012a/sys/os/glnxa64/libXm.so.3 /opt/MATLAB/R2012a/bin/glnxa64

$ g++ -o main -I. -L. -L/opt/MATLAB/R2012a/runtime/glnxa64 main.cpp -lmyFunc -lmwmclmcrrt -lmwmclmcr </span>
编译成功通过!


虽然编译成功了,但是执行不了,报错:

<span style="font-size:12px;">$ ./main 
./main: error while loading shared libraries: libmwmclmcrrt.so.7.17: cannot open shared object file: No such file or directory</span>

这是因为动态链接库只是一些链接,运行时需要把matlab的动态库装一下,才能让这些链接找到那个库文件。需要安装“MCRInstaller”(安装步骤点此),才可以正常调用。

安装完以后,根据页面中提示将MCRInstaller的一长串路径添加到系统变量的LD_LIBRARY_PATH中就行。调用成功结果如下

<span style="font-size:12px;">$ ./main 
Warning: latest version of matlab app-defaults file not found.
Contact your system administrator to have this file installed
Please input 2 numbers <a b> and then press enter: 
4 4 
The sum is: 8</span>

你可能感兴趣的:(gcc,matlab)