Skip to content

Instantly share code, notes, and snippets.

@benzap
Created June 9, 2012 20:41
Show Gist options
  • Save benzap/2902510 to your computer and use it in GitHub Desktop.
Save benzap/2902510 to your computer and use it in GitHub Desktop.
Template Makefile For library Construction
# Disable implicit rules to speedup build
.SUFFIXES:
SUFFIXES :=
%.out:
%.a:
%.ln:
%.o:
%: %.o
%.c:
%: %.c
%.ln: %.c
%.o: %.c
%.cc:
%: %.cc
%.o: %.cc
%.C:
%: %.C
%.o: %.C
%.cpp:
%: %.cpp
%.o: %.cpp
%.p:
%: %.p
%.o: %.p
%.f:
%: %.f
%.o: %.f
%.F:
%: %.F
%.o: %.F
%.f: %.F
%.r:
%: %.r
%.o: %.r
%.f: %.r
%.y:
%.ln: %.y
%.c: %.y
%.l:
%.ln: %.l
%.c: %.l
%.r: %.l
%.s:
%: %.s
%.o: %.s
%.S:
%: %.S
%.o: %.S
%.s: %.S
%.mod:
%: %.mod
%.o: %.mod
%.sym:
%.def:
%.sym: %.def
%.h:
%.info:
%.dvi:
%.tex:
%.dvi: %.tex
%.texinfo:
%.info: %.texinfo
%.dvi: %.texinfo
%.texi:
%.info: %.texi
%.dvi: %.texi
%.txinfo:
%.info: %.txinfo
%.dvi: %.txinfo
%.w:
%.c: %.w
%.tex: %.w
%.ch:
%.web:
%.p: %.web
%.tex: %.web
%.sh:
%: %.sh
%.elc:
%.el:
(%): %
%.out: %
%.c: %.w %.ch
%.tex: %.w %.ch
%: %,v
%: RCS/%,v
%: RCS/%
%: s.%
%: SCCS/s.%
.web.p:
.l.r:
.dvi:
.F.o:
.l:
.y.ln:
.o:
.y:
.def.sym:
.p.o:
.p:
.txinfo.dvi:
.a:
.l.ln:
.w.c:
.texi.dvi:
.sh:
.cc:
.cc.o:
.def:
.c.o:
.r.o:
.r:
.info:
.elc:
.l.c:
.out:
.C:
.r.f:
.S:
.texinfo.info:
.c:
.w.tex:
.c.ln:
.s.o:
.s:
.texinfo.dvi:
.el:
.texinfo:
.y.c:
.web.tex:
.texi.info:
.DEFAULT:
.h:
.tex.dvi:
.cpp.o:
.cpp:
.C.o:
.ln:
.texi:
.txinfo:
.tex:
.txinfo.info:
.ch:
.S.s:
.mod:
.mod.o:
.F.f:
.w:
.S.o:
.F:
.web:
.sym:
.f:
.f.o:
#includes several paths that will be common among the subdirectories
#along with several compiler flags
CC = g++
UNAME := $(shell uname)
##########################
######## Linux ###########
##########################
ifeq ($(UNAME), Linux)
#### Global Variables ####
LIBS :=
CFLAGS := -std=c++11
MACRO_FLAGS := -D__LINUX_X86__
endif #END ifeq ($(UNAME), Linux)
##########################
###### Win32-Mingw #######
##########################
#find out if it is a version of mingw
SUB_UNAME := $(findstring MINGW32, $(UNAME))
ifeq ($(SUB_UNAME), MINGW32)
#### Global Variables ####
LIBS := -lboost_unit_test_framework-mgw34-mt
CFLAGS := -std=gnu++0x
MACRO_FLAGS := -D__WIN32_X86__
endif #END ifeq ($(SUB_UNAME), MINGW32)
# Makefile Template for library construction
# Written By: Benjamin Zaporzan
# Date: June 9, 2012
# include this makefile to your parent folder
# directory setup based on exampleObject:
# ./
# ./include/exampleObject/exampleObject.hpp
# ./include/exampleObject/TEST_example_object.hpp
# ./disableImplicit.mk
# ./global_constants.mk
# ./exampleObject/exampleObject.cpp
# ./exampleObject/TEST_exampleObject.cpp
# ./exampleObject/templateLibMakefile
# output result:
# ./
# ./libs/x86_linux/libexampleObject.so
# ./exampleObject/exampleObject.o
# ./libs/exampleObject/TEST_exampleObject.o
# test executable
# ./libs/exampleObject/TEST_exampleObject
#placed in parent folder
include ../disableImplicit.mk
include ../global_constants.mk
TEST_NAME := TEST_exampleObject
#including the uncompiled lib in a test case
UNCOMPILED_LIB := libexampleObject.so
UNCOMPILED_LIB_OBJ := exampleObject.o
#sub-directory where the savage_parser is stored
INC_PATH := ../includes/exampleObject/
INC_DIR := -I../includes/
UNAME := $(shell uname)
##########################
######## Linux ###########
##########################
ifeq ($(UNAME), Linux)
#### Variables ####
LIBS := $(LIBS)
LIBS_PATH := ../libs/x86_linux/
LIBS_DIR := -L../libs/x86_linux/
CFLAGS := $(CFLAGS)
MACRO_FLAGS := $(MACRO_FLAGS) #-Dname=value
FLAGS := $(CFLAGS) $(MACRO_FLAGS)
#### Begin ####
#nothing yet
#### End ####
endif #END ifeq ($(UNAME), Linux)
##########################
###### Win32-Mingw #######
##########################
#find out if it is a version of mingw
SUB_UNAME := $(findstring MINGW32, $(UNAME))
ifeq ($(SUB_UNAME), MINGW32)
#### Variables ####
LIBS := $(LIBS)
LIBS_PATH := ../libs/x86_win32/
LIBS_DIR := -L../libs/x86_win32/
CFLAGS := $(CFLAGS)
MACRO_FLAGS := $(MACRO_FLAGS) #-Dname=value
FLAGS := $(CFLAGS) $(MACRO_FLAGS)
#### Begin ####
$(TEST_NAME) : $(UNCOMPILED_LIB) $(TEST_NAME).o
$(CC) $(UNCOMPILED_LIB) [email protected] -o [email protected] $(LIBS_DIR) $(FLAGS)
$(UNCOMPILED_LIB) : $(UNCOMPILED_LIB_OBJ)
$(CC) -shared -o $@ $<
cp $@ $(LIBS_PATH)/$@
# rm $(UNCOMPILED_LIB)
$(TEST_NAME).o : $(UNCOMPILED_LIB) $(TEST_NAME).cpp $(INC_PATH)/$(TEST_NAME).hpp
$(CC) -c $(TEST_NAME).cpp -o $@ $(LIBS_DIR) $(LIBS) $(INC_DIR) $(FLAGS)
#######################
####### BEGIN #########
#######################
#$(UNCOMPILED_LIB_OBJ) list of makefile targets and dependencies
exampleObject.o : exampleObject.cpp $(INC_PATH)/exampleObject.hpp
$(CC) -c $< -o $@ $(LIBS_DIR) $(LIBS) $(INC_DIR) $(FLAGS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment