- Prison Code Breaker Diary -

=> aka: Nhật Kí Code Tù

Categories

I write this bash script to automate the process of creating Linux libraries in C/C++

#!/bin/bash

clear

echo -e "##########################################"
echo -e "#     LIBRARY AUTO-CREATOR by [JaPh]     #"
echo -e "#========================================#"
echo -e "Usage: `basename $0` lib_name type path   "
echo -e "Example: "
echo -e " + creating libraries from hello.c in current directory"
echo -e " `basename $0` hello C . "
echo -e " + creating libraries from bye.cpp to '/opt/lib' directory"
echo -e "   `basename $0` bye C++ /opt/lib"
echo -e "Feel free to contact, pete.houston.17187@gmail.com\n"

ARG=3

if [ "$#" -ne "$ARG" ]
then
 echo -e "[?] wrong input arguments!"
 exit
fi

libname=lib"$1"

if [ "$2" == "C" ]
then
 filename="$1.c"
 compiler="gcc"
elif [ "$2" == "C++" ]
then
 filename="$1.cpp"
 compiler="g++"
else
 echo -e "[?] unknown filetype"
 exit
fi

echo -e "< Static Library: $libname.a >"
command="$compiler -Wall -c $filename"
echo -e "\$ $command"
if [ !`$command` ]
then
 echo -e ".created: $1.o"
else
 echo -e ".[?] cannot create object $1.o"
 exit
fi

echo -e "\$ ar -cvq $libname.a $1.o"
ar -cvq $libname.a $1.o
echo -e ".created: $libname.a"
echo -e "< Static Library '$libname.a' created ! >"

rm -f $1.o
echo -e "< Shared Object Library: $libname.so.1.0 >"
command="$compiler -Wall -fPIC -c $1.c"
echo -e "\$ $command"
if [ !`$command` ]
then
 echo -e ".created: $1.o"
else
 echo -e ".[?] cannot create object $1.o"
 exit
fi

command="$compiler -shared -Wl,-soname,$libname.so.1 -o $libname.so.1.0 $1.o"
echo -e "\$ $command"
if [ !`$command` ]
then
 echo -e ".created: $libname.so.1.0"
else
 echo -e ".[?] cannot create shared object $libname.so.1.0"
 exit
fi

ln -sf $libname.so.1.0 $libname.so
ln -sf $libname.so.1.0 $libname.so.1

echo -e "< Shared Object Library '$libname.so.1.0' created ! >"

if [ "$3" != "." ]
then
 mv $libname.* "$3"
 echo -e "[!] All files moved to $3"
fi


0 comments

Post a Comment