c++ runtime symbol lookup error

來源:互聯網
上載者:User
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為最新編譯的。然後測試,運行正確。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.