Example Makefile for Static Library

January 11, 2005

This is just a simple template. There's always the option of looking at a more complete manual.

# source files.
SRC =  	file1.cpp file2.cpp file3.cpp
 
OBJ = $(SRC:.cpp=.o)
 
OUT = ../libutils.a
 
# include directories
INCLUDES = -I. -I../include/ -I/usr/local/include
 
# C++ compiler flags (-g -O2 -Wall)
CCFLAGS = -g
 
# compiler
CCC = g++
 
# library paths
LIBS = -L../ -L/usr/local/lib -lm
 
# compile flags
LDFLAGS = -g
 
.SUFFIXES: .cpp
 
default: dep $(OUT)
 
.cpp.o:
	$(CCC) $(INCLUDES) $(CCFLAGS) -c $< -o $@
 
$(OUT): $(OBJ)
	ar rcs $(OUT) $(OBJ)
 
depend: dep
 
dep:
	makedepend -- $(CFLAGS) -- $(INCLUDES) $(SRC)
 
clean:
	rm -f $(OBJ) $(OUT) Makefile.bak

Related Posts

11 Comments

Comment March 3, 2010 by anonymous
give a fucking output maybe?
Comment March 4, 2010 by digitalpeer
compile some code and you'll get some fucking output.
Comment March 11, 2010 by gabriel
Hi, Thanks for the example. It is just what I was looking for and works fine. gabriel
Comment October 6, 2010 by mikev
nice example, i dont know what the problem with the other guys was. Also if all your directory .cpp files are sources you can do like that: SRC := $(wildcard *.cpp)
Comment May 10, 2011 by anon
Thanks!
Comment August 3, 2011 by bob flob
@anonymous (3/3/10) get your own fucking output ;)
Comment October 13, 2011 by anonymouse
@anonymous : fuck you and all your outputs
Comment March 10, 2015 by passing_by_stranger
thanks so much for ur example! it's really simple (especially among others in the internet) and helped me to start with my own Makefile for static c++ library ^^
Comment July 19, 2015 by Sahil
can anybody explain the below line: OBJ = $(SRC:.cpp=.o) please...
Comment September 26, 2015 by digitalpeer
This is called substitution references: http://www.gnu.org/software/make/manual/make.html#Substitution-Refs OBJ = $(SRC:.cpp=.o) This is creating a variable called OBJ that contains all of the items listed in SRC with a .cpp extension converted to a .o extension- and it also includes everything else as is without a .cpp extension. So, in the example it will end up being this: OBJ = file1.o file2.o file3.o Automatic conversions like this are useful because you can just modify one variable (SRC) and all of the other variables automatically populate as expected. In the simple example, OBJ is just used to clean the compiled object files- one per source file.
Comment June 30, 2016 by anonymous
Who cares of your fucking output