【问题标题】:GNU make sometime doesn't search in folders declared in VPATHGNU make 有时不会在 VPATH 中声明的文件夹中搜索
【发布时间】:2013-07-18 22:25:02
【问题描述】:

我正在尝试使用 GNU make(版本 3.81)构建一个小型 C++ 项目,但我必须调用 make 两次,因为第一次运行失败。这是我的项目目录:

project
    makefile
    include
        lexer.hpp   
    src
        main.cpp
        lexer.l

以下是我的makefile:

CC = g++
CPPFLAGS = -I include
VPATH = include src

OBJECTS = main.o lexer.o

test: $(OBJECTS)
$(CC) $(CPPFLAGS) -lfl -o $@ $^

main.o: lexer.hpp main.cpp
    $(CC) -c $(CPPFLAGS) $^

lexer.o: lexer.cpp
    $(CC) -c $(CPPFLAGS) $^

lexer.cpp: lexer.l
    flex -t $^ > src/lexer.cpp

.PHONY: clean

clean:
    rm -fR $(OBJECTS) src/lexer.cpp test

我第一次运行 make 时得到以下输出,其中 make 抱怨找不到 lexer.cpp 文件。但我不明白为什么 make 不在 VPATH 中声明的文件夹中寻找。

g++ -c -I include include/lexer.hpp src/main.cpp
flex -t src/lexer.l > src/lexer.cpp
g++ -c -I include lexer.cpp
g++: error: lexer.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.
make: *** [lexer.o] Error 1

但是,如果我再次调用 make,则会找到 lexer.cpp 并且编译工作正常。

g++ -c -I include src/lexer.cpp
g++ -I include -lfl -o test main.o lexer.o

为什么?

附:我为糟糕的英语道歉。

【问题讨论】:

    标签: c++ makefile gnu-make flex-lexer


    【解决方案1】:

    这条规则是错误的:

    lexer.cpp: lexer.l
            flex -t $^ > src/lexer.cpp
    

    这条规则告诉 make 它将构建一个文件 lexer.cpp,这就是 make 期望它做的事情,并且在规则完成后,make 认为该文件已准备好运行,并且它将在其他文件时使用该文件名目标取决于它。但该规则的真正作用是构建src/lexer.cpp

    要正确编写此规则,您需要将其编写为:

    src/lexer.cpp: lexer.l
            flex -t $^ > $@
    

    (您编写的每个 make 规则都应始终准确地更新文件 $@)。

    但是,通常 VPATH 不适合查找生成的文件(目标文件等:任何由 make 生成的文件)。它仅对查找源文件有用(文件 make 不会自行构建)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多