Friday, March 13, 2009

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.)


trinity said...

Hey I had forgotten to write a destructor..
nm with c++filt is awesome!
Just the thing I needed
Thanks.. :)

admorgan said...

nm -C does the same as filtering through c++filt

Kevin said...

great post, you saved the day for me today!

Ben said...

This is a very nicely written post with all of the necessary detail for starting with these tools. Thanks!

Santosh said...

Really helpful article. Thanks a lot.

Unknown said...

Great tip, indeed! Thanks!

aalia lyon said...

Nice blog ,,, This blog help you to find your DLL file is corrupt or missing .please go through this link.
How To Remove Rundll Error
Thank you
Aalia lyon

alina mark said...

Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man,Keep it up. Bin lookup

Dilip Tiwari said...

Really good abstract from both the websites

Maxim Galushka said...

Thanks for these suggestions, you saved me ton of time.

GK said...

Hey really useful stuff, you saved my time. I was compiling against an older version of library and the target has a different version of the same library.