Makefile:从目录编译到另一个目录 [英] Makefile: Compiling from directory to another directory
问题描述
我正在尝试使用Makefile编译位于src/code/*.cpp
中的一堆.cpp
文件,然后编译build/
中的每个*.o
,最后还生成与build/
中的文件相同的可执行文件.
I am trying to use Makefile to compile a bunch of .cpp
files located in src/code/*.cpp
, then compile each *.o
in build/
, and finally generate executable with those in build/
as well.
我已经阅读了一些我尝试使用的答案,但是遇到了我不理解的问题.
I have read a couple answers that I tried to work with but have encountered issues I do not understand.
CC = g++
FLAGS = -g -c
SOURCEDIR = /src/code
BUILDDIR = build
EXECUTABLE = DesktopSpecificController
SOURCES = $(wildcard src/code/*.cpp)
OBJECTS = $(patsubst src/code/*.cpp,build/%.o,$(SOURCES))
all: dir $(BUILDDIR)/$(EXECUTABLE)
dir:
mkdir -p $(BUILDDIR)
$(BUILDDIR)/$(EXECUTABLE): $(OBJECTS)
$(CC) $^ -o $@
$(OBJECTS): $(BUILDDIR)/%.o : $(SOURCEDIR)/%.cpp
$(CC) $(FLAGS) $< -o $@
clean:
rm -f $(BUILDDIR)/*o $(BUILDDIR)/$(EXECUTABLE)
我确实收到以下错误,但我不确定为什么:
I do get the following error, and I am not sure why:
Makefile:19: target `src/code/main.cpp' doesn't match the target pattern
我还看到,当尝试构建EXECUTABLE时,它没有使用.o文件,因此在这里看来我的规则是错误的.
I also see that when trying to build the EXECUTABLE, it is not using the .o files, so it seems my rule is wrong here.
推荐答案
您的patsubst
函数错误;您不能使用像*
这样的shell通配符.您想要:
Your patsubst
function is wrong; you can't use shell wildcard characters like *
. You want:
OBJECTS = $(patsubst $(SOURCEDIR)/%.cpp,$(BUILDDIR)/%.o,$(SOURCES))
此外,您应该在各处使用SOURCEDIR
和BUILDDIR
,而不仅仅是在某些地方使用(否则会出现不一致的地方).最后,您的SOURCEDIR值是错误的:它不应以我期望的/
开头:
Also you should be using SOURCEDIR
and BUILDDIR
everywhere, not just in some places (otherwise you'll get inconsistencies). And finally, your SOURCEDIR value is wrong: it should not start with /
I expect:
SOURCEDIR = src/code
SOURCES = $(wildcard $(SOURCEDIR)/*.cpp)
这篇关于Makefile:从目录编译到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!