Re: How to adding pathes to dependencies in rules of `.d` files

"ljh" <[email protected]>
Newsgroups gmane.comp.gnu.make.general
Message-ID <[email protected]>
Can you please give me suggestions to improve it.

1. In thie out of source version, I can not omit recipes in both building and linking rules. This make it a bit complicated.

2. if I put # comment at the end of the line of export OBJDIR, it causes mysterious error message in rules involve the variable.


```
## src/Makefile

# Makefile for top dir

export OBJDIR = $(dir $(realpath $(lastword $(MAKEFILE_LIST))))/objdir_tmp

# $(call version_number,1.2.3)
# major.minor.patch
# libtool manual 4.2: -version-number
define version_number
	$(MAKE) -C $@ [email protected].$(word 1,$(subst ., ,$(1)))
	cp $(OBJDIR)/$@/$@ $(OBJDIR)/$@/[email protected].$(1)
	cd $(OBJDIR)/$@; ln -f -s $(OBJDIR)/$@/[email protected].$(1) $(OBJDIR)/$@/[email protected].$(word 1,$(subst ., ,$(1))); cd ..
	cd $(OBJDIR)/$@; ln -f -s $(OBJDIR)/$@/[email protected].$(1) $(OBJDIR)/$@/[email protected]; cd ..
endef

SUBDIRS = main foo

all : $(SUBDIRS)
install : $(SUBDIRS)

main : foo
main : ; $(MAKE) -C $@
foo : ; $(call version_number,6.2.3)

# make DESTDIR=~/foo install
install :
	install -d "$(DESTDIR)/usr/local/bin"
	install -d "$(DESTDIR)/usr/local/lib"
	install -m 0755 $(OBJDIR)/main/main "$(DESTDIR)/usr/local/bin"
	install -m 0755 $(OBJDIR)/foo/*.so* "$(DESTDIR)/usr/local/lib"

clean : ; -rm -fr $(OBJDIR)

.PHONY : $(SUBDIRS) all install clean
```


---


```
## src/main/Makefile

# Makefile for subdir

# build shared library with -fPIC, -shared
CFLAGS    = # -g -O3 -fPIC # CXXFLAGS for .cpp
CPPFLAGS  = -MMD -MP -I../foo
LDFLAGS   = -L$(OBJDIR)/foo # -shared
LDLIBS    = -lfoo
#CC       = $(CXX) # link with CXX for .cpp

LDFLAGS  += -Wl,-rpath,$(OBJDIR)/foo
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 : $(OBJDIR)/main/main # foo

$(OBJDIR)/main/main : $(OBJDIR)/main/$(patsubst %.c,%.o,$(wildcard *.c))
	$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@  # CXX for .cpp

$(OBJDIR)/main/%.o : %.c | $(OBJDIR)/main
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $&lt;  # CXX CXXFLAGS for .cpp

$(OBJDIR)/main: ; mkdir -p $(OBJDIR)/main

-include $(OBJDIR)/main/*.d
clean : ; -rm -fr $(OBJDIR)

.PHONY : all clean
```


---


```
## src/foo/Makefile

# Makefile for subdir

# build shared library with -fPIC, -shared
CFLAGS    = -fPIC # -g -O3 # CXXFLAGS for .cpp
CPPFLAGS  = -MMD -MP # -I../bar
LDFLAGS   = -shared # -L../bar
LDLIBS    = # -lbar
#CC       = $(CXX) # link with CXX for .cpp

LDFLAGS  += -Wl,-rpath,'$$ORIGIN/../bar'
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 : $(OBJDIR)/foo/foo # foo

$(OBJDIR)/foo/foo : $(OBJDIR)/foo/$(patsubst %.c,%.o,$(wildcard *.c))
	$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@  # CXX for .cpp

$(OBJDIR)/foo/%.o : %.c | $(OBJDIR)/foo
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $&lt;  # CXX CXXFLAGS for .cpp

$(OBJDIR)/foo: ; mkdir -p $(OBJDIR)/foo

-include $(OBJDIR)/foo/*.d
clean : ; -rm -fr $(OBJDIR)

.PHONY : all clean
```

---
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.