【问题标题】:GNU Make producing a totally different resultGNU Make 产生完全不同的结果
【发布时间】:2012-12-20 03:31:58
【问题描述】:

这令人困惑。我有我的 Makefile:

OBJECTS =
INCLUDE_BUILD_PATH = /Users/wen/Projects/include

# Change compilation settings here
COMPILE = g++
override COMPILE_FLAGS += -O2

# Change linker/compiler specific settings here
LD_FLAGS :=
CC_FLAGS := -c -I$(INCLUDE_BUILD_PATH)/bigint

# Add source extensions here
SRC_EXT = cpp cc

# Add header dependencies here
HEADERS = $(wildcard *.hpp) $(wildcard $(INCLUDE_BUILD_PATH)/*/*.hh)

# Add source files here
CC_FILES = $(wildcard *.cpp) $(wildcard $(INCLUDE_BUILD_PATH)/*/*.cc)
CC_O_BUFFER = $(CC_FILES)
CC_O_BUFFER := $(CC_O_BUFFER:.cpp=.o)
CC_O_BUFFER := $(CC_O_BUFFER:.cc=.o)
OBJECTS = $(CC_O_BUFFER)

# Change .exe name here
EXE_NAME = maketest

# Link object files

$(EXE_NAME): $(OBJECTS)
    $(COMPILE) $(COMPILE_FLAGS) $(LD_FLAGS) -o $@ $^

# Build source files

define compile_rule
%.o : %.$1
        $$(COMPILE) $$(COMPILE_FLAGS) $$(CC_FLAGS) -o $$@ $$<
endef
    $(foreach EXT,$(SRC_EXT),$(eval $(call compile_rule,$(EXT))))

# Clean

clean:
    rm -f $(OBJECTS) $(EXE_NAME)

# Debug Build

debug:
    @echo "Rerun with COMPILE_FLAGS=-D_DEBUG"

# Print variables

print:
    @echo $(CC_FILES)
    @echo $(OBJECTS)
    @echo $(HEADERS)

一开始编译成功,后来无故停止,输出如下:

Yoshi-Air:maketest wen$ make
c++    -c -o maketest.o maketest.cpp
maketest.cpp:4:10: fatal error: 'BigIntegerLibrary.hh' file not found
#include "BigIntegerLibrary.hh"
         ^
1 error generated.

问题是,我什至没有告诉它在 Makefile 中使用“c++”,而是使用“g++”。此外,当我清除 CC_FLAGS 时,-c 仍然存在。就像 Make 有自己的想法一样。

如果我使用make print 打印出我的变量,它似乎没问题:

maketest.cpp /Users/wen/Projects/include/bigint/BigInteger.cc /Users/wen/Projects/include/bigint/BigIntegerAlgorithms.cc /Users/wen/Projects/include/bigint/BigIntegerUtils.cc /Users/wen/Projects/include/bigint/BigUnsigned.cc /Users/wen/Projects/include/bigint/BigUnsignedInABase.cc
maketest.o /Users/wen/Projects/include/bigint/BigInteger.o /Users/wen/Projects/include/bigint/BigIntegerAlgorithms.o /Users/wen/Projects/include/bigint/BigIntegerUtils.o /Users/wen/Projects/include/bigint/BigUnsigned.o /Users/wen/Projects/include/bigint/BigUnsignedInABase.o
maketest.hpp /Users/wen/Projects/include/bigint/BigInteger.hh /Users/wen/Projects/include/bigint/BigIntegerAlgorithms.hh /Users/wen/Projects/include/bigint/BigIntegerLibrary.hh /Users/wen/Projects/include/bigint/BigIntegerUtils.hh /Users/wen/Projects/include/bigint/BigUnsigned.hh /Users/wen/Projects/include/bigint/BigUnsignedInABase.hh /Users/wen/Projects/include/bigint/NumberlikeArray.hh

我们将不胜感激任何帮助或建议。谢谢!

更新

我更新了我的print 以打印预期的编译执行:

print:
    @echo $(CC_FILES)
    @echo $(OBJECTS)
    @echo $(HEADERS)
    @echo "Compiles with:"
    @echo $(COMPILE) $(COMPILE_FLAGS) $(LD_FLAGS) $(CC_FLAGS)

结果:

maketest.cpp /Users/wen/Projects/include/bigint/BigInteger.cc /Users/wen/Projects/include/bigint/BigIntegerAlgorithms.cc /Users/wen/Projects/include/bigint/BigIntegerUtils.cc /Users/wen/Projects/include/bigint/BigUnsigned.cc /Users/wen/Projects/include/bigint/BigUnsignedInABase.cc
maketest.o /Users/wen/Projects/include/bigint/BigInteger.o /Users/wen/Projects/include/bigint/BigIntegerAlgorithms.o /Users/wen/Projects/include/bigint/BigIntegerUtils.o /Users/wen/Projects/include/bigint/BigUnsigned.o /Users/wen/Projects/include/bigint/BigUnsignedInABase.o
maketest.hpp /Users/wen/Projects/include/bigint/BigInteger.hh /Users/wen/Projects/include/bigint/BigIntegerAlgorithms.hh /Users/wen/Projects/include/bigint/BigIntegerLibrary.hh /Users/wen/Projects/include/bigint/BigIntegerUtils.hh /Users/wen/Projects/include/bigint/BigUnsigned.hh /Users/wen/Projects/include/bigint/BigUnsignedInABase.hh /Users/wen/Projects/include/bigint/NumberlikeArray.hh
Yoshi-Air:maketest wen$ make print
maketest.cpp /Users/wen/Projects/include/bigint/BigInteger.cc /Users/wen/Projects/include/bigint/BigIntegerAlgorithms.cc /Users/wen/Projects/include/bigint/BigIntegerUtils.cc /Users/wen/Projects/include/bigint/BigUnsigned.cc /Users/wen/Projects/include/bigint/BigUnsignedInABase.cc
maketest.o /Users/wen/Projects/include/bigint/BigInteger.o /Users/wen/Projects/include/bigint/BigIntegerAlgorithms.o /Users/wen/Projects/include/bigint/BigIntegerUtils.o /Users/wen/Projects/include/bigint/BigUnsigned.o /Users/wen/Projects/include/bigint/BigUnsignedInABase.o
maketest.hpp /Users/wen/Projects/include/bigint/BigInteger.hh /Users/wen/Projects/include/bigint/BigIntegerAlgorithms.hh /Users/wen/Projects/include/bigint/BigIntegerLibrary.hh /Users/wen/Projects/include/bigint/BigIntegerUtils.hh /Users/wen/Projects/include/bigint/BigUnsigned.hh /Users/wen/Projects/include/bigint/BigUnsignedInABase.hh /Users/wen/Projects/include/bigint/NumberlikeArray.hh
Compiles with:
g++ -O2 -c -I/Users/wen/Projects/include/bigint

这证明 make 知道我想要什么,但是当它构建时却完全不同:c++ 而不是 g++?!

更新 2:

c++ 调用 clang,安装在我的系统上。

Alex B 的解决方案:

但是从编译命令行看起来 Make 正在尝试 使用隐式后缀规则,并忽略您的模式规则。

我尝试了.SUFFIXES:,是的,它报告了发现无规则。谢谢,我去看看说明书。

【问题讨论】:

  • 我的建议?检查"BigIntegerLibrary.hh" 是否存在。
  • 这已经告诉我很多次了;它确实存在。问题是 make 没有正确包含 CC_FLAGS 以将包含路径传递给编译器。
  • 我还需要提供什么其他信息吗?
  • 这在我的 GNU Make 3.81 机器(OSX 也是)上很好。问题可能出在您的环境(您是否包含任何其他构建文件?)或 Make 版本(如果您有较旧的 OSX)。
  • @AlexB 奇怪,我会调查一下。

标签: c++ g++ makefile


【解决方案1】:

正如我在评论中所说,它适用于我的环境(Mac OSX、GNU Make 3.81),所以问题可能是您发布的 makefile 不完整或者您使用的是不同版本的 Make。

但从编译命令行看来,Make 正在尝试使用隐式后缀规则,并忽略了您的模式规则。

您可以通过指定一个空的后缀列表来告诉 Make 忽略默认规则,这样您就可以进一步调试您的问题。

.SUFFIXES:

【讨论】:

  • "但是从编译命令行看来,Make 正在尝试使用隐式后缀规则,并忽略了您的模式规则。"这完全解释了它。我会调查的。
  • 我可以 +10'd 你的答案,我可以:D
猜你喜欢
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 2018-05-23
  • 2017-09-11
  • 2020-03-14
  • 2018-09-24
  • 2018-06-06
  • 1970-01-01
相关资源
最近更新 更多