C 使用不同的测试创建makefile
我试图了解我是否正确创建了Makefile。我有以下文件:C 使用不同的测试创建makefile,c,makefile,C,Makefile,我试图了解我是否正确创建了Makefile。我有以下文件: Student.h Student.C University.h University.c list.h IDCard.h IDCard.c union.h 我还有一个库mylib,我使用list.h和union.hfrom. 此外,我有以下层次结构: - University - Student - list - IDCard - union 这意味着大学导入学生和工会和学生导入列表
Student.h Student.C University.h University.c list.h IDCard.h IDCard.c union.h
我还有一个库mylib
,我使用list.h
和union.h
from.此外,我有以下层次结构:
- University
- Student
- list
- IDCard
- union
这意味着大学
导入学生
和工会
和学生
导入列表
和身份证
我需要遵循的步骤:
testing1.exe
-无断言的释放模式。此文件是给定testing1.c
的可运行文件make testing2.exe
创建test2.exe
-无断言的释放模式。此文件是可运行的testing2.c
make testing2_debug.exe
创建带有断言的testing2_debug.exe
-调试模式。此文件是可运行的test2.c
maketest
创建test.exe
-无断言的释放模式。此文件可运行于test.c
make clean
进行清理,以便重建成功CC = gcc
OBJS = IDCard.o Student.o University.o
DEBUG_OBJS = IDCard_debug.o Student_debug.o University_debug.o
SOURCE = IDCard.c Student.c University.c
HEADER = IDCard.h Student.h University.h list.h union.h
CFLAGS = -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG -L. -mylib
EXEC = testing.exe testing1.exe testing2.exe testing2_debug.exe
testing_O = testing1.o testing2.o testing2_debug.o testing.o
#make testing1.exe
testing1.exe : $(OBJS) $*.o
$(CC) -o $@ -DNDEBUG $(OBJS) -L. mylib
#make testing2.exe
testing2.exe : $(OBJS) $*.o
$(CC) -o $@ -DNDEBUG $(OBJS) -L. mylib
#testing2_debug.exe
testing2_debug.exe : $(DEBUG_OBJS) $*.o
$(CC) -o $@ -DNDEBUG $(OBJS) -L. mylib
#make testing.exe
testing.exe : $(OBJS) $*.o
$(CC) -o $@ -DNDEBUG $(OBJS) -L. mylib
testing1.o : testing1.c Student.h University.h
$(CC) -c -DNDEBUG $(CFLAGS) $*.c
Student.o : list.h IDCard.h Student.c Student.h
$(CC) -c -DNDEBUG $(CFLAGS) $*.c
University.o : union.h University.c University.h
$(CC) -c -DNDEBUG $(CFLAGS) $*.c
IDCard.o : IDCard.h
$(CC) -c -DNDEBUG $(CFLAGS) $*.c
testing2_debug.o : testing2.c Student.h University.h
$(CC) -c -g $(CFLAGS) $*.c -o $@
Student_debug.o : list.h IDCard.h Student.c Student.h
$(CC) -c -g $(CFLAGS) $*.c -o $@
University_debug.o : union.h University.c University.h
$(CC) -c -g $(CFLAGS) $*.c -o $@
IDCard_debug.o : IDCard.h
$(CC) -c -g $(CFLAGS) $*.c -o $@
clean :
rm -f $(OBJS) $(DEBUG_OBJS) $(EXEC) $(testing_O)
我对创建makefile有点陌生,所以我尽量少犯错误。我的Makefile能满足我的需要吗?可以简化吗?它遵循惯例吗
IDCard.o : IDCard.h
$(CC) -c -DNDEBUG $(CFLAGS) $*.c
但是.h
文件没有被编译
作为常规样式,在.h
依赖项之前列出.c
依赖项
编辑:社区维基。如果不是OP的澄清评论,我现在会删除这篇文章。
$*。o
不应该是一个依赖项<代码>$(OBJS)已经涵盖了这一点
一般来说,可以避免单个对象文件的目标
以下是一个可能需要修改以满足您需求的示例目标:
%.o: %.c %.h
<tab>$(CC) -DNDEBUG -c $(CFLAGS) -o $@ $<
%_debug.o: %.c %.h
<tab>$(CC) -c -g $(CFLAGS) -o $@ $<
此外,请确保在这些示例中用硬选项卡替换
,以使Makefile有效。那么,如何编译它呢?这是唯一的错误吗?最后的makefile
应该是什么样子?@vesii:你不编译.h
文件,所以只需删除这两行以及对DCard.o
的任何其他引用(除非某处真的有IDCard.c
文件)。哦,糟糕,我忘了提到它。有一个IDCard.C
文件。我会编辑。如果你有一些有用的东西,并且你想知道如何改进它,就是这个地方。看起来这个makefile可以工作,但是很多重构是可能的。请注意,如果您重新安排先决条件以将源文件放在第一位(例如“foo.o:foo.h bar.h foo.c”=>“foo.o:foo.c foo.h bar.h”),那么您可以使用$@Beta来演示如何操作。现在,我更仔细地看了一下,我看到了一些看起来像实际bug的东西。你试过用这个来构建文件吗?他需要单个.o文件的单个目标,因为他的.h依赖关系树并不简单。否则,答案比我的好。@Joshua我添加了一个稍微好一点的规则,应该有效。我不认为这个规则有效。所有的依赖项都不是这样传播的。一切都不是关键词。现在我们有了一个非常好的答案。可惜我不能投两次票。
testing1.o: Student.h University.h
Student.o: list.h IDCard.h Student.h
University.o: union.h University.h
IDCard.o: IDCard.h
testing2_debug.o: Student.h University.h
Student_debug.o: list.h IDCard.h Student.h
University_debug.o: union.h University.h
IDCard_debug.o: IDCard.h
%.o: %.c
<tab>$(CC) -DNDEBUG -c $(CFLAGS) -o $@ $<
%_debug.o: %.c
<tab>$(CC) -c -g $(CFLAGS) -o $@ $<
%.exe: $(OBJS)
<tab>$(CC) -DNDEBUG $(CFLAGS) $^ -o $@ -L. -lmylib
%_debug.exe: $(DEBUG_OBJS)
<tab>$(CC) -g $(CFLAGS) $^ -o $@ -L. -lmylib