如何从range()函数获取最后一个数字?(How do I get the last number from the range() function?)
有没有办法从
range()
函数中获取最后一个数字? 我需要在前20个术语中得到Fibonacci序列中的最后一个数字,还是应该使用列表而不是range()
?Is there a way to get the last number from the
range()
function? I need to get the last number in a Fibonacci sequence for first 20 terms or should I use a list instead ofrange()
?
原文:https://stackoverflow.com/questions/780057
最满意答案
我找到了解决方案。 我的完整规则太复杂了,所以我发布了简化代码来解释我的解决方案。
首先,我们有一个规则创建一个文件夹,并在同一个命令中使用
$(shell ...)
:all: mkdir -p $(PREFIX) && \ cd $(PREFIX) && \ cat $(PREFIX)/../$(INPUT) > $(shell cat $(PREFIX)/../$(INPUT) | awk -F "=" '/LoaderDir/ {print $$2}' | tr -d ' ')/./loaders.cache touch $@ @echo [DONE]
如果之前没有创建文件夹,这可能会失败。 在这种情况下,
$(shell ...)
将展开为空字符串。 cat命令将失败。 我们在这里看到,这不是一个扩展问题。 这是一个计时问题。解决方案是将此规则分为两部分:
all_pass1: mkdir -p $(PREFIX) && \ touch $@ @echo [DONE] all_pass2: all_pass1 cd $(PREFIX) && \ cat $(PREFIX)/../$(INPUT) > $(shell cat $(PREFIX)/../$(INPUT) | awk -F "=" '/LoaderDir/ {print $$2}' | tr -d ' ')/./loaders.cache touch $@ @echo [DONE]
好的,我有一个解决方案。 但我想知道为什么我的第一个makefile工作,第二个开始第一次失败。 这就是我发布这个问题的原因。 这在我看来仍然像一些GNU魔法。
对我来说,它已经解决了,但如果您认为有更好的解决方案,请随时发表评论或提出更好的解决方案。
I found a solution. My full rule is too complex so I post simplified code to explain my solution.
First we have a rule that creates a folder and uses
$(shell ...)
in the same command:all: mkdir -p $(PREFIX) && \ cd $(PREFIX) && \ cat $(PREFIX)/../$(INPUT) > $(shell cat $(PREFIX)/../$(INPUT) | awk -F "=" '/LoaderDir/ {print $$2}' | tr -d ' ')/./loaders.cache touch $@ @echo [DONE]
This can fail, if the folder was not created before. In that case
$(shell ...)
will expand to an empty string. And the cat command will fail. We see here that this is not an expansion problem. It's a timing problem.The solution is to break this rule into two parts:
all_pass1: mkdir -p $(PREFIX) && \ touch $@ @echo [DONE] all_pass2: all_pass1 cd $(PREFIX) && \ cat $(PREFIX)/../$(INPUT) > $(shell cat $(PREFIX)/../$(INPUT) | awk -F "=" '/LoaderDir/ {print $$2}' | tr -d ' ')/./loaders.cache touch $@ @echo [DONE]
Ok I have a solution. But I wonder why my first makefile worked and the second that started the first fail. And that's the reason why I posted this problem. That still seems to me like some GNU make magic.
For me it's solved, but feel free to comment or present a better solution, if you think there is a better one.
相关问答
更多-
你可以给像makefile.win和makefile.nix这样的文件提供明智的名字并使用它们: make -f makefile.win make -f makefile.nix 或者有一个包含以下内容的Makefile: win: make -f makefile.win nix: make -f makefile.nix 并使用make win或make nix You can give sensible names to the files like makefile.win and ...
-
为什么patsubst在使用$$ *的二次扩展时停止工作?(Why does patsubst stop working when using secondary expansion of $$*?)[2021-12-18]
百分比字符正在通过与词干中的通配符相匹配而被读取,并正在被词干匹配替换。 如果您检查您的示例的make -p输出,您会看到解析的目标行如下所示: %.a: $(patsubst %.c,%.o,$($*_SRCS)) 就制作而言,它只是一组非常奇怪的图案化目标(或类似的东西)。 如果您以类似的方式从make解析中跳过百分比字符,那么您可以得到您想要的工作: pc := % $$(patsubst $$(pc).c,$$(pc).o,$$($$*_SRCS)) 对于附加信息替换引用 (例如$(foo_SR ... -
Makefiles和通配符(Makefiles and wildcards)[2021-10-12]
简而言之 ,该语法不能按照您希望的方式工作。 在GNU make语法中做正确的方法是使用模式规则: public%.o: public%.c hashtable.h $(CC) $(CFLAGS) -c $< public%: public%.o $(CC) -o $@ $< 长答案 :这个: public*.o: public*.c hashtable.h 并不意味着你想要它的意思。 假设您在构建开始时有几个文件public01.c等,并且没有文件public01.o ... -
了解Makefiles(understanding Makefiles)[2022-08-03]
因为您的目标文件显然是从.cpp文件构建的。 您没有明确的规则来构建.cpp文件中的.o文件,因此Make使用隐式规则 $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c 。 Because your object files are apparently built from .cpp files. You have no explicit rule for building .o files from .cpp files, so Make uses the implicit rule ... -
我找到了解决方案。 我的完整规则太复杂了,所以我发布了简化代码来解释我的解决方案。 首先,我们有一个规则创建一个文件夹,并在同一个命令中使用$(shell ...) : all: mkdir -p $(PREFIX) && \ cd $(PREFIX) && \ cat $(PREFIX)/../$(INPUT) > $(shell cat $(PREFIX)/../$(INPUT) | awk -F "=" '/LoaderDir/ {print $$2}' | tr -d ' ' ...
-
替代GNU的makefile中的二次扩展make 3.79.1(Alternative to secondary expansion in a makefile for GNU make 3.79.1)[2022-04-21]
只需在源代码树中添加make-3.81源代码即可。 您的主Makefile首先应使用任何可用的make编译make-3.81 ,然后使用make-3.81编译其他所有内容。 Just add make-3.81 sources in your source tree. Your main Makefile first should compile make-3.81 using whatever make is available and then use make-3.81 for compiling e ... -
它可能是这样的: floppy: picoc command to make floppy picoc: make -C directory/of/picoc all .PHONY : picoc 这更适用于指南然后完全解决方案,因为我不确定我是否理解您的项目结构。 It could be something along the lines: floppy: picoc command to make floppy picoc: make -C directory/of ...
-
任何方式,我告诉你一些事情,希望可以帮助你: all: @$(MAKE) -C subfolder 或者如果你想使用* .mk作为你的子makefile,你可以写: all: @$(MAKE) -C busfolder -f somename.mk 我向你展示我的例子,我的DIR看起来像: TOPDIR-- Makefile | |-- debug | |-- debug.c | |-- debug.h | |-- debug.mk | |-- instrument.c ...
-
我不知道为什么$$<以这种方式扩展。 您可以使用$$*来改变配置,但是: %.result: jobs/% $$(strip $$(call get-job,jobs/$$*)) echo $^ 您需要添加strip调用,以便将call结果中嵌入的换行符转换为空格。 I don't know why $$< expands that way. You can make your configuration work by using $$* instead, though: %.resul ...
-
Makefile辅助扩展(Makefile Secondary Expansion)[2022-08-20]
该手册具有误导性,可能是由于拼写错误。 这两句话中的第一句: $$ <变量计算为此目标的第一个规则中的第一个先决条件。 $$ ^和$$ +计算已经为同一目标出现的规则的所有先决条件列表($$ +重复,$$ ^没有)。 应该像第二个一样,通过附加“已经出现的”字样来证明。 您可以通过运行两个makefile来验证这是否正确描述了该行为: 制作1 .SECONDEXPANSION: foo: foo.1 $$< @echo $+ 输出是: foo.1 显示$$<已扩展为空字符串。 然后: 制作2 . ...