使用 Gnu Make 实用程序自动创建具有与源代码不同的构建结构的文件
我见过以下技术用于自动编译特定目录中的所有 C(或 C++,或者我想您想要的任何扩展名)文件。
SOURCE_DIRS = .
SOURCES := $(subst ./,,$(wildcard $(SOURCE_DIRS:=/*.cpp)))
HEADERS := $(subst ./,,$(wildcard $(SOURCE_DIRS:=/*.h)))
OBJECTS := $(addprefix build/,$(SOURCES:.cpp=.o))
CXX = g++
CXXFLAGS = -Wall -pedantic -g
#LDFLAGS =
all: build/exe_name
build/exe_name: $(OBJECTS)
$(CXX) -o $@ $(OBJECTS) $(LDFLAGS)
build/%.o: %.cpp $(HEADERS) Makefile
mkdir -p $(dir $@)
$(CXX) -o $@ $(CXXFLAGS) -c $<
唯一的问题是它以与源目录中相同的结构构建所有文件。因此,如果您有一个包含代码的子文件夹,那么构建将有一个匹配的子文件夹。
那么,我如何修改它(或者还能做什么),以使所有输出的目标直接放置在 build/ 目录中,而不是放置在与源目录匹配的层次结构中?
我考虑过尝试删除在目标中使用“%”时可能出现的任何目录,但我找不到任何好的命令来做到这一点。另外,尝试在创建时更改 OBJECTS 路径似乎有点问题,因为我不知道“%”变量如何读取它,除了它神奇地编译 OBJECTS 列表中的所有内容之外。
感谢您提供的任何帮助。
I've seen the following technique used to automatically compile all of the C (or C++, or I suppose whatever extension you wanted) files in a particular directory.
SOURCE_DIRS = .
SOURCES := $(subst ./,,$(wildcard $(SOURCE_DIRS:=/*.cpp)))
HEADERS := $(subst ./,,$(wildcard $(SOURCE_DIRS:=/*.h)))
OBJECTS := $(addprefix build/,$(SOURCES:.cpp=.o))
CXX = g++
CXXFLAGS = -Wall -pedantic -g
#LDFLAGS =
all: build/exe_name
build/exe_name: $(OBJECTS)
$(CXX) -o $@ $(OBJECTS) $(LDFLAGS)
build/%.o: %.cpp $(HEADERS) Makefile
mkdir -p $(dir $@)
$(CXX) -o $@ $(CXXFLAGS) -c lt;
The only problem is that it builds all of the files in the same structure as is in the source directory. So if you had a sub folder with code in it, than the build would have a matching sub folder.
So, how can I modify this (or what else can be done), to get all of the outputted targets to be placed directly in the build/ directory, rather than in a hierarchy that matches the source directory?
I've thought about trying to strip any directories that may be presented in the '%' when it's used in the target, but I can't find any good commands to do that. Also, it seems a bit problematic to try to change the OBJECTS path when created, as I have no idea how the '%' variable reads it, other than that it is magically compiling everything in the OBJECTS list.
Thank you for any help you can give.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按照顺序,
In order,
最简单的(尽管不是严格意义上的 make-only)方法是使用 automake 并且不设置 subdir-options 选项。
The easiest (although not strictly make-only) way would be to use
automake
and not set thesubdir-options
option.