makefile scripts and samples
It is efficient to use scripts when the machine environment is diverse. Most of the settings are standardized. Modify the scripts if you use external libraries and headers. Options are also easily added or removed.
Common mk script
#!/bin/ksh
#-----------------------------------------------------------
# common mk script
#-----------------------------------------------------------
UNAME=`uname`
if [ "$UNAME" = "Linux" ]
then
export CC=gcc
export OS="-DOS_LINUX"
export CFLAGS="${OS} -g -O2 -m64 -Wall -fPIC"
export LFLAGS="-dynamic"
export SHFLAGS="-shared"
elif [ "$UNAME" = "AIX" ]
then
export CC=xlc
export OS="-DOS_AIX"
export CFLAGS="${OS} -g -O2 -q64"
export LFLAGS="-brtl"
export SHFLAGS="-G -bM:SRE"
elif [ "$UNAME" = "HP-UX" ]
then
export CC=cc
HPOS=`uname -a | awk '{if($5 == "ia64) {print $5}}'`
if [ "$HPOS" = "ia64" ]
then
export OS="-DUSR_OS_HPUX"
else
export OS="-DUSR_OS_HPUX_PARISC"
fi
export CFLAGS="${OS} -g -O2 +DD64"
export LFLAGS="-O2 +DD64"
export SHFLAGS="-b -z"
elif [ "$UNAME" = "SunOS" ]
then
export CC=cc
export OS="-DUSR_OS_SUN"
export CFLAGS="${OS} -g -O2 -m64 -KPIC"
export LFLAGS="-g -O2 -m64 -dy -lsocket -nsl"
# export LFLAGS="-g -O2 -dy -lsocket -nsl" # SunOS 32bits
export SHFLAGS="-G"
else
exit
fi
echo "-----------------------------------------"
echo "${UNAME} Start compiling..."
echo "-----------------------------------------"
make $1 $2 $3
makefile case #1 : c
#===========================================================
# makefile
#===========================================================
#-----------------------------------------------------------
# TARGETS : Multiple targets can be separated by space.
#-----------------------------------------------------------
TARGETS=pgm1 pgm2
USRHOME=$HOME
BIN=${USRHOME}/bin
INC=${USRHOME}/inc
LIB=${USRHOME}/lib
USRINCS=-I. -I${INC}
USRLIBS=-L. -L${LIB}
USRDEFS=
# if you need more cflags, you can add them.
CFLAGS=$(CFLAGS) $(USRINCS) $(USRDEFS)
# if you need more lflags, you can add them.
LFLAGS=$(LFLAGS) $(USRLIBS)
#-----------------------------------------------------------
# Compile
#-----------------------------------------------------------
.SUFFIXES : .c .o
.c.o:
$(CC) $(CFLAGS) -c $<
all:$(TARGETS)
#-----------------------------------------------------------
# Linking
#-----------------------------------------------------------
pgm1:pgm1.o
$(CC) $(LFLAGS) -o $@ $<
pgm2:pgm2.o
$(CC) $(LFLAGS) -o $@ $<
#-----------------------------------------------------------
# install
#-----------------------------------------------------------
install:all
cp -pf $(TARGETS) $(BIN)
#-----------------------------------------------------------
# clean
#-----------------------------------------------------------
clean:
rm -f $(TARGETS) *.o
makefile case #2 : c and c++
If you met the following message for linking, you can add c++ library. For gcc try adding -lstdc++ to your LFLAG. However, for g++ it will link libstdc.so.6++ automatically.
- libstdc++: DSO missing from command line
#===========================================================
# makefile
#===========================================================
#-----------------------------------------------------------
# TARGETS : Multiple targets can be separated by space.
#-----------------------------------------------------------
TARGETS=pgm1 pgm2 pgm3 pgm4
USRHOME=$HOME
BIN=${USRHOME}/bin
INC=${USRHOME}/inc
LIB=${USRHOME}/lib
USRINCS=-I. -I${INC}
USRLIBS=-L. -L${LIB}
USRDEFS=
# if you need more cflags, you can add them.
CFLAGS=$(CFLAGS) $(USRINCS) $(USRDEFS) -DHAVE_COMFIG_H -D_GNU_SOURCE -Wno-write-strings -D_X_AGT_CPP
# if you need more lflags, you can add them.
LFLAGS=$(LFLAGS) -lstdc++ $(USRLIBS)
#-----------------------------------------------------------
# Compile
#-----------------------------------------------------------
.SUFFIXES : .c .o .cpp
.c.o:
$(CC) $(CFLAGS) -c $<
.cpp.o:
$(CC) $(CFLAGS) -c $<
all:$(TARGETS)
#-----------------------------------------------------------
# Linking
#-----------------------------------------------------------
# case #1 - you can add -D flag here
pgm1:pgm1.cpp pgm1.h
$(CC) $(CFLAGS) $(LFLAGS) -o $@ pgm1.cpp
#case #2 - -D flag is not working here
pgm2:pgm2.o
$(CC) $(LFLAGS) -o $@ $@.o
#case #3 - -D flag is not working here
pgm3:pgm3.o
$(CC) $(LFLAGS) -o $@ $<
#-----------------------------------------------------------
# install
#-----------------------------------------------------------
install:all
cp -pf $(TARGETS) $(BIN)
#-----------------------------------------------------------
# clean
#-----------------------------------------------------------
clean:
rm -f $(TARGETS) *.o
Leave a comment