4

I have a static library (C++) (say, libmylib_DARWIN.a and libmylib_LINUX.a for 2 architectures) compiled on my Mac using clang (Apple LLVM version 9.0.0 (clang-900.0.39.2) if is of any relevance).

Right now, there are two problems:

  1. The static library (using a current build configuration) contains debug symbols
  2. It also shows names of the object files that were used for the archive

    otool -Iv libmylib_DARWIN.a

    Archive : libmylib_DARWIN.a libmylib_DARWIN.a(firstobjectfile.cpp.o) libmylib_DARWIN.a(secondobjectfile.cpp.o) ....

I would like to remove both the debug symbols and archived filenames from this library. I wonder if there is a convenient way to do it without changing my build configuration.

  • will strip on Mac do it for both DARWIN- and LINUX-built libraries? Anything I should pay attention too?
  • strip doesn't seem to remove the archive filenames

There are some similar questions on SO; however, the ones I found deal with either iOS, Objective C, do not talk about multiplatform, and do not mention archive names.

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
  • 1
    Just currious; "I would like to remove both ..." - *why*? – Jesper Juhl Apr 03 '18 at 13:12
  • both the debug symbols and filenames disclose information that I legally should not share. With filenames, it can technically be solved by renaming (which is very inconvenient), and I would like to strip debug symbols for multiple architectures from my home OS - also a convenience. – Anton Menshov Apr 03 '18 at 13:15
  • 1
    extract the original .o files with `ar`, run `strip` on them individually, rename them, repack them again with `ar` or `ld`. – Sigi Apr 03 '18 at 13:17

1 Answers1

5

This script implements Sigismondo's suggestion (unpacks the archive, strips each object file individually, renames them 1000.o, 1001.o, etc., and repacks). The parameters for ar crus may vary depending on your version of ar.

#!/bin/bash
# usage: repack.sh file.a

if [ -z "$1" ]; then
    echo "usage: repack file.a"
    exit 1
fi

if [ -d tmprepack ]; then
    /bin/rm -rf tmprepack
fi

mkdir tmprepack
cp $1 tmprepack
pushd tmprepack

basename=${1##*/}

ar xv $basename
/bin/rm -f $basename
i=1000
for p in *.o ; do
    strip -d $p
    mv $p ${i}.o
    ((i++))
done

ar crus $basename *.o
mv $basename ..

popd
/bin/rm -rf tmprepack
exit 0
Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
FBergo
  • 1,010
  • 8
  • 11
  • Thanks for the implementation. This script definitely removes the debug symbols and changes filenames, but now I cannot link to it as before. Undefined symbols - as references are undefined now.... – Anton Menshov Apr 03 '18 at 14:57
  • 1
    Sorry about that, try changing the strip line to `strip -d $p` to strip only debug symbols. – FBergo Apr 03 '18 at 15:09
  • 1
    That script worked on Mac for a Mac version of the library. However, it refused to work on the LINUX one. However, it also worked on LINUX for LINUX library. That solves 90% of the problem. I'll accept it if nothing better comes up. Thanks! – Anton Menshov Apr 03 '18 at 15:21
  • 2
    Thx for the script! Only addition I had to do on macOS was to only strip .o files (`if [[ $p == *.o ]]; then`), and use the "-S" flag (-d means something else): `strip -S $p` – JCash Jan 08 '21 at 15:44