Makefile - 只使两个Libs中的一个

时间:2017-10-31 05:53:18

标签: c makefile lib

我是makefile的新手,我正在尝试编译几个文件。

首先称为s-chat,其他人是RWT(读写者)和DP(餐饮哲学家)。他们都使用我的列表库,但是s-chat需要使用-m32标志进行编译。

这就是我尝试做的:我使用lib-adders.o,lib_movers.o,lib_removers.o调用了一个libMonitor.a。另一个是使用lib_adders_32.o,lib_removers_32.o,lib_movers_32.o的liblist_32。出于某种原因,只有这两个库中的第一个被创建,而另一个说它无法找到它的第一个依赖。

PTHREADS  = /student/cmpt332/pthreads
RTT  = /student/cmpt332/rtt

CC       = gcc
CFLAGS   = -g
CPPFLAGS = -std=c90 -Wall -pedantic

.PHONEY: all clean

ARCH = $(shell uname -sm | tr -d ' ')

ifeq ($(ARCH),SunOS)
ARCH = $(PLATFORM)

PROCESSOR = "$(shell uname -p)"
ifeq ($(PROCESSOR),i386)
ARCH = i86pc
endif

endif

all: s-chat reader-writer-test dining-philosophers-test 

RWT_OBJS = reader-writer_$(ARCH).o reader-writer-monitor_$(ARCH).o Monitor_$(ARCH).o libMonitor_$(ARCH).a

reader-writer-test: CPPFLAGS += -I$(PTHREADS)
reader-writer-test: LDFLAGS += -L$(PTHREADS)/lib/$(ARCH)
reader-writer-test: LDLIBS += -lpthreads
reader-writer-test: $(RWT_OBJS)
    $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@

reader-writer_$(ARCH).o: reader-writer.c reader_writer_monitor.h Monitor.h

reader-writer-monitor_$(ARCH).o: reader-writer-monitor.c reader_writer_monitor.h Monitor.h

DP_OBJS = dining-philosophers_$(ARCH).o dining-philosophers-monitor_$(ARCH).o Monitor_$(ARCH).o libMonitor_$(ARCH).a

dining-philosophers-test: CPPFLAGS += -I$(PTHREADS)
dining-philosophers-test: LDFLAGS += -L$(PTHREADS)/lib/$(ARCH)
dining-philosophers-test: LDLIBS += -lpthreads
dining-philosophers-test: $(DP_OBJS)
    $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@

dining-philosophers_$(ARCH).o: dining-philosophers.c dining_philosophers_monitor.h Monitor.h

dining-philosophers-monitor_$(ARCH).o: dining-philosophers-monitor.c dining_philosophers_monitor.h Monitor.h

Monitor_$(ARCH).o: Monitor.c Monitor.h

list_adders.o: list_adders.c list.h
list_movers.o: list_movers.c list.h
list_removers.o: list_removers.c list.h

libMonitor_$(ARCH).a: list_adders.o list_movers.o list_removers.o
    ar rcs $@ $^


list_adders_32.o: CFLAGS += -m32
list_adders_32.o: LDFLAGS += -m32
list_adders_32.o: list_adders.c list.h
list_movers_32.o: CFLAGS += -m32
list_movers_32.o: LDFLAGS += -m32
list_movers_32.o: list_movers.c list.h
list_removers_32.o: CFLAGS += -m32
list_removers_32.o: LDFLAGS += -m32
list_removers_32.o: list_removers.c list.h

liblist_32_$(ARCH).a: list_adders_32.o list_movers_32.o list_removers_32.o
    ar rcs $@ $^

s-chat: CPPFLAGS += -I$(RTT)/include
s-chat: CFLAGS += -m32
s-chat: LDFLAGS += -L$(RTT)/lib/$(ARCH) -m32
s-chat: LDLIBS += -lRtt -lRttUtils
s-chat: s-chat.o liblist_32_$(ARCH).a
s-chat.o: s-chat.c

%_$(ARCH).o %_32_$(ARCH).o %.o %_32.o: %.c
    $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

%: %.o
    $(CC) $(LDFLAGS) $(LDLIBS) $^ -o $@

clean:
    rm -rf *.o *.a reader-writer-test dining-philosophers-test s-chat

这是我得到的错误:

ar rcs libMonitor_Linuxx86_64.a list_adders.o list_movers.o list_removers.o
ar: list_adders.o: No such file or directory
Makefile:66: recipe for target 'libMonitor_Linuxx86_64.a' failed
make: *** [libMonitor_Linuxx86_64.a] Error 1

如果我最后进行s-chat,则会生成libMonitor,但我在liblist_32中得到了完全相同的错误,并且说它无法找到list_adders_32.o

任何人都知道发生了什么以及如何解决这个问题?

提前致谢!!

1 个答案:

答案 0 :(得分:0)

https://www.gnu.org/software/make/manual/make.html#Pattern-Intro

  

如果模式规则有多个目标,则make知道规则的配方负责制作所有目标。配方只执行一次以制作所有目标。

%_$(ARCH).o %_32_$(ARCH).o %.o %_32.o: %.c
    $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

应该像

%_$(ARCH).o: %.c
    $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

%_32_$(ARCH).o: %.c
    $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

%.o: %.c
    $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

%_32.o: %.c
    $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

但是,由于您的所有来源似乎都在同一个目录中,因此您可以完全删除这些规则,因为make已经有%.o: %.c的内置规则。