Skip to content

Instantly share code, notes, and snippets.

@PythonJedi
Last active April 3, 2016 21:58
Show Gist options
  • Save PythonJedi/cff91a0445310ea84e49e8c8e8e07336 to your computer and use it in GitHub Desktop.
Save PythonJedi/cff91a0445310ea84e49e8c8e8e07336 to your computer and use it in GitHub Desktop.
# whitespace separated list of source directories and desired products
sources = common server client
products = server client
# compiletime flags, constant
CXXFLAGS = -Wall -std=c++11
# Initialize the obj and source lists for an arbitrary directory
define init
$(1)_src = $(wildcard $(1)/*.cpp)
$(1)_obj = $(wildcard $(1)/*.o)
endef
# parameterized flags for include and lib locations
lib_flags = -Lcommon -L$(1)
include_flags = -Icommon -I$(1)
# parameterized compile rule
define compile
compile_$(1): $(common_src) $($(1)_src)
g++ -c $(1) $^ $(CXXFLAGS) $(call include_flags,$(1))
endef
#parameterized link rule
define link
$1: $(common_obj) $($(1)_obj)
g++ -o $(1) $^ $(call lib_flags,$(1))
endef
# define all the variables for each directory
$(foreach src,sources,$(eval $(call init,src)))
# define the compile rules for each directory
$(foreach src,sources,$(eval $(call compile,dir)))
# define the link rules for each product
$(foreach p,products,$(eval $(call link,p)))
# At this point, there are [common|client|server]_[src|obj] variables,
# [common|client|server]_compile rules, and [client|server] rules, such that
# all: $(product) will try to link all the product executables, requiring it to
# compile all the sources.
all: $(products)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment