views:

696

answers:

2

the title of this question is an exact dupe, but the answers in that question don't help me.

I have a bunch of object files packed in a static library:

% g++ -std=c++98 -fpic -g -O1 -c -o foo.o foo.cpp
% g++ -std=c++98 -fpic -g -O1 -c -o bar.o bar.cpp
% ar -rc libsome.a foo.o bar.o

I'd like to generate libsome.so from libsome.a instead of the object files, but the library is really barebones:

% g++ -std=c++98 -fpic -g -O1 -shared -o libsome.so libsome.a
% nm -DC libsome.so
0000xxxx A _DYNAMIC
0000xxxx A _GLOBAL_OFFSET_TABLE_
         w _Jv_RegisterClasses
0000xxxx A __bss_start
         w __cxa_finalize
0000xxxx A _edata
0000xxxx A _end
0000xxxx T _fini
0000xxxx T _init

the static library is ok, or at least I'm perfectly able to link it to an executable and have it run the contained functionality. also, everything is fine if I create libsome.so from foo.o and bar.o.

+1  A: 

Your files are not being pulled in from the .a file because nothing is referencing them. Why are you making a .a file first? You'd have better luck making a .o file by linking them with the -r option.

Richard Pennington
when I build libsome.so from foo.o and bar.o, nothing is referencing the symbols either, and yet they end up in the library. and since the `.a` file is a just those two exact `.o` files slapped together, there *should* be a (simple) way to use it in place of the contained object files, no?
just somebody
Because object files are pulled out of a .a file only if a symbol in them is referenced. A .o file is always linked in, referenced or not.
Richard Pennington
ok, ignoring the specifics of my situation: is there or is there not a way to convert a static library to a shared one *as if* the `.o` files were used? (discounting `ar -x`)
just somebody
You could add -u <symbol> options to the linker command line for each .o file, but that sounds painful. I think ar -x might be the best way to go.
Richard Pennington
btw, +1 for the mention of `-r`, I'll read up on partial linking, and probably be back with a question or two. ;)
just somebody
+6  A: 

Assuming you're using the GNU linker, you need to specify the --whole-archive option so that you'll get all the contents of the static archive. Since that's an linker option, you'll need -Wl to tell gcc to pass it through to the linker:

g++ -std=c++98 -fpic -g -O1 -shared -o libsome.so -Wl,--whole-archive libsome.a

If you were doing something more complicated where you want all of library some but only the part of library support needed by libsome, you would want to turn off whole archive after you've used it on libsome:

... -Wl,--whole-archive libsome.a -Wl,--no-whole-archive libsupport.a

If you're not using the GNU linker, you'll need to see if your linker supports it and what it's called. On the Sun linker, it's called -z allextract and -z defaultextract.

R Samuel Klatchko
+1 There you go. I tried "man ld" and missed that.
Richard Pennington
thanks, this is it! i've searched the ld manpage up and down, but somehow missed this option.
just somebody