Home > Backend Development > C++ > How to Build Multiple Executables with Similar Rules Using GNU Make?

How to Build Multiple Executables with Similar Rules Using GNU Make?

Linda Hamilton
Release: 2024-10-28 14:56:02
Original
940 people have browsed it

 How to Build Multiple Executables with Similar Rules Using GNU Make?

Building Multiple Executables with Similar Rules Using GNU Make

While Scons is a capable build tool, implementing the desired functionality can be challenging. A more straightforward approach is to utilize GNU Make, which allows for easy building and cleaning from both top-level and individual project directories.

Makefile Setup

The provided makefiles enable building and cleaning from both the all_lessons directory and individual project directories. Each project's executable is named after its directory.

Project Structure

To achieve this, you will need to set up a project structure similar to the example provided:

all_lessons/
    helloworld/
        lesson.cpp
        main.cpp
    even_or_odd/
        lesson.cpp
        main.cpp
    calculator/
        lesson.cpp
        user_created_add.cpp
        main.cpp
Copy after login

Makefile Contents

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

# project configuration

project_dirs := $(shell find * -maxdepth 0 -type d )
exes := $(foreach dir,${project_dirs},${dir}/${dir})

all : ${exes}

# 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 dependency files
-include $(foreach dir,${project_dirs},$(wildcard ${dir}/*.d))
Copy after login

Usage

Building from Individual Project Directories

[project_directory]$ ln -s ../project.mk Makefile  # create a symlink
[project_directory]$ make
Copy after login

Building from the Top-Level Directory

[all_lessons]$ make
Copy after login

Cleaning Individual Project Directories

[project_directory]$ cd ..
[all_lessons]$ make clean
Copy after login

Cleaning All Projects

[all_lessons]$ make clean
Copy after login

The above is the detailed content of How to Build Multiple Executables with Similar Rules Using GNU Make?. 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