C,重构Makefile

发布于 2025-01-23 05:08:34 字数 1557 浏览 2 评论 0原文

但是,如果我删除eputliste.o lecterfichier.o statistiques.o tri.o,我的makefile有效。 从目标编译中,我认为这仍然可以在其他.O目标中完成。

Make:    link

compile: listechainee.o debutliste.o lecturefichier.o statistiques.o tri.o

listechainee.o: listechainee.c listechainee.h debutliste.o 
    gcc -g -Wall -Wextra -std=c11 -c listechainee.c
debutliste.o: debutliste.c debutliste.h lecturefichier.o
    gcc -g -Wall -Wextra -std=c11 -c debutliste.c
lecturefichier.o: lecturefichier.c lecturefichier.h statistiques.o
    gcc -g -Wall -Wextra -std=c11 -c lecturefichier.c
statistiques.o: statistiques.c statistiques.h tri.o
    gcc -g -Wall -Wextra -std=c11 -c statistiques.c
tri.o: tri.c  
    gcc -g -Wall -Wextra -std=c11 -c tri.c

clean:
    rm -rf *.o 

link: compile
    gcc *.o -o tri

如果我理解正确的话,我应该删除peputliste.o lecterfichier.o statistiques.o tri.o在.o targets内部的bc内部,它们将被执行为来自目标编译的依赖项,看起来应该像这?

Make:    link

compile: listechainee.o debutliste.o lecturefichier.o statistiques.o tri.o

listechainee.o: listechainee.c listechainee.h 
    gcc -g -Wall -Wextra -std=c11 -c listechainee.c
debutliste.o: debutliste.c debutliste.h 
    gcc -g -Wall -Wextra -std=c11 -c debutliste.c
lecturefichier.o: lecturefichier.c lecturefichier.h 
    gcc -g -Wall -Wextra -std=c11 -c lecturefichier.c
statistiques.o: statistiques.c statistiques.h 
    gcc -g -Wall -Wextra -std=c11 -c statistiques.c
tri.o: tri.c  
    gcc -g -Wall -Wextra -std=c11 -c tri.c

clean:
    rm -rf *.o 

link: compile
    gcc *.o -o tri

My makefile works however if I erase debutliste.o lecturefichier.o statistiques.o tri.o
from the target compile, it still works I imagine bc they are done in the other .o targets.

Make:    link

compile: listechainee.o debutliste.o lecturefichier.o statistiques.o tri.o

listechainee.o: listechainee.c listechainee.h debutliste.o 
    gcc -g -Wall -Wextra -std=c11 -c listechainee.c
debutliste.o: debutliste.c debutliste.h lecturefichier.o
    gcc -g -Wall -Wextra -std=c11 -c debutliste.c
lecturefichier.o: lecturefichier.c lecturefichier.h statistiques.o
    gcc -g -Wall -Wextra -std=c11 -c lecturefichier.c
statistiques.o: statistiques.c statistiques.h tri.o
    gcc -g -Wall -Wextra -std=c11 -c statistiques.c
tri.o: tri.c  
    gcc -g -Wall -Wextra -std=c11 -c tri.c

clean:
    rm -rf *.o 

link: compile
    gcc *.o -o tri

If I understand correctly I should delete the debutliste.o lecturefichier.o statistiques.o tri.o that are inside the .o targets bc they will be executed as dependencies from the target compile and it should look like this?

Make:    link

compile: listechainee.o debutliste.o lecturefichier.o statistiques.o tri.o

listechainee.o: listechainee.c listechainee.h 
    gcc -g -Wall -Wextra -std=c11 -c listechainee.c
debutliste.o: debutliste.c debutliste.h 
    gcc -g -Wall -Wextra -std=c11 -c debutliste.c
lecturefichier.o: lecturefichier.c lecturefichier.h 
    gcc -g -Wall -Wextra -std=c11 -c lecturefichier.c
statistiques.o: statistiques.c statistiques.h 
    gcc -g -Wall -Wextra -std=c11 -c statistiques.c
tri.o: tri.c  
    gcc -g -Wall -Wextra -std=c11 -c tri.c

clean:
    rm -rf *.o 

link: compile
    gcc *.o -o tri

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎盛开的樱花 2025-01-30 05:08:34

是的。如果使用GNU MAKE,可以通过依靠它已经具有规则目录。这些规则告诉如何将C文件编译到对象文件中以及如何将对象文件链接到二进制中。递归标志-r rm使我感到紧张,因此我将其删除。您可能不需要单独的链接步骤,因此我使用标准all目标,而不是compile

.PHONY: all clean
CFLAGS:=-g -Wall -Wextra -std=c11

all: tri

clean:
    rm -f *.o ./tri

tri: listechainee.o debutliste.o lecturefichier.o statistiques.o tri.o

debutliste.o: debutliste.c debutliste.h

lecturefichier.o: lecturefichier.c lecturefichier.h

listechainee.o: listechainee.c listechainee.h

statistiques.o: statistiques.c statistiques.h

如果您想幻想,则可以使用模式规则

.PHONY: all clean
CFLAGS:=-g -Wall -Wextra -std=c11

all: tri

clean:
    rm -f *.o ./tri

tri: listechainee.o debutliste.o lecturefichier.o statistiques.o tri.o

%.o: %.c %h
     $(CC) $(CFLAGS) $(LDFLAGS) -o $@ 
lt;

。一个不同的名称。您可以将其写为:

OBJECTS:=$(patsubst %.c,%.o,$(wildcard *.c))

tri: $(OBJECTS)

但是,我通常不会在项目中执行此步骤,因为现在的构建对我在工作时经常创建的临时.c文件敏感。

Yes. If you are using GNU Make, you can simplify your makefile (per above) by relying on the fact it already has a catalog of rules. Those rules tells make how to compile c files into object files and how to link object files into a binary. The recursive flag -r for rm makes me nervous so I removed it. You probably don't need a separate link step so I use the standard all target instead of compile:

.PHONY: all clean
CFLAGS:=-g -Wall -Wextra -std=c11

all: tri

clean:
    rm -f *.o ./tri

tri: listechainee.o debutliste.o lecturefichier.o statistiques.o tri.o

debutliste.o: debutliste.c debutliste.h

lecturefichier.o: lecturefichier.c lecturefichier.h

listechainee.o: listechainee.c listechainee.h

statistiques.o: statistiques.c statistiques.h

If you want to be fancy you can use a pattern rule %.o: %.c %h that says a given file .o depends on it's corresponding .c and .h files which makes the makefile quite compact:

.PHONY: all clean
CFLAGS:=-g -Wall -Wextra -std=c11

all: tri

clean:
    rm -f *.o ./tri

tri: listechainee.o debutliste.o lecturefichier.o statistiques.o tri.o

%.o: %.c %h
     $(CC) $(CFLAGS) $(LDFLAGS) -o $@ 
lt;

The list of object files is probably just all your c files with a different name. You can write that as:

OBJECTS:=$(patsubst %.c,%.o,$(wildcard *.c))

tri: $(OBJECTS)

However, I don't usually do that step in my projects as the build now become sensitive to temporary .c files that I often create while working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文