gcc编译: 打包若干.o和.a文件为新的.a文件

使用场景:

gcc编译cpp的时候会生成.o , 然后若干.o文件会打包生成.a文件

但是有的时候是需要, 若干.o和多个.a, 打包生成 最终的一个.a文件

*.o  --------\

xxx.a              --------->  des.a  

bbb.a ------/


gcc编译: 打包若干.o和.a文件为新的.a文件


方法:

编写了一个脚本, 复制并存储为 rebuild_archive.sh, 直接在Makefile里调用即可, 调用命令:
sh ./rebuild_archive.sh 目标.a    源.a ,,.... 源.o,,,,,</span>

sh ./rebuild_archive.sh  libdes.a  libda_frame.a libda_dso_dll.a_da.o libda_dso_dll.a_daconf.o libda_dso_dll.a_damain.o


#!/bin/sh


# Usage: sh rebuild_archive.sh  guo.a libda.sf_dll.a da_version.o 


if [ "$#" -lt "1" ]; then
echo "param error for archive rebuild.sh"
fi


#输入参数集合
PARAM_STR=$*
# 目标文件
TARGET=$1

TARGETTMPDIR=$TARGET.rebuild_archive
ARMACTEMPOBJ=$TARGET.rebuild_archive.ar.mac


test -d $TARGETTMPDIR && (echo "[FATAL]: some process running for $TARGET" && exit 0)
test -d $TARGETTMPDIR.2nd && (echo "[FATAL]: some process running for $TARGET" && exit 0)


rm -rf $TARGETTMPDIR
rm -rf $TARGETTMPDIR.2nd
mkdir -p $TARGETTMPDIR
mkdir -p $TARGETTMPDIR.2nd


echo OPEN ${TARGET} > $TARGETTMPDIR/$ARMACTEMPOBJ


for line in  ${PARAM_STR#* }
do
    SUFFIX=`echo ${line:0-2}`


    # 如果文件后缀是 .a, 那么解压到.o 临时文件夹
    if [ "$SUFFIX" = ".a" ];then
    #echo "this is .a : $line, extract it to tmpdir" 
    echo ADDLIB $line >> $TARGETTMPDIR/$ARMACTEMPOBJ
    fi


    # 如果文件后缀是 .o, 那么cp到临时文件夹
    if [ "$SUFFIX" = ".o" ];then
    #echo "this is .o : $line, copy it to tmpdir" 
    cp -f $line $TARGETTMPDIR || exit $?
    fi


# 忽略其他文件类型
# ignore other file types 
done


echo SAVE >> $TARGETTMPDIR/$ARMACTEMPOBJ
echo END >> $TARGETTMPDIR/$ARMACTEMPOBJ


# 打包目标
OOO_NUM=`ls ${TARGETTMPDIR}/*.o  | wc -l`
if [ "$OOO_NUM" -gt "0" ];then
cd ${TARGETTMPDIR} && ar crs ../${TARGET} ./*.o && cd - &&  ar -M < $TARGETTMPDIR/$ARMACTEMPOBJ || exit $?
else
sed -i 's/OPEN/CREATE /g' $TARGETTMPDIR/$ARMACTEMPOBJ 
ar -M < $TARGETTMPDIR/$ARMACTEMPOBJ || exit $?
fi




rm -rf $TARGETTMPDIR
rm -rf $TARGETTMPDIR.2nd






©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页