How to Merge Two "Ar" Static Libraries into One

How to merge two ar static libraries into one?

You can extract the object from both the .a files and create your .a file using the extracted .os:

ar -x libabc.a
ar -x libxyz.a
ar -c libaz.a *.o

Combining static libraries

1/ Extract ALL of the object files from each library (using ar) and try to compile your code without the libraries or any of the object files. You'll probably get an absolute bucket-load of undefined symbols. If you get no undefined symbols, go to step 5.

2/ Grab the first one and find out which object file satisfies that symbol (using nm).

3/ Write down that object file then compile your code, including the new object file. You'll get a new list of undefined symbols or, if there's none, go to step 5.

4/ Go to step 2.

5/ Combine all the object files in your list (if any) into a single library (again with ar).

Bang! There you have it. Try to link your code without any of the objects but with the new library.

This whole thing could be relatively easily automated with a shell script.

How to bundle multiple static libraries into single library in CMake for windows

If you are using Visual Studio, you can take advantage of the Microsoft Library Manager (LIB.exe) to combine your static libraries into one. Your CMake could follow these steps:

  1. Use find_program() to have CMake locate the MSVC lib.exe tool on your system. If you run cmake from the Visual Studio Command Prompt, find_program can locate lib.exe automatically, without using the optional PATHS argument to tell it where to look.

  2. Use CMake's add_custom_target() command to call lib.exe using the syntax for merging libraries:

    lib.exe /OUT:alpha_lib.lib  a.lib b.lib c.lib d.lib e.lib

    You can use target-dependent generator expressions in the custom target command to have CMake resolve the locations of your built libraries. The custom target will create a Project in your Visual Studio solution that can be run separately to merge all of the built static libraries into one library.

Your CMake could look something like this:

# Create the static libraries (a, b, c, d, and e)
add_library(a STATIC ${a_SOURCES})
add_library(e STATIC ${e_SOURCES})

# Tell CMake to locate the lib.exe tool.
find_program(MSVC_LIB_TOOL lib.exe)

# If the tool was found, create the custom target.

Combine static libraries on Apple

you can use libtool to do it

libtool -static -o new.a old1.a old2.a

How to merge two windows vc static library into one

LIB.EXE /OUT:c.lib a.lib b.lib

LIB.EXE is available in < VC6_InstalledFolder >/VC98/BIN. And this LIB.EXE is available in all versions of visual studio.

Related Topics

Leave a reply