Home > Backend Development > C++ > body text

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
Release: 2024-10-27 16:07:02
Original
877 people have browsed it

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_
Copy after login

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))
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!