English 中文(简体)
在一张纸质档案中,我如何在每份档案名称上以变数执行指挥?
原标题:In a makefile, how do I execute a command on each file name in variable?
  • 时间:2009-10-16 06:21:50
  •  标签:

我知道我这样做是错误的,但我可以说明如何组织这一文件。 我界定了我的“利用源”档案,并利用一些职能来界定这些档案:

UTIL_SRC = utils/src/foo.cpp utils/src/bar.cpp utils/src/baz.cpp

UTIL_OBJS = $(patsubst utils/src/%.cpp,utils/obj/%.o,$(UTIL_SRC))

这是我使用这些档案用于:

lib : lib/libutils.a

lib/libutils.a : $(UTIL_OBJS)
    rm -f lib/libutils.a
    ar -c -q lib/libutils.a $(UTIL_OBJS)

然后,当我接受将这些婴儿编成册的规则时,我只想有一个指挥部,通过每个UTIL_来播种。 OBJS 案和每个UTIL_SRC案。 相反,我诉诸了这种霸权主义,这违背了将其储存在变数中的目的。

$(UTIL_OBJS) : $(UTIL_SRC)
    g++ $(UTIL_FLAGS) utils/src/foo.cpp -o utils/obj/foo.o
    g++ $(UTIL_FLAGS) utils/src/bar.cpp -o utils/obj/bar.o
    g++ $(UTIL_FLAGS) utils/src/baz.cpp -o utils/obj/baz.o

我能否把这推向一条线? 如何? 感谢你们!

最佳回答

采用隐含规则通常比较容易。 有许多预先界定的变量,其中你只需要确定变量。

CXX=g++
CXXFLAGS=$(UTIL_FLAGS)

然后,你们需要界定可起诉的定义,就像这一点一样。

myutil: $(UTIL_OBJS)

由于你不再把物体放在同一个目录中,你需要明确新的隐含规则,尽管(否则,我们现在就应该这样做)。

utils/obj/%.o: utils/obj/%.cpp

% is a pattern-match, it ll match the same text on both left and right side, so this rule will make foo.o out of foo.cpp. Try if that ll work without the command (it might have grabbed that from another rule, I m not sure), otherwise let it say:

utils/obj/%.o: utils/obj/%.cpp
    $(CXX) $(CXXFLAGS) -o $@ $^

$@ is the target of the rule (e.g. foo.o), and $^ is all files on the right hand side. I m writing this off the top of my head, without the possibility to test it, so please let me know how it turned out.. :)

为了使之更加合法,你可以包括扶养档案。

include .depend

如果你重新执政,如果能够找到独立档案的话(如果旧学校愿意的话,你需要首先创建独立档案,那么它就只能是一种 du笑,但如果你想通过档案来管理它的话。

.depend: $(UTIL_SRC)
    $(CXX) -MM -o $@ $^

扶养档案将载有每个申请档案的线索,说明它需要哪些主人档案,这样就可以在你改变情况时重新整理必要的档案。 这与你原来的问题毫不相干。

EDIT:
As a response to your edit. You could probably drop the commands for creating the .a-file as well, that too is already available as an implicit rule. Not sure exactly how it works though, haven t used it much. I do know that there are a bunch of quirks in make for dealing with .a(rchive?)-files.

问题回答

我认为你可以这样做:

$(UTIL_OBJS) : $(UTIL_SRC)
    g++ $(UTIL_FLAGS) $(@ : .o = .cpp)  -o $@ 

我再说一遍,我不敢肯定......。





相关问题
热门标签