GNU Make产生完全不同的结果

ihs*_* ih 8 c++ makefile g++

这令人困惑.我有我的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)
Run Code Online (Sandbox Code Playgroud)

它首先成功编译,但随后它无缘无故停止,这是输出:

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.
Run Code Online (Sandbox Code Playgroud)

问题是,我甚至没有告诉它在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
Run Code Online (Sandbox Code Playgroud)

任何帮助或建议将不胜感激.谢谢!

更新

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

print:
    @echo $(CC_FILES)
    @echo $(OBJECTS)
    @echo $(HEADERS)
    @echo "Compiles with:"
    @echo $(COMPILE) $(COMPILE_FLAGS) $(LD_FLAGS) $(CC_FLAGS)
Run Code Online (Sandbox Code Playgroud)

结果:

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
Run Code Online (Sandbox Code Playgroud)

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

更新2:

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

Alex B解决方案:

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

我试过了.SUFFIXES:,是的,它报告了一个无规则的发现.谢谢,我会去查阅手册.

Ale*_*x B 2

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

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

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

.SUFFIXES:
Run Code Online (Sandbox Code Playgroud)