Linux Snippet: include a static library inside another static library

The linux static library format .a is basically only an archive of compiled object files. If you are developing a linux static library that use external functions coming from another static library you need to release both libraries (your library just developed and the second library you get functions from) for allow the final executable to compile correctly.



However a more "elegant" and easy solution would be to "incorporate" the secondary library inside your library as result to distribute one single library containing all required object files for compile. This can be done by extracting all the object files from the secondary library and include inside your library. In our example we'll call MyLibrary.a the library we just developed and SecondaryLibrary.a the library we require function from. All the required command will be included inside a shell script we can call addlib.sh as follow:

#!/bin/sh

mkdir tmp
cd tmp

ar x ../SecondaryLibrary.a
ar r ../MyLibrary.a *

rm *
cd ..
rm -r tmp

As you can see the script create a temporary folder called tmp where extract all the object files from inside SecondaryLibrary.a and include all into the MyLibrary.a just compiled. At the end remove all object files and remove the temporary folder. This will allow MyLibrary.a to incorporate all the required objects for allow compilation distributing it standalone. Both SecondaryLibrary.a and MyLibrary.a need to be placed in the same position where the script file is executed. In case you use some IDE like, for example, Code::block you need to set the automatically execution of this script at the end of each compilation.

Comments

Popular posts from this blog

Access GPIO from Linux user space

Android: adb push and read-only file system error

Tree in SQL database: The Nested Set Model