I have a small piece of code that depends on many static libraries (a_1-a_n). I'd like to package up that code in a static library and make it available to other people.
My static library, lets call it X, compiles fine.
I've created a simple sample program that uses a function from X, but when I try to link it to X, I get many errors about missing symbols from libraries a_1 - a_n.
Is there a way that I can create a new static library, Y that contains X and all the functionality needed by X (selected bits from a_1 - a_n), so that I can distribute just Y for people to link their programs to?
UPDATE:
I've looked at just dumping everything with ar and making one mega-lib, however, that ends up including a lot of symbols that are not needed (all the .o files are about 700 MB, however, a statically linked executable is 7 MB). Is there a nice way to include only what is actually needed?
This looks closely related to How to combine several C/C++ libraries into one?.
Note before you read the rest: The shell script shown here is certainly not safe to use and well tested. Use at your own risk!
I wrote a bash script to accomplish that task. Suppose your library is lib1 and the one you need to include some symbols from is lib2. The script now runs in a loop, where it first checks which undefined symbols from lib1 can be found in lib2. It then extracts the corresponding object files from lib2 with
ar
, renames them a bit, and puts them into lib1. Now there may be more missing symbols, because the stuff you included from lib2 needs other stuff from lib2, which we haven't included yet, so the loop needs to run again. If after some passes of the loop there are no changes anymore, i.e. no object files from lib2 added to lib1, the loop can stop.Note, that the included symbols are still reported as undefined by
nm
, so I'm keeping track of the object files, that were added to lib1, themselves, in order to determine whether the loop can be stopped.I named that script
libcomp
, so you can call it then e.g. withwhere libwhatever is where you want to include symbols from. However, I think it's safest to copy everything into a separate directory first. I wouldn't trust my script so much (however, it worked for me; I could include libgsl.a into my numerics library with that and leave out that -lgsl compiler switch).
Static libraries do not link with other static libraries. The only way to do this is to use your librarian/archiver tool (for example ar on Linux) to create a single new static library by concatenating the multiple libraries.
Edit: In response to your update, the only way I know to select only the symbols that are required is to manually create the library from the subset of the .o files that contain them. This is difficult, time consuming and error prone. I'm not aware of any tools to help do this (not to say they don't exist), but it would make quite an interesting project to produce one.
On Linux or MingW, with GNU toolchain:
Of if you do not delete
liba.a
andlibb.a
, you can make a "thin archive":On Windows, with MSVC toolchain:
If you are using Visual Studio then yes, you can do this.
The library builder tool that comes with Visual Studio allows you to join libraries together on the command line. I don't know of any way to do this in the visual editor though.
A static library is just an archive of
.o
object files. Extract them withar
(assuming Unix) and pack them back into one big library.Alternatively to
Link Library Dependencies
in project properties there is another way to link libraries in Visual Studio.Add Existing Item...
).Item Type
isLibrary
This will include the other libraries in X as if you ran