简体   繁体   English

Makefile循环依赖

[英]Makefile circular dependency

Here is my Makefile: 这是我的Makefile:

.PHONY: all homework1
CFLAGS= -g -O0 -Wall -Werror -Wno-unused-function
LDFLAGS= -lm

all : homework1

homework1 : program.tab.o program.lex.o
%.o : %.c
    gcc -o$@ -c $(CFLAGS) $<
%.lex.c : %.lex %.tab.h
    flex -o$@ $<
%.tab.c %.tab.h : %.y
    bison --verbose -o$@ -d $<

Whenever I try to compile, I get the warning make: Circular program.lex <- program.lex.o dependency dropped. 每当我尝试编译时,我都会收到警告make: Circular program.lex <- program.lex.o dependency dropped. I don't see how program.lex is dependent on program.lex.o at all in the makefile. 我看不出program.lex在makefile中是如何依赖program.lex.o的。 I see how the dependency tree is about 4 layers deep, but it doesn't look circular. 我看到依赖树是如何约4层深,但它看起来不是圆形的。

How can I improve my makefile? 如何改进我的makefile?

The trouble is that there is an implicit rule in Make (well, in GNUMake anyway) for linking a single object file to build en executable. 麻烦的是Make中有一个隐含的规则(好吧,无论如何,在GNUMake中),用于链接单个目标文件以构建可执行文件。 Your makefile says nothing about how to build program.lex , so Make falls back on the implicit rule to build it from program.lex.o . 你的makefile没有说明如何构建program.lex ,所以Make依赖于隐式规则来从program.lex.o构建它。

Since your your layout seems to depend on having program.lex to begin with, you can suppress the implicit rule by adding your own rule for program.lex (which does nothing): 由于你的布局似乎依赖于开始使用program.lex ,你可以通过为program.lex添加自己的规则来禁止隐式规则(它什么都不做):

program.lex:;

run make with -d: 用-d运行make:

Considering target file `all'.
 File `all' does not exist.
  Considering target file `homework1'.
   File `homework1' does not exist.
    Considering target file `program.lex.o'.
     File `program.lex.o' does not exist.
     Looking for an implicit rule for `program.lex.o'.
     Trying pattern rule with stem `program.lex'.
     Trying implicit prerequisite `program.lex.c'.
     Found an implicit rule for `program.lex.o'.
      Considering target file `program.lex.c'.
       Looking for an implicit rule for `program.lex.c'.
       Trying pattern rule with stem `program'.
       Trying implicit prerequisite `program.lex'.
       Found an implicit rule for `program.lex.c'.
        Considering target file `program.lex'.
         Looking for an implicit rule for `program.lex'.
         Trying pattern rule with stem `program.lex'.
         Trying implicit prerequisite `program.lex.o'.
         Found an implicit rule for `program.lex'.
make: Circular program.lex <- program.lex.o dependency dropped.

there is implicit link rule which gets invoked, not lex I originally put down 有隐含的链接规则被调用,而不是我最初放下的lex

get rid off ".lex.*" extensions 摆脱“.lex。*”扩展

@aaa carp is correct that is an implicit rule, but it's not one of the LEX rules, because they build Nc or Nr from Nl , and this is a rule that's building N from No . @aaa carp是正确的,这是一个隐含的规则,但它不是LEX规则之一,因为它们从Nl构建NcNr ,这是一个从No构建N的规则。 That looks to me like the "Linking a single object file" rule, described in info (make) Catalogue of Rules . 在我看来,像“链接单个目标文件”规则,在info (make) Catalogue of Rules描述。 Renaming your .lex files to .l files will fix this problem. .lex文件重命名为.l文件将解决此问题。

Also: running flex , I don't think you really need the .tab.h file to build the lexer source. 另外:运行flex ,我认为你真的不需要.tab.h文件来构建lexer源代码。 Try this instead: 试试这个:

%.l.c: %.l
    flex -o$@ $<
program.l.o: program.tab.h

This will add the extra dependency to program.lo . 这将为program.lo添加额外的依赖项。

My immediate reaction was that you need an action after the rule that says homework1: program.tab.o program.lex.o : 我的直接反应是你需要在homework1: program.tab.o program.lex.o规则之后执行一个操作homework1: program.tab.o program.lex.o

 homework1:  program.tab.o program.lex.o
         ${CC} -o $@ program.tab.o program.lex.o ${LDFLAGS}

However, since you've also marked homework1 as .PHONY , maybe you are not ready to link the program yet. 但是,由于你还将homework1标记为.PHONY ,也许你尚未准备好连接程序。 But your problem reproduces... 但是你的问题再现了......

Conventionally, the source for Lex (or Flex) is kept in a .l file, not in a .lex file, so I changed the makefile to: 通常,Lex(或Flex)的源代码保存在.l文件中,而不是.lex文件中,因此我将makefile更改为:

.PHONY: all homework1
CFLAGS= -g -O0 -Wall -Werror -Wno-unused-function
LDFLAGS= -lm

all : homework1

homework1 : program.tab.o program.lex.o

%.o : %.c
    gcc -o $@ -c $(CFLAGS) $<
%.lex.c : %.l %.tab.h
    flex -o $@ $<
%.tab.c %.tab.h : %.y
    bison --verbose -o $@ -d $<

The warning message goes away. 警告信息消失了。 (I created your environment by using ' touch program.lex program.y ' initially; I then moved program.lex to program.l ). (我最初使用' touch program.lex program.y '来创建你的环境;然后我将program.lex移动到program.l )。

However, I'm still not quite sure what is going on in the broken version - but it probably does revolve around implicit rules as suggested by @Beta. 但是,我仍然不太确定破碎版本中发生了什么 - 但它可能确实围绕@Beta建议的隐式规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM