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

在Ubuntu 12.04.5 64bit环境下,使用gcc 4.6和matlab 2012a时,通过C++调用MATLAB编译的.so文件遇到`undefined reference to `mclcppCreateError'`错误。解决方法是进行额外的链接操作。完成链接后,虽然编译成功,但执行时还需将MCRInstaller路径添加到LD_LIBRARY_PATH环境变量中,以确保正确执行并调用成功。

在进行这个实验中,通过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 `mw
/tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'initscr' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'cbreak' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'noecho' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'stdscr' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'keypad' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'curs_set' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'stdscr' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'wtimeout' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'endwin' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'clear' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'stdscr' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'wmove' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'stdscr' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'waddch' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'wmove' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'waddch' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'wmove' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'waddch' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'wmove' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'waddch' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'refresh' /tmp/ccURoooa.o:test.cpp:function Game::input(): error: undefined reference to 'wgetch' collect2: error: ld returned 1 exit status run.sh: line 2: ./main: No such file or directory
04-04
<think>嗯,用户遇到了链接错误,提示很多未定义的引用,比如initscr、cbreak这些函数。首先,我得确定这些函数属于哪个库。看起来这些函数都是ncurses库里的,所以问题可能出在编译时没有链接这个库。 用户可能是在编译的时候没有加上-lncurses选项。比如,如果使用g++编译,正确的命令应该是g++ test.cpp -o main -lncurses。如果漏掉了这个选项,链接器就找不到这些函数的实现,导致undefined reference错误。 另外,检查一下用户是否在代码中正确包含了头文件,比如#include <ncurses.h>。如果头文件没包含,编译器可能不会报错,但链接时会找不到函数定义。不过根据错误信息,这些函数已经被引用,所以可能头文件是存在的,只是链接问题。 还有可能用户安装的ncurses库版本不对或者没有安装。比如在Ubuntu系统上需要安装libncurses-dev包。如果库没安装,自然找不到相关的链接库。 总结下来,解决方法应该是确保编译命令中正确链接了ncurses库,并且库已经正确安装。用户需要检查编译命令,并确认库的存在。</think>这些链接错误表明在编译时未能正确链接ncurses库。以下是解决方案和原因分析: 1. **缺少ncurses库链接**: 这些函数(initscr、cbreak等)属于ncurses图形库,编译时需要显式链接该库。 **解决方案**: 在编译命令中添加`-lncurses`选项: ```bash g++ test.cpp -o main -lncurses ``` 2. **未安装ncurses开发包**: 系统可能缺少ncurses的开发文件。 **解决方案**: - Ubuntu/Debian: ```bash sudo apt-get install libncurses-dev ``` - CentOS/RHEL: ```bash sudo yum install ncurses-devel ``` 3. **代码示例验证**: ```cpp #include <ncurses.h> int main() { initscr(); // 初始化ncurses cbreak(); // 禁用行缓冲 noecho(); // 关闭输入回显 keypad(stdscr, TRUE);// 启用功能键 curs_set(0); // 隐藏光标 printw("Hello Ncurses!"); refresh(); getch(); // 等待输入 endwin(); // 清理终端 return 0; } ```
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值