74324

makefile: pattern rules for subdirectory

Question:

Here is my project:<br />

project |--- main.cpp |--- makefile |--- test |--- Test.cpp |--- Test.h

Here is the makefile:<br />

g++1x:=g++ -std=c++14 -stdlib=libc++ -MMD -MP cflags:= -Wall -lncurses PATHS:=./ ./test/ TARGET:=matrix.out SRC:=$(foreach PATH,$(PATHS),$(wildcard $(PATH)/*.cpp)) OBJDIR:=.obj OBJ:=$(addprefix $(OBJDIR)/,$(notdir $(SRC:.cpp=.o))) .PHONY: install install: $(OBJDIR) $(TARGET) $(OBJDIR): mkdir -p $(OBJDIR) $(TARGET): $(OBJ) $(g++1x) $(cflags) -o $@ $^ -g $(OBJDIR)/%.o: %.cpp $(g++1x) -c -o $@ $< -g $(OBJDIR)/%.o: ./test/%.cpp $(g++1x) -c -o $@ $< -g -include $(addprefix $(OBJDIR)/,$(notdir $(SRC:.cpp=.d))) .PHONY: clean clean: rm -f $(TARGET) rm -rf $(OBJDIR)

It works well but I have two questions:<br /> 1) Is it possible to avoid foreach for PATHS so that I can use the same makefile for all of cpp projects?<br /> 2) As you see, to generate main.o and Test.o I write two blocks:<br />$(OBJDIR)/%.o: ./test/%.cpp and $(OBJDIR)/%.o: %.cpp.<br /> Is it possible to write only once?<br /> I've tried as below but it doesn't work:<br />

$(OBJDIR)/%.o: $(foreach PATH,$(PATHS),$(wildcard $(PATH)/%.cpp)) $(g++1x) -c -o $@ $< -g

I've even tried like this but it doesn't work still:<br />

$(OBJDIR)/%.o: %.cpp ./test/%.cpp $(g++1x) -c -o $@ $< -g

Answer1:

You should keep the source tree into your object tree. This way it will be easier to create <em>global</em> rules and keep dependencies.

# Use the shell find command to get the source tree SOURCES := $(shell find * -type f -name "*.c") OBJDIR := .objects # Keep the source tree into the objects tree OBJECTS := $(addprefix $(OBJDIR)/,$(SOURCES:.c=.o)) all: mytarget mytarget: $(OBJECTS) $(CC) $^ -o $@ # As we keep the source tree we have to create the # needed directories for every object $(OBJECTS): $(OBJDIR)/%.o: %.c mkdir -p $(@D) $(CC) -MMD -MP -c $< -o $@ -include $(OBJECTS:.o=.d) <hr /><pre class="lang-sh prettyprint-override">$ make mkdir -p .objects cc -MMD -MP -c main.c -o .objects/main.o mkdir -p .objects/test cc -MMD -MP -c test/test.c -o .objects/test/test.o cc .objects/main.o .objects/test/test.o -o mytarget $ tree -a . ├── main.c ├── Makefile ├── mytarget ├── .objects │   ├── main.d │   ├── main.o │   └── test │   ├── test.d │   └── test.o └── test ├── test.c └── test.h 3 directories, 9 files <hr />

<strong>EDIT</strong>: You can reduce the number of mkdir to the minimum by adding the object directory as an <a href="https://www.gnu.org/software/make/manual/make.html#Prerequisite-Types" rel="nofollow">order only prerequisites</a>:

# Do not create the directory $(OBJECTS): $(OBJDIR)/%.o: %.c $(CC) -MMD -MP -c $< -o $@ # Every target finishing with "/" is a directory $(OBJDIR)%/: mkdir -p $@ # Add the "directory/" as an order only prerequisite $(foreach OBJECT,$(OBJECTS),$(eval $(OBJECT): | $(dir $(OBJECT))))

Recommend

  • Build PyQt5 on Windows with OpenSSL support?
  • makefile with mex-project: `.rodata' can not be used when making a shared object
  • How to build Openssl for Android on Windows with ndk8?
  • Why isn't my eval function in my Makefile working?
  • makefile multiple source file compilation rule in one step
  • How to compute the range in Makefile
  • Makefile Fatal error: can't create obj/calc.o
  • GTK+ compilation undefined reference C
  • ignoring ensurepip failure pip requires ssl/tls error in Ubuntu 18.04
  • ResultsController to another ResultsController
  • Can I use Preprocessor Directives in .ld file
  • dbus - how to set include paths
  • manupulating a string to create directories in unix
  • Using Netbeans with Cygwin and SDL, including SDL.h creates strange error
  • Android mkdirs() sdcard do not work
  • How to stamp out template in self contained custom elements with vanilla js?
  • File loader changed image file name but not the file name in HTML file
  • Is it better to use the “hidden” CSS attribute or fetch each set of new images?
  • MAVEN : Run Multiple Maven Project using Maven Test
  • Get a trait object reference from a vector
  • google maps autocomplete bounces back already cleared text …odd…odd…odd
  • Is there a way to dynamically embed PDF Files in a JSP pulled from the file system?
  • Outputting SharePoint Hyperlink Column as URL
  • How to load gif image while ajax content is loading and javascript [duplicate]
  • Doctrine/Symfony entity generator and generating entity from one table
  • Uncaught TypeError: $(…).select2 is not a function
  • How to view images from protected folder with php?
  • Using a canvas object in a thread to do simple animations - Java
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • Display images in Django
  • Resize panoramic image to fixed size
  • Importing jscolor library in angular 2
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • How to get Windows thread pool to call class member function?
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal