如何在 C++ 中跨多个文件夹使用 makefile
How to use a makefile across multiple folders in C++
我有一个相当大的程序,所以我使用 makefile 来编译程序。但是,我想将程序的各个部分分成不同的文件夹,而不是将整个程序放在一个文件夹中。
Root folder with makefile and other folders
Folder inside of root folder with files
基本上我有一个跨多个文件的程序,我想将其编译成一个可执行文件。我不知道如何将这一切添加到 makefile 中,所以如果有人可以向我解释一下,我将不胜感激。谢谢。
您可以让根 makefile 调用文件夹内的 makefile。
示例:
# The list of your directories under the root directory:
SUBDIRS := foo bar baz
.PHONY: build all test subdirs $(SUBDIRS)
.SUFFIXES:
all: subdirs
subdirs: $(SUBDIRS)
# for each sub directory, do "make -C the_directory"
$(SUBDIRS):
$(MAKE) -C $@
clean:
@$(foreach dir,$(SUBDIRS),$(MAKE) -C$(dir) clean;)
您的 all
子文件夹中的目标随后可以调用下一级子文件夹中的 make 文件,如果您愿意,也可以构建实际目标。
我有一个相当大的程序,所以我使用 makefile 来编译程序。但是,我想将程序的各个部分分成不同的文件夹,而不是将整个程序放在一个文件夹中。
Root folder with makefile and other folders
Folder inside of root folder with files
基本上我有一个跨多个文件的程序,我想将其编译成一个可执行文件。我不知道如何将这一切添加到 makefile 中,所以如果有人可以向我解释一下,我将不胜感激。谢谢。
您可以让根 makefile 调用文件夹内的 makefile。
示例:
# The list of your directories under the root directory:
SUBDIRS := foo bar baz
.PHONY: build all test subdirs $(SUBDIRS)
.SUFFIXES:
all: subdirs
subdirs: $(SUBDIRS)
# for each sub directory, do "make -C the_directory"
$(SUBDIRS):
$(MAKE) -C $@
clean:
@$(foreach dir,$(SUBDIRS),$(MAKE) -C$(dir) clean;)
您的 all
子文件夹中的目标随后可以调用下一级子文件夹中的 make 文件,如果您愿意,也可以构建实际目标。