Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does program need additional symbols from .so shared library except those declared in header file?

Tags:

In C programming, I thought that a object file can be successfully linked with a .so file as long as the .so file offers all symbols which have been declared in the header file.

Suppose I have foo.c, bar.h and two libraries libbar.so.1 and libbar.so.2. The implementation of libbar.so.1 and libbar.so.2 is totally different, but I think it's OK as long as they both offers functions declared in bar.h.

I linked foo.o with libbar.so.1 and produced an executable: foo.bin. This executable worked when libbar.so.1 is in LD_LIBRARY_PATH.(of course a symbolic link is made as libbar.so) However, when I change the symbolic link to libbar.so.2, foo.bin could not run and complainted this:

 undefined symbol: _ZSt4cerr

libbar.so.1 is a c++ built library, while libbar.so.2 is a c built library. I don't understand why foo.bin needs those c++ related symbols only meaningful in libbar.so.1 itself, since foo.bin is built upon pure c code foo.c.

like image 752
solotim Avatar asked Mar 03 '10 08:03

solotim


2 Answers

_ZSt4cerr is obviously a mangled C++ name. You may need to check if you are using the right compiler (gcc/g++, i know it sounds stupid, but i happened to run into such confusion ;) ), and if there are any macros in the bar.h file that could have referenced cerr.

like image 132
Roman Dmitrienko Avatar answered Sep 28 '22 06:09

Roman Dmitrienko


You must demangle c++ name before searching. For gcc there is a c++filt utility:

$ c++filt
_ZSt4cerr
std::cerr

It is just standard error file stream.

like image 28
osgx Avatar answered Sep 28 '22 05:09

osgx