Makefile
// Makefile
SUBDIRS = main foo
all : $(SUBDIRS)
install : $(SUBDIRS)
# $(call version_number,1.2.3) # major.minor.patch
# libtool manual chapter 4.2: -version-number
define version_number
? $(MAKE) -C $@ soname=lib$@.so.$(word 1,$(subst ., ,$(1)))
? @ cp $@/$@ $@/lib$@.so.$(1)
? @ cd $@; ln -f -s lib$@.so.$(1) lib$@.so.$(word 1,$(subst ., ,$(1))); cd ..
? @ cd $@; ln -f -s lib$@.so.$(1) lib$@.so; cd ..
endef
main : foo
main :
? $(MAKE) -C $@
foo :
? $(call version_number,5.2.3)
# make DESTDIR=~/foo install
install :
? install -d "$(DESTDIR)/usr/local/bin"
? install -d "$(DESTDIR)/usr/local/lib"
? install -m 0755 main/main "$(DESTDIR)/usr/local/bin"
? install -m 0755 foo/*.so* "$(DESTDIR)/usr/local/lib"
clean :
? @ for dir in $(SUBDIRS); do \
? ? $(MAKE) -C $$dir $@; \
? done
.PHONY : $(SUBDIRS) all install clean
---
// main/Makefile
# build shared library with -fPIC, -shared
CFLAGS ? = # -g -O3 -fPIC # CXXFLAGS for .cpp
CPPFLAGS = -MMD -MP -I../foo
LDFLAGS ?= -L../foo -Wl,-rpath,'$$ORIGIN/../foo' # -shared
LDLIBS ? = -lfoo
#CC ? ? ?= $(CXX) # link with CXX for .cpp
LDFLAGS += -Wl,-rpath,'$$ORIGIN/../lib'
#LDFLAGS += -Wl,-soname,$(soname)
# make # NDEBUG=1
ifdef NDEBUG
CPPFLAGS += -DNDEBUG
CFLAGS += -O3 # .cpp
else
CFLAGS += -g # .cpp
LDFLAGS += -fsanitize=address
endif
all : main # foo
# target name is basename of one of the source files
main : $(patsubst %.c,%.o,$(wildcard *.c)) # .cpp
#foo : $(patsubst %.c,%.o,foo.c) # .cpp
-include *.d
clean : ; -rm -fr *.o *.d main
.PHONY : all clean
---
// foo/Makefile
# build shared library with -fPIC, -shared
CFLAGS ? = -fPIC # -g -O3 # CXXFLAGS for .cpp
CPPFLAGS = -MMD -MP # -I../bar
LDFLAGS ?= -shared # -L../bar -Wl,-rpath,'$$ORIGIN/../bar'
LDLIBS ? = #-lbar
#CC ? ? ?= $(CXX) # link with CXX for .cpp
LDFLAGS += -Wl,-rpath,'$$ORIGIN/../lib'
LDFLAGS += -Wl,-soname,$(soname)
# make # NDEBUG=1
ifdef NDEBUG
CPPFLAGS += -DNDEBUG
CFLAGS += -O3 # .cpp
else
CFLAGS += -g # .cpp
LDFLAGS += -fsanitize=address
endif
all : foo # bar
# target name is basename of one of the source files
foo : $(patsubst %.c,%.o,$(wildcard *.c)) # .cpp
#bar : $(patsubst %.c,%.o,bar.c) # .cpp
-include *.d
clean : ; -rm -fr *.o *.d foo *.so*
.PHONY : all clean