c - Makefile - 仅生成两个库之一

标签 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

有人知道发生了什么事以及如何解决这个问题吗?

提前致谢!!

最佳答案

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

If a pattern rule has multiple targets, make knows that the rule’s recipe is responsible for making all of the targets. The recipe is executed only once to make all the targets.

%_$(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 的内置规则.

关于c - Makefile - 仅生成两个库之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47028708/

相关文章:

c++ - 如何在make下编译多个文件

macos - 安装 Valgrind 时 make 失败

c++ - 自动添加额外的引用

找不到/lib64/ld-linux-x86-64.so.2

c - 在 linux 中使用 send() 函数发送数据

c - 尽可能快地在C中提取两个相似(或不同)字符串之间的字符串

java - 反模问题 where gcd(denominator,mod)!=1

在 C 中将 const void * 转换为 const char *

makefile - 传递参数以在 Makefile 中进行调用

c++ - 共享库中的 QQuickWindow 在 QApplication 中显示时自动关闭