This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How to control to use the function static linked to a shared library


"Hongbo Li" <hongbol@nortel.com> writes:

This question would be more appropriate for the gcc-help mailing list
rather than the gcc mailing list.

> 	I currently hit an issue that I would like to use a function
> statically linked to a shared library but my program use the same
> function from another shared library. Here is what I do:
> 
> 	1. I have toto.cxx that has one function called: toto() {cout <<
> "static toto" << endl;}
> 	2. I create an archive say toto.a has the toto.o
> 	3. I have toto1.cxx that has one functin called toto() {cout
> cout <<"shared toto"<<endl;}
> 	4. I create a shared libtoto1.so
> 	5. I have use_toto.cxx that has one function called use_toto() {
> toto();}:
> 	6. I create a shared libuse_toto.so that statically link to
> toto.a
> 	7. My main program test.cxx calling use_toto()
> 
> 	I would like to always see the output of "static toto" but the
> output depends on the order of linking toto1.so and use_toto.so
> 
> 	I will see "static toto" when I do this
> 	g++ -o test -L./ -luse_toto -ltoto1.so ./test.o
> 
> 	But I see "shared toto" if I change the order:
> 	g++ -o test -L./ -ltoto1.so -luse_toto ./test.o
> 
> 
> 	My question: do we have any way during the compilation/link to
> control the program so that toto() in toto.cxx is always used? Since I
> may not have way to control how to build the main program, is there a
> way to build libuse_toto.so so that toto() in toto.cxx is always used?

As you have discovered, the order on the link line determines when
symbols will be used.

You can force a few other possibilities by using a linker version
script or by setting the visibility attributes.  See the linker manual
for version scripts and the gcc manual for attributes.

Ian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]