Technorati Tags: c++,rumtime,symbol lookup error
今天編程遇到問題,我對一個類不加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:
- http://osdir.com/ml/gcc.g++.general/2005-02/msg00061.html
- http://www.linuxquestions.org/questions/linux-software-2/undefined-symbol-cout-263568/
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為最新編譯的。然後測試,運行正確。