简体   繁体   English

如何将 append 到 Makefile 中的变量列表?

[英]How to append to a list of variables in a Makefile?

I am trying to write a Makefile for the following project structure我正在尝试为以下项目结构编写 Makefile

/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我有 3 个包含 .cpp 和 .hpp 文件的文件夹,我想在 INC 变量中将所有 hpp 文件放在一起

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. INC 只抓取 Makefile 目录而不是子目录中的.hpp 文件。 How to automatically specify all headers in subdirectories?如何自动指定子目录中的所有标题? Thanks for tips.感谢您的提示。

The wildcard function takes several patterns. wildcard function 采用多种模式。 The addsuffix function appends a suffix to each word in a list.后缀addsuffix为列表中的每个单词附加一个后缀。 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) ):或者,如果您有find实用程序并想在任何深度的任何子目录中查找所有*.hpp文件(不仅仅是在$(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.最后一种形式可能会更好,因为您不需要提供目录列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM