makefile隐式规则 - 困惑

时间:2013-03-25 05:45:27

标签: gcc makefile

我正在开发一个使用makefile制作目标的项目。 在这里我很困惑makefile如何从.cpp文件生成.o文件。

必须从clientthread_level1_unix.cpp文件生成类似文件clientthread_level1_unix.o。 但是没有指定使用* .cpp文件的地方。

EXTRALIBS = -pthread -lz -ldl -lm
OPENGLLIBS =
LDFLAGS_GL =
LDLIBS = ${APPEXTRALIBS} ${top_builddir}/lib/libwx_based-2.6.a ${EXTRALIBS}

# Compiler used
CXX         = c++
CC          = gcc

CANALOBJS =     ../../common/listenthread_unix.o ../../common/clientthread_level1_unix.o ../../common/devicethread_unix.o \
        ../../common/canalshmem_level1_unix.o ../../common/clientlist.o ../../common/controlobject.o \
        ../../common/devicelist.o ../../common/udpreceivethread_unix.o ../../../vscp/common/vscp.o \
        ../../common/clientthread_level2_unix.o ../../common/canalshmem_level2_unix.o \
        ../../common/tcplistenthread.o
CANALHDRS = ../../common/clientlist.h ../../common/controlobject.h ../../common/devicelist.h \
        ../../common/canal.h ../../common/canaldlldef.h \
        ../../common/version.h ../../common/canal_unix_ipc.h ../../common/CanalShMem_level1_unix.h \
        ../../common/CanalShMem_level2_unix.h ../../common/clientthread_level1_unix.h ../../common/clientthread_level2_unix.h
PROJOBJS    = ../../../common/dllist.o ../../../common/configfile.o ../../../common/crc.o
PROJHDRS    = ../../../common/dllist.h ../../../common/configfile.h
OBJS        = canald.o
HDRS        = canald.h


all:        canald

# Build the Linux executable
canald:     $(OBJS) $(HDRS) $(CANALOBJS) $(CANALHDR) $(PROJOBJS) $(PROJHDRS)
        $(CXX) $(OBJS) $(CANALOBJS) $(PROJOBJS) -o canald $(LIBS) $(LDLIBS)

如果我们也使用隐式规则,那么 canald 目标也不是那样的。 http://www.gnu.org/software/make/manual/make.html#make-Deduces http://www.gnu.org/software/make/manual/make.html#Implicit-Rules

请知道这条生产线如何生产 canald

    $(CXX) $(OBJS) $(CANALOBJS) $(PROJOBJS) -o canald $(LIBS) $(LDLIBS)

1 个答案:

答案 0 :(得分:0)

该行

 $(CXX) $(OBJS) $(CANALOBJS) $(PROJOBJS) -o canald $(LIBS) $(LDLIBS)

将大约(我跳过未定义的变量)翻译成:

 c++  canald.o ../../common/listenthread_unix.o ../../common/clientthread_level1_unix.o ../../common/devicethread_unix.o \
    ../../common/canalshmem_level1_unix.o ../../common/clientlist.o ../../common/controlobject.o \
    ../../common/devicelist.o ../../common/udpreceivethread_unix.o ../../../vscp/common/vscp.o \
    ../../common/clientthread_level2_unix.o ../../common/canalshmem_level2_unix.o \
    ../../common/tcplistenthread.o \
    ../../../common/dllist.o ../../../common/configfile.o ../../../common/crc.o \
    -o canald /lib/libwx_based-2.6.a -pthread -lz -ldl -lm

,即“正常”编译器调用。由于canald有明确的规则,因此无需搜索隐含的规则。此外,它是Makefile中的目标规则,因此它是默认目标。

Howerver,对于canald对象的依赖,不存在明确的规则,因此使用隐式规则。

如果您想知道存在哪些隐式规则,请调用make -p并在输出中搜索该模式,例如在您的情况下为%.cpp