Home  >  Article  >  Backend Development  >  Can GNU Make streamline building numerous executables with similar rules across a directory structure, allowing compilation from both the main directory and individual project folders?

Can GNU Make streamline building numerous executables with similar rules across a directory structure, allowing compilation from both the main directory and individual project folders?

Linda Hamilton
Linda HamiltonOriginal
2024-10-27 16:07:02878browse

Can GNU Make streamline building numerous executables with similar rules across a directory structure, allowing compilation from both the main directory and individual project folders?

Building Multiple Executables with Similar Rules Using GNU Make

Question:

Can GNU Make facilitate building multiple executables with similar rules in a directory structure, enabling compilation from both the main directory and individual project directories?

Answer:

Yes, GNU Make can handle this task with a concise and efficient approach. Below are two makefiles that implement the desired functionality:

project.mk:

all :
% : forward_ # build any target by forwarding to the main makefile
    $(MAKE) -C .. project_dirs=$(notdir ${CURDIR}) $@
.PHONY : forward_

Makefile:

# one directory per project, one executable per directory
project_dirs := $(shell find * -maxdepth 0 -type d )

# executables are named after its directory and go into the same directory
exes := $(foreach dir,${project_dirs},${dir}/${dir})

all : ${exes}

#  the rules

.SECONDEXPANSION:

objects = $(patsubst %.cpp,%.o,$(wildcard $(dir )*.cpp))

# link
${exes} : % : $$(call objects,$$*) Makefile
    g++ -o $@ $(filter-out Makefile,$^) ${LDFLAGS} ${LDLIBS}

# compile .o and generate dependencies
%.o : %.cpp Makefile
    g++ -c -o $@ -Wall -Wextra ${CPPFLAGS} ${CXXFLAGS} -MD -MP -MF ${@:.o=.d} $<

.PHONY: clean

clean :
    rm -f $(foreach exe,${exes},$(call objects,${exe})) $(foreach dir,${project_dirs},$(wildcard ${dir}/*.d)) ${exes}

# include auto-generated dependency files
-include $(foreach dir,${project_dirs},$(wildcard ${dir}/*.d))

Usage:

  • Create symbolic links named Makefile to project.mk in each project directory.
  • To build a specific project, navigate to the project directory and run make.
  • To build all projects, run make from the main directory (all_lessons).
  • To clean a specific project, run make clean from the project directory.
  • To clean all projects, run make clean from the main directory.

Advantages:

  • Easy to implement and suitable for the specified requirements.
  • Supports building and cleaning from both the main directory and project directories.
  • Automatically generates dependencies and is fully parallelizable.

The above is the detailed content of Can GNU Make streamline building numerous executables with similar rules across a directory structure, allowing compilation from both the main directory and individual project folders?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn