Skip to content

Instantly share code, notes, and snippets.

@zeph1e
Created February 6, 2014 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeph1e/8840168 to your computer and use it in GitHub Desktop.
Save zeph1e/8840168 to your computer and use it in GitHub Desktop.
Makefile Template for small project
# Makefile template for small project
# Yunsik Jang <doomsday@kldp.org>
SYSROOT=/
# If you use custom toolchain specify it
TOOLCHAIN_PATH :=
TOOLCHAIN_PREFIX :=
ifneq ($(strip $(TOOLCHAIN_PATH)),)
CC := $(TOOLCHAIN_PATH)/bin/$(TOOLCHAIN_PREFIX)$(CC)
CXX := $(TOOLCHAIN_PATH)/bin/$(TOOLCHAIN_PREFIX)$(CXX)
LD := $(TOOLCHAIN_PATH)/bin/$(TOOLCHAIN_PREFIX)$(LD)
AR := $(TOOLCHAIN_PATH)/bin/$(TOOLCHAIN_PREFIX)$(AR)
endif
# specify target binary names:
# the source filename which have main function should be same to target name
# ex) aa.c or aa.cpp --> aa (binary)
TARGETS := \
# If you want to build shared lib, use this.
# Also, binaries in $(TARGET) will be linked with this lib
TARGETLIBS := \
TARGETLIBOBJS := \
UTILOBJS := \
# specify objects being referenced from $(TARGETS)
COMMONOBJ := \
$(UTILOBJS)
# specify package name being known to pkg-config
PKG_DEPENDENCIES :=
# if pkg-config path is differ from default one. give it here
PKG_CONFIG_PATH :=
PKG_CFLAGS := \
$(shell export PKG_CONFIG_PATH=$(PKG_CONFIG_PATH); \
$(patsubst %,pkg-config --cflags %,$(PKG_DEPENDENCIES)))
PKG_LDFLAGS := \
$(shell export PKG_CONFIG_PATH=$(PKG_CONFIG_PATH); \
$(patsubst %,pkg-config --libs %,$(PKG_DEPENDENCIES)))
# write some include and lib flags
INCLUDES := \
LIBS := \
DEFINES := \
# some other compiler/linker flags here
CFLAGS := -fPIC -rdynamic
CXXFLAGS := -fPIC -fpermissive
LDFLAGS :=
all: $(TARGETLIBS) $(TARGETS)
%.o:%.c
$(CC) -c $< $(INCLUDES) $(PKG_CFLAGS) $(DEFINES) $(CFLAGS)
%.o:%.cpp
$(CXX) -c $< $(INCLUDES) $(PKG_CFLAGS) $(DEFINES) $(CXXFLAGS)
$(TARGETS) : $(COMMONOBJ) $(patsubst %,%.o,$(TARGETS)) $(TARGETLIBS)
$(CXX) -o $@ $@.o $(COMMONOBJ) $(PKG_LDFLAGS) $(LDFLAGS) $(LIBS) $(patsubst %,-l%,$(TARGETLIBS))
$(TARGETLIBS) : $(TARGETLIBOBJS) $(COMMONOBJ)
$(CXX) -shared -o lib$@.so $(COMMONOBJ) $(TARGETLIBOBJS) $(PKG_LDFLAGS) $(LDFLAGS) $(LIBS)
clean:
rm -f *.o $(TARGETS) $(patsubst %,lib%.so,$(TARGETLIBS))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment