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

14 comments:

trinity said...

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

Unknown said...

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

Kevin said...

great post, you saved the day for me today!

Unknown 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!

Unknown 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

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

Unknown said...

Wow that tremendously helped me out. I had to work with the mongo cxx driver and it turns out the executable was built referencing a mongo library that was built with boost. The executable wouldn't run on another computer with mongo library built without boost.

That being said, I'll walk your dogs, get your dry cleaning for a week if you'd like me to. THANKS SO MUCH!!

moshrabbi121 said...

I’m impressed, I need to say. Really hardly ever do I encounter a weblog that’s each educative and entertaining, and let me tell you, you will have hit the nail on the head. Your concept is excellent; the difficulty is one thing that not sufficient people are speaking intelligently about. I am very completely happy that I stumbled throughout this in my search for one thing referring to this. online casino

Rafiya said...
This comment has been removed by the author.
Rafiya said...
This comment has been removed by the author.

Pyjamas