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 .o
s:
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:
Use
find_program()
to have CMake locate the MSVClib.exe
tool on your system. If you runcmake
from the Visual Studio Command Prompt,find_program
can locatelib.exe
automatically, without using the optionalPATHS
argument to tell it where to look.Use CMake's
add_custom_target()
command to calllib.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.
if(MSVC_LIB_TOOL)
add_custom_target(CombineStaticLibraries
COMMAND ${MSVC_LIB_TOOL} /OUT:$<TARGET_FILE_DIR:a>/alpha_lib.lib
$<TARGET_FILE:a>
$<TARGET_FILE:b>
$<TARGET_FILE:c>
$<TARGET_FILE:d>
$<TARGET_FILE:e>
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
endif()
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
How to Keep Environment Variables When Using Sudo
How Does "Cat ≪≪ Eof" Work in Bash
Encryption/Decryption Doesn't Work Well Between Two Different Openssl Versions
How to Get the Total Cpu Usage of an Application from /Proc/Pid/Stat
What Is an Uninterruptible Process
How to Run a Program With a Different Working Directory from Current, from Linux Shell
How to Use Debug Version of Libc
How to Find Out Which Processes Are Using Swap Space in Linux
Ipc Performance: Named Pipe VS Socket
How to Print Third Column to Last Column
Is There a Way For Non-Root Processes to Bind to "Privileged" Ports on Linux
How to Cat ≪≪Eof ≫≫ a File Containing Code
How to Install Gcc 4.9.2 on Rhel 7.4
How Many Socket Connections Possible
How to Redirect Output of Systemd Service to a File