Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

c++ - How to append to a list of variables in a Makefile?

I am trying to write a Makefile for the following project structure

/project
  Makefile
  main.cpp
  test.hpp
  /a
    s1.cpp 
    s1.hpp 
    s2.cpp 
    s2.hpp
  /b
    s1.cpp 
    s1.hpp 
    s2.cpp 
    s2.hpp
  /c
    s1.cpp 
    s1.hpp 
    s2.cpp 
    s2.hpp

I have 3 folders with .cpp and .hpp files, I'd like to have in the INC variable all the hpp files together

DIRS = ./a ./b ./c
INC = $(wildcard *.hpp) # how to have here ./a/s1.hpp ./a/s2.hpp ./b/s1.hpp ./b/s2.hpp etc...?

INC only grabs the .hpp files in the Makefile directory and not in the subdirectories. How to automatically specify all headers in subdirectories? Thanks for tips.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The wildcard function takes several patterns. The addsuffix function appends a suffix to each word in a list. So:

INC = $(wildcard $(addsuffix /*.hpp,$(DIRS)))

should do what you want. There are other ways like:

INC = $(foreach d,$(DIRS),$(wildcard $(d)/*.hpp))

or, if you have the find utility and want to find all *.hpp files, in any subdirectory at any depth (not just in $(DIRS)):

INC = $(shell find . -type f -name '*.hpp')

This last form may be better because you do not need to provide the list of directories.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...