gpt4 book ai didi

c - 如何创建正确的 Makefile 以及依赖项的顺序重要吗?

转载 作者:行者123 更新时间:2023-11-30 19:16:30 24 4
gpt4 key购买 nike

我有 5 个文件,必须使用 makefile 来创建一个文件。

student.c has #include "student.h"

linkedlist.c has #include "linkedlist.h"

and main has #include "linkedlist.h" and #include "student.h"

student.c
student.h
linkedlist.c
linkedlist.h
main.c

我不知道顺序对于解决依赖关系是否重要。我想我真正要问的是自下而上的依赖意味着什么???有人可以澄清如何在未来的项目中正确使用 makefile 吗?

最佳答案

基本上,您需要了解的是:

#You can make some const variables like `CC = gcc` 
#and use them in the Makefile like that `$(CC)`
#(you basically wrap the variable with brackets and
#put a dollar sign before it).

CC = gcc

#The order is crutial in a makefile.

# First of all you want to compile `main` using objects
# which are yet to be linked.
main: student.o linkedlist.o
# Line below is a command
# that is already well known to you I guess: "gcc -o main ..."
$(CC) -o main student.o linkedlist.o

# If makefile doesn't have the newest version of a .o file,
# then it goes to lines below
# to find out, how can it obtain the newest version
# of student.o or linkedlist.o

# This is how it can produce the .o file:
student.o: student.c
$(CC) -c student.c
# -c flag is crutial here, because it means that you want to create
# a .o (object) file. Not an executable program.

# Same with linkedlist.c:
linkedlist.o: linkedlist.c
$(CC) -c linkedlist.c

我希望它能起作用,我还没有测试过。如果我有任何错误,请纠正我。

<小时/>

还有一件事:请记住,您必须使用 TABS 而不是空格来缩进行。

关于c - 如何创建正确的 Makefile 以及依赖项的顺序重要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30020728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com