Search code examples
c++static-librariesdebug-symbolsotool

Strip/Remove debug symbols and archive names from a static library


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.


Solution

  • 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