Monday, October 10, 2011

makefile for 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

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

MAKE = make
CXX     = c++
CXXFLAGS = -O2 -g
LINK   = -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:
$(CXX) $(CXXFLAGS) $(COMPILE) $<

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


-include depend.mak


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


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


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


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

6 comments:

  1. hi,
    can u provide a make file for c especially ?

    ReplyDelete
  2. please find the make file for c @

    http://saurabhgupta0527.blogspot.com/2011/10/makefile-for-c_12.html

    ReplyDelete
  3. nice it worked fine. thanks buddy.

    ReplyDelete
  4. Hi,

    Do u offer any class room trainings for C++?

    ReplyDelete
    Replies
    1. Hi,

      We are sorry to say that currently there is no such plan for training.

      Thanks,
      Saurabh Gupta
      saurabh.gupta@ccplusplus.com

      Delete