Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...