现在的位置: 首页 > 综合 > 正文

c++ runtime symbol lookup error

2013年08月19日 ⁄ 综合 ⁄ 共 2194字 ⁄ 字号 评论关闭

今天编程遇到问题,我对一个类不加namespace的时候是对的,运行完全正确。可是我加上namespace的时候,编译都对,但是就是运行时出错:

../bin/test_gbr: symbol lookup error: ../bin/test_gbr: undefined symbol: _ZN3gbr9ksdensityC1EPKdiiSsSs。

我以为是namespace的名字有冲突,就改了个名字,发现还是一样错。然后测试其它同样有namespace的类,没有问题,单单这个类有问题。

找同学帮忙,google了一下,“namespace class symbol lookup error c++",第一条就是答案:http://gdwarner.blogspot.com/2009/03/c-runtime-symbol-lookup-error.html.

转载全文如下:

 

c++ runtime "symbol lookup error"

I was working on a c++ project. Everything linked and compiled fine. Upon running the executable, I got the following error:

./TestCppProgram: symbol lookup error: ./TestCppProgram: undefined symbol: _ZN12CppProgramC1Ev

I searched the internet. Two of the interesting links I found were the following:

For me it ended up being a bad LD_LIBRARY_PATH. The path I intended the executable to find it's needed shared library was in the LD_LIBRARY_PATH, it just wasn't before a different path which had an older version of the needed shared library. (This happened to me when I updated by bashrc with a library path and just re-sourced it).

Some cool commands in the debugging process:

  • ldd TestCppProgram (Shows you where your program is getting it's libraries from. An early-on careful inspection of this would've quickly let me to my problem!)
  • ldd -d -r TestCppProgram (Shows you any undefined symbols. There shouldn't be any undefined symbols for an executable, but there will be for a shared lib if it depends on another shared lib. Somebody please correct me if I'm wrong)
  • nm TestCppProgram | c++filt (displays unmangled symbol information)
  • nm TestCppProgram (Displays mangled symbol information. Ie: You should be able to find stuff like ZN12CppProgramC1Ev in here. In my problem above, I found which line number the undefined symbol in question was on, and then looked it up in the unmangled version to see what function it was trying to resolve. It let me know, but it didn't really help me find out what my problem was.)
  • readelf -d TestCppProgram (Shows library dependencies. similar to ldd.)

发现我的问题与作者是一样的。我的gbr需要编译成shared library,然后我会安装到$USR_LIB这个我自己的目录下。但是每次我重新编译gbr时,新生成的libgbr.so在本地目录,并没有自动安装到$USR_LIB目录下,因而$USR_LIB里的libgbr.so一直是旧的,就是没有用namespace的情况。在运行可执行文件时,系统自动找到$USR_LIB里的libgbr.so并未更新,所以每次都会出错。

问题找到了,解决办法很简单,更新$USR_LIB里的libgbr.so为最新编译的。然后测试,运行正确。

抱歉!评论已关闭.