GNU Make产生完全不同的结果

时间:2012-12-20 03:31:58

标签: c++ g++ makefile

这令人困惑。我有我的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以打印预期的compile编译执行:

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:,是的,它报告了一个无规则。谢谢,我会去查阅手册。

1 个答案:

答案 0 :(得分:2)

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

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

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

.SUFFIXES: