Feb 26, 2010

A script to create shared lib

   A good script to create shared lib

   1. With root privileges: create directories /usr/local/lib and /usr/local/include (if not exist)
   2. add pass for /usr/local/lib in file /etc/ld.so.conf (if not exists)
   3. run script: create_shared_lib.sh (text below)
      f.e. you have money.c file that is prepared for creating library "money".
      $sh create_shared_lib money
   4. copy your money.h to /usr/local/include
   5. to use your library:
      include money.h in global area of your program (#include &ltmoney.h>)
      gcc -Wall -g -o my_prog.exe my_prog.c -I/usr/local/include -lmoney

   1. Without root privileges: create directory lib and include in your $HOME directory
   2. change script's line 'mv "$libname".* /usr/local/lib/' for 'mv "$libname".* ~/lib/'
   3. comment line 'ldconfig &'
   4. run script (as I explained above)
   5. copy your header file into ~/include directory
   6. to use your library:
      include money.h in global area of your program (#include &ltmoney.h>)
      gcc -Wall -g -o my_prog.exe my_prog.c -I$HOME/include -L$HOME/lib -lmoney

create_shared_lib.sh

#!/bin/bash

ARG=1
BADARG=65
if [ "$#" -ne "$ARG" ] 
then
echo -e "\t\tUsages: `basename $0` name_of_shared_library"
exit $BADARG
fi

libname=lib"$1"

##########################################################################
# uncomment next 2 lines and comment 3-4 lines if you create C++ library #
##########################################################################

#g++ -fPIC -c "$1".cpp          
#g++ -shared -Wl,-soname,"$libname".so.1 -o "$libname".so.1.0 "$1".o
gcc -fPIC -c "$1".c
gcc -shared -Wl,-soname,"$libname".so.1 -o "$libname".so.1.0 "$1".o
ln -s "$libname".so.1.0 "$libname".so.1
ln -s "$libname".so.1 "$libname".so

echo -e "\t\tSHARED LIBRARY $libname IS CREATED"
echo -e "\t\t==================================\n"
echo
mv "$libname".* /usr/local/lib/

echo -e "\t\tldconfig is working"
ldconfig &

exit 0

No comments: