使用makefile编写makefile中的依赖项 [英] Writing dependencies in makefile, with makefile
问题描述
根据一些SO问题 - 以及一些进一步的参考文献,我正在尝试构建一个makefile:
Based on some SO questions -- and some further reference found --, I'm trying to build a makefile able to:
- 找到
$(SRC)
中的目录,要编译的.cpp
文件; - 编译
.cpp
,产生.o
对象; - 从每个
.o
生成.so
共享对象。
- find, given the directories in
$(SRC)
, the.cpp
files to be compiled; - compile the
.cpp
, producing.o
objects; - generate
.so
shared objects from each.o
formerly compiled.
make文件应该做什么来实现:
What the make file is supposed to do to achieve that is:
- 找到
$(SRC)
中的目录,要编译的.cpp
文件; - 使用
-MM
编译器标志为每个.cpp
构建依赖性列表; - 使用
$(eval ...)
注释/添加每个依赖项; - 发现,生成
.o
和.so
文件。
- find, given the directories in
$(SRC)
, the.cpp
files to be compiled; - build the dependency list for each
.cpp
using-MM
compiler's flag; - annotate/add each dependency using
$(eval ...)
; - evaluate/solve each dependency found, producing both the
.o
and.so
files.
我到目前为止:
我已经能够做到这一切,除了使它工作(:
我得到的错误说明有一个空的标签''
作为依赖:
$ make make: * 没有规则要使目标
'需要
/ home / rubens /bin/label.o'。停止。
$ make make: * No rule to make target
', needed by
/home/rubens/bin/label.o'. Stop.
所以,这里是我还无法运行的makefile:
So, here is the makefile I'm yet not able to run:
# Directories
SRC := process init
BIN := $(HOME)/bin
LIB := watershed
LIBPATH := $(WATERSHED)/lib
INC := $(WATERSHED)/include $(XERCES)/include
# Paths
MPICPP := mpic++
SOURCES := $(shell find $(SRC) -name '*.cpp')
OBJECTS := $(addprefix $(BIN)/, $(notdir $(SOURCES:.cpp=.o)))
SHARED := $(OBJECTS:.o=.so)
# Flags
CFLAGS := -std=c++0x -O2 -Wall -fPIC
CFLAGS += $(foreach inc, $(INC), -I $(inc))
LFLAGS :=
LFLAGS += $(foreach lib, $(LIB), -l $(lib))
LFLAGS += $(foreach path, $(LIBPATH), -L $(lib))
# Rules
$(SHARED): | bindir
%.o:
@echo $@ -- [$<][$^][$*]
@echo $(MPICPP) $(CFLAGS) -c $< -o $@
%.so: %.o
@echo $(MPICPP) $(LFLAGS) -shared $< -o $@
bindir:
@mkdir -p $(BIN)
# Utilities
.PHONY: all clean
all: $(SHARED)
clean:
@rm -f $(OBJECTS) $(SHARED)
# Dependencies
define dependencies
$(addprefix $(BIN)/, $(notdir $(1:.cpp=.o))): \
$(shell $(MPICPP) $(CFLAGS) -MM $(1) | sed 's/^.*\.o:[ ]*//')
endef
$(foreach src, $(SOURCES), $(eval $(call dependencies, $(src))))
似乎是错误:
由于依赖生成步骤,当我删除任何空行,管道输出到 grep
,如下:
define dependencies
$(addprefix $(BIN)/, $(notdir $(1:.cpp=.o))): \
$(shell $(MPICPP) $(CFLAGS) -MM $(1) | sed 's/^.*\.o:[ ]*//' | \
grep -v ^$)
endef
错误以某种方式挖掘到一个空的依赖列表;我可以看到列表实际上是空的 echo
'在规则%。o:
唯一不为空的变量为 $ @
和 $ *
。
The error somehow digivolves to an empty list of dependencies; I'm able to see the list is actually empty with the echo
's in rule %.o:
-- the only variables not empty are $@
and $*
.
/home/rubens/bin/label.o - [] [] [/ home / rubens / bin / label]
/home/rubens/bin/label.o -- [][][/home/rubens/bin/label]
mpic ++ -std = c ++ 0x -O2 -Wall -fPIC -I / home / rubens / libwatershed / include -I /home/rubens/xerces-c-3.1.1/include -c -o / home / rubens / bin / label.o
mpic++ -std=c++0x -O2 -Wall -fPIC -I /home/rubens/libwatershed/include -I /home/rubens/xerces-c-3.1.1/include -c -o /home/rubens/bin/label.o
mpic ++ -l watershed -L -shared /home/rubens/bin/label.o -o /home/rubens/bin/label.so
mpic++ -l watershed -L -shared /home/rubens/bin/label.o -o /home/rubens/bin/label.so
任何有关更好的解决方法的建议都是非常非常受欢迎的;但是,在我使用 Cmake
或 Scons
之前,我真的想写完这个makefile。 / p>
Any advice on better ways to solve this are very, very welcome; yet, I would really like to finish writing this makefile, before I get to use something like Cmake
or Scons
.
推荐答案
-MM已经生成了对makefile格式的依赖,所以你可以通过生成每个.cpp的依赖关系同样的文件,然后只是做一个-include $(DEPS_FILE)。这可能比使用eval更易于维护。
-MM will already generate the dependencies on a makefile format so you could simplify this by generating the dependencies for each .cpp into a same file, then just doing an "-include $(DEPS_FILE)". That's probably more maintainable than using eval.
这篇关于使用makefile编写makefile中的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!