[英]Makefile to compile a list of sources to custom directory
I have this makefile:我有这个makefile:
IDIR = include
SDIR = src
ODIR = obj
BDIR = build
DEPS = $(shell find $(IDIR)/ -name '*.hpp')
SRCS = $(shell find $(SDIR)/ -name '*.cpp')
OBJS = $(patsubst %.cpp,$(ODIR)/%.o,$(notdir $(SRCS)))
BIN = main
CPPC = g++
CFLAGS = -Wall -c
all: dir $(BDIR)/$(BIN)
@echo Finished compiling $(BIN)
dir:
mkdir -p $(BDIR)
mkdir -p $(ODIR)
$(BDIR)/$(BIN): $(OBJS)
$(CPPC) $^ -o $@
$(OBJS): $(SRCS)
$(CPPC) $(CFLAGS) $^ -o $@
clean:
rm -rf $(BDIR) $(ODIR)
When I try to make, I get the following error:当我尝试制作时,出现以下错误:
mkdir -p build
mkdir -p obj
g++ -Wall -c src/sdk/tcp/Tcp.cpp src/sdk/socket/Socket.cpp src/Main.cpp -o obj/Tcp.o
g++: fatal error: cannot specify ‘-o’ with ‘-c’, ‘-S’ or ‘-E’ with multiple files
compilation terminated.
make: *** [Makefile:27: obj/Tcp.o] Error 1
My question is, is it possible to achieve what I am trying with this makefile?我的问题是,是否有可能用这个 makefile 来实现我想要的? Going through each source file in $(SRCS), and compile the object file directly inside the obj directory with just the basename.
遍历 $(SRCS) 中的每个源文件,并直接在 obj 目录中使用基本名称编译目标文件。 An example of obj directory after a successful compilation:
编译成功后的obj目录示例:
obj
/ | \
/ | \
Tcp.o Socket.o Main.o
Your $(OBJS)
rule is wrong.你的
$(OBJS)
规则是错误的。
There are (at least) two ways to do this.有(至少)两种方法可以做到这一点。
You could write a pattern rule and use vpath
to locate the sources:您可以编写一个模式规则并使用
vpath
来定位源:
vpath %.cpp $(dir $(SRCS))
$(OBJS): obj/%.o: %.cpp
$(CPPC) $(CFLAGS) $^ -o $@
Or you could generate a rule for each object:或者您可以为每个对象生成规则:
define template
$(patsubst %,obj/%.o,$(notdir $(1))): $(addsuffix .cpp,$(1))
echo $(CPPC) $(CFLAGS) $$^ -o $$@
endef
$(foreach src,$(SRCS),$(eval $(call template,$(basename $(src)))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.