【问题标题】:Error generating dependencies with make使用 make 生成依赖项时出错
【发布时间】:2012-10-05 20:41:44
【问题描述】:

我正在尝试实现论文“Recursive Make Considered Harmful”中概述的非递归 make 解决方案。我目前坚持要生成 *.d 依赖文件。我在下面提供了 makefile、示例 module.mk 和错误。有什么想法可以解决这个问题吗?

MODULES :=      \
    module1     \
    module2     

# define compiler
CC = /opt/local/bin/clang++-mp-3.1

# exclude the following warnings for clang
CLANG_NO_WARN =                 \
    -Wno-c++98-compat           \
    -Wno-weak-vtables           \
    -Wno-padded                 \
    -Wno-global-constructors    \
    -Wno-exit-time-destructors   

# look for include files in each of the modules
CFLAGS +=                                                               \
    -g -Weverything -Wall -std=c++11 -stdlib=libc++ $(CLANG_NO_WARN)    \
    -I../ -I/usr/local/include $(patsubst %, -I%, $(MODULES))

# linker flags
LDFLAGS :=                                                  \
    -stdlib=libc++                                          \
    -L/usr/local/boost_1_50_0/stage/lib -L/usr/local/lib

# extra libraries if required (each module will add to this)
LIBS :=                     \
    -lboost_program_options \
    -lboost_system          \
    -lglog                  \
    -lpugixml

# source files to be compiled (each module will add to this)
SRCS := \
    Main.cpp

# include the descriptions for each module
include $(patsubst %, %/module.mk, $(MODULES))

# determine the object files
OBJS := \
    $(patsubst %.cpp, %.o, $(filter %.cpp, $(SRCS)))

# link the program
prog: $(OBJS)
    $(CC) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)

# include the C include dependencies
include $(OBJS:.o=.d)

# calculate C include dependencies
%.d: %.cpp
    depend.sh `dirname $*.cpp` $(CFLAGS) $*.cpp > $@  

----------

#!/bin/sh

# Evaluate dependencies for use by the makefile

echo "Called"
DIR="$1"
shift 1
case "$DIR" in
    "" | ".")
        $CC -MM -MG "$@" | sed -e 's@ˆ\(.*\)\.o:@\1.d \1.o:@' ;;
    *)
        $CC -MM -MG "$@" | sed -e "s@ˆ\(.*\)\.o:@$DIR/\1.d \ $DIR/\1.o:@" ;;
esac    

------------

# module.mk
SRCS += \
    Algo.cpp        \
    CommandHandler.cpp  \
    Exchange.cpp        \
    TCPSocket.cpp       \
    TradingEngine.cpp       

----------

$ make
makefile:68: Main.d: No such file or directory
makefile:68: view_string.d: No such file or directory
makefile:68: Algo.d: No such file or directory
makefile:68: CommandHandler.d: No such file or directory
makefile:68: Exchange.d: No such file or directory
makefile:68: TCPSocket.d: No such file or directory
makefile:68: TradingEngine.d: No such file or directory
makefile:68: Exchange.d: No such file or directory
makefile:68: Requests.d: No such file or directory
makefile:68: TickCapture.d: No such file or directory
makefile:68: types.d: No such file or directory
make: *** No rule to make target `types.d'.  Stop.

更新 完成的makefile和示例module.mk

$cat makefile
# executable name
BINARY := my_prog

# clang config
CLANG := /opt/local/bin/clang++-mp-3.1

CLANG_WARNINGS :=               \
    -Wno-c++98-compat           \
    -Wno-weak-vtables           \
    -Wno-padded                 \
    -Wno-global-constructors    \
    -Wno-exit-time-destructors

CLANG_CFLAGS := \
    -g -Weverything -Wall -std=c++11 -stdlib=libc++

CLANG_LDFLAGS := \
    -stdlib=libc++

# generic compiler config
CC :=       $(CLANG)
CFLAGS :=   $(CLANG_WARNINGS) $(CLANG_CFLAGS) 
LDFLAGS :=  $(CLANG_LDFLAGS)

INCS :=                             \
    -I../                           \
    -I/usr/local/include            \
    $(patsubst %, -I%, $(SUBDIRS))

LIBS :=                                 \
    -L/usr/local/boost_1_50_0/stage/lib \
    -L/usr/local/lib                    \
    -lboost_program_options             \
    -lboost_system                      \
    -lglog                              \
    -lpugixml

# list subdirectories in which to look for dependencies
# must define SRCS first as subdirs will append to this
# their src files
SRCS := Main.cpp

SUBDIRS :=  \
    module1 \
    module2

include $(patsubst %, %/module.mk, $(SUBDIRS))

# derive object files from srcs
OBJS := $(patsubst %.cpp, %.o, $(filter %.cpp, $(SRCS)))

# link the program
$(BINARY): $(OBJS)
    $(CC) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)

# include generated dependency files
DEPS := $(OBJS:.o=.d)
-include $(DEPS)

# generate include dependencies
%.d: %.cpp
    ./depend.sh `dirname $*.cpp` $(INCS) $*.cpp > $@

# compile
.cpp.o:
    $(CC) $(CFLAGS) $(INCS) $< -c -o $@

# clean, obviously
clean:
    rm -f $(BINARY)
    rm -f $(OBJS)
    rm -f $(DEPS)

# et voila!   

-----

$cat module1/module.mk
SRCS_PATH := module1
SRCS += \
    $(SRCS_PATH)/Algo.cpp           \
    $(SRCS_PATH)/CommandHandler.cpp \
    $(SRCS_PATH)/Exchange.cpp       \
    $(SRCS_PATH)/TCPSocket.cpp      \
    $(SRCS_PATH)/TradingEngine.cpp 

【问题讨论】:

  • 我认为您可能需要在 `dirname $*.cpp` 周围加上引号
  • 尝试将“include $(OBJS:.o=.d)”移动到makefile中“%.d: %.cpp”转换规则之后的某个点。
  • @jpm 这个不行,后面的勾需要展开
  • @ShaunMarko 排序无关紧要, .o=.d 计算时会搜索 %.d 规则
  • Gnu make 分两个阶段读取 makefile。在第一阶段,它将包含由“include”指令指示的所有其他 makefile。在您的情况下, include 指令包含 %.d 依赖文件。 %.d 依赖文件是使用“%.d: %.cpp”配方从 %.cpp 文件创建的,但该配方直到 make 执行的第二阶段才会运行。配方执行推迟到第二阶段。

标签: c++ makefile gnu-make


【解决方案1】:

看起来好像某些模块将types.cpp 添加到SRCS,即使不存在这样的源文件。

至于警告,当你第一次运行这个makefile时,依赖文件(foo.d)还不存在,所以Make抱怨它不能include他们。这不是问题,当这些文件事先确实存在时,警告不会出现在后续运行中。要完全禁止警告,请将include 更改为-include

【讨论】:

    猜你喜欢
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多