Wednesday, July 27, 2011

Sample Makefile for C, C++

File Usage:


1. to build the code executable copy the make file to your source code location ((.cpp, .h)) and directly hit the "make" command 
2. if you are trying to build the static library (.a) then make sure the the file having the "main" function should be removed first. then hit "make static"
3. if you are trying to build the shared library (.so) then make sure the the file having the "main" function should be removed first. then hit "make shared"


######################################################################################
# Purpose               :   Sample MakeFile that can ber used to build you c++ code
#
# Author                :   Saurabh Gupta
#
# Date Created          :   25-02-2010
#
# Date Modified         :   27-07-2011
#
# Note                  : 1. make        : to build exe
#                         2. make static : to build static library (remove main function first)
#                         3. make shared : to build shared library (remove main function first)
#
# Usage                 : change the "EXE" to your program name or else use as it is
######################################################################################
OSTYPE = $(shell uname)
MAKE = make
CC = c++
LINK = -g -pedantic -Wall -lstdc++ -lpthread -ldl -lm -Wl,-rpath,.
COMPILE = -g -O0 -D_THREAD_SAFE -pedantic -Wall -c -Wno-deprecated 
EXE    = ./myProgram
static = libmyProgram.a
shared = libmyProgram.so
SRCS   = $(shell ls *.cpp)
OBJS   = $(subst .cpp,.o,$(SRCS))


.SUFFIXES: .o .cpp

.cpp.o:
$(CC) $(COMPILE) $<

all: $(OBJS)
$(CC) $(OBJS) -o $(EXE) $(LINK)


-include depend.mak


depend:
g++ -MM $(SRCS) > depend.mak


static:
ar -crvs $(a) $(OBJS)


shared: $(OBJS)
$(CC) -shared -Wl,-soname -lc -o $(so) $(OBJS) 


clean:
rm -rf $(OBJS) depend.mak $(EXE) $(so) $(a)

######################################################################################

See Also
makefile for c

No comments:

Post a Comment