Profile picture
, 21 tweets, 4 min read Read on Twitter
Question for Android C devs: how do you invoke Libtool to link several static libraries into one?
I haven’t found any article explaining this.
Explainer: I have several static libs that I want to link into a single static lib (so I can then edit the resulting lib with ar to remove a few annoying objects).
With Xcode, I simply have to call libtool -o libout.a libinput1.a ...
So I think it should work the same.
Further explainer: the input libs are customized Poco libs and other in-house stuff, and they include zlib symbols baked into libPocoFoundation.a.
Obviously the zlib symbols conflict with my own version of zlib.
No real answer, but a thread on how I circumvented the issue: (1/n)
So, it figures, GNU libtool and Xcode libtool are 2 completely different tools. Functionality-wise at least. This made the idea of repacking the libs for Android slightly more complicated.
(2/n) I tried running libtool with clang, ld, running clang, ld, separately as well, to no avail.
Last solution was to just unpack the lib files (.a files are just archives packed with a tool called `ar`).
(3/n) Unpacking with `ar` obviously worked, and repacking would have worked as well, if it weren't for a larger number of similarly object files that were apparently packed into the same lib, and whose files were merrily getting overwritten while unpacking.
(4/n) Repacking the objects (after having reduced their number through overwriting) was prone to lead to linker issues down the line. So this was not a valid solution.
(5/n) The fun part starts here: Since I'm using GENie to generate my projects, and since GENie is just Lua being executed, I could extend the idea of a metaproject to generate patched libs.
(6/n) Aside: my earlier version (which worked great for macOS and iOS since I could rely on Xcode tools) consisted in having a simple 'meta-project' which contained in single C file to generate a stub library.
(7/n) Aside cont'd: As a postbuildcommand step of my 'meta-project' I ran a script that would take the actual input libs, merge them into a single lib, and the run `ar` over it to remove the zlib symbols.
(8/n) Aside cont'd: Note: this was pretty fast, even for iOS (<20s) where the removal script had to 'thin' the 'fat' library for each contained architecture (armv7, arm7s, arm64, x86, x86_64), patch it, then re-coalesce at the end. (`lipo` allows to do that).
(9/n): Back to the fun part: so instead of generating a single 'meta-project', I am now generating a 'remix-project' that outputs a stub lib file, which I then overwrite with the patched version of the actual input lib.
(10/n) So the script looks like this
```createRemixProjects(os.matchfiles(path.join(proj_root, "debug", "path", "*.a")),
{ "android-arm", "Debug"},{
"AR=" .. premake.gcc.ar .. " " ..
relPath(path.join(script, "remove_zlib_gnu.sh")) ..
" $(TARGET) "
})
```
(11/n) I should've written a gist for this.
createRemixProjects just looks like this:
```
function createRemixProjects(libs, cfgterms, cmds)
for _, a in ipairs(libs) do
createRemixProject(a, cfgterms, cmds)
end
end
```
(12/n) ```
function createRemixProject(libfile, cfgterms, cmds)
local finishCmd = function(cmd) return cmd .. " " .. relPath(libfile) end
project (createRemixProjectName(libfile))
kind "StaticLib"
language "C"
files { "stub.c" }
configuration{ cfgterms }
-- cont'd below..
postbuildcommands {
table.translate(cmds, finishCmd)
}
configuration{}
end
```
(14/n) the object removal just consists in 1 call to `ar`:
```
OUTPUT=$1
INPUT=$2

cp -fv $INPUT $OUTPUT
${AR} t $OUTPUT | grep adler32
${AR} dv $OUTPUT $zlib_objects
```
(15/n) ~~xcode~~ BSD `ar` is pretty much the same, only it takes -t, -d as options.
(16/n) and for fat (iOS) libs, you need to iterate with lipo like this
```
for arch in `lipo -archs $INPUT`
do
ARCHLIB=${INPUT_BASENAME}_$arch.${INPUT_EXTENSION}
archlibs="$archlibs $ARCHLIB"
lipo -thin $arch $INPUT -output $ARCHLIB
ar -d $ARCHLIB $zlib_objects
done
```
(17/17) Thread end.
This whole build process works pretty neatly, it's fast, and it doesn't fail if the symbols can't be found.
Next time I'll write how I use lib.exe to do the same with MSVC.
@threadreaderapp unroll please :)
Missing some Tweet in this thread?
You can try to force a refresh.

Like this thread? Get email updates or save it to PDF!

Subscribe to KageKirin
Profile picture

Get real-time email alerts when new unrolls are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!