views:

127

answers:

4

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.

+3  A: 

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

Roman D
Thank you! It's compiler mismatch.
solotim
Changed to another question: http://stackoverflow.com/questions/2371073/how-to-write-c-so-library-to-subsitute-existing-c-so-library
solotim
+1  A: 

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.

osgx
A: 

You probably just forgot to link the whole program with the C++ standard library.

Let_Me_Be
A: 

The statement in the question is just wrong. You say, 'the header file.' There is no such thing as 'the (one and only) header file.' If you mean 'the header file declaring a certain C++ class', well, that class might inherit from other classes. Or it might use exceptions. Or RTTI. In which case, by default, the .so containing the code that goes with it will contain 'hanging undefined symbols'. By default, the expectation is that the 'main' program is in C++, and it links to the C++ runtime.

It is possible to create a self-contained .so, but you have to do extra work to create it. You might need to use -Bsymbolic, or specify some -l libraries when linking it, or both. This area is not well-documented and generally required some archaeology.

bmargulies
In c programming I 'include' headers from whatever libraries when compile(not link). I mean this 'header file'. I'm still not quite sure about the meaning of "no such thing as the header file", would you please explain it more? (better in C jargon) Thanks!
solotim