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

标签 c makefile

我有 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/

相关文章:

c - 是否可以在 C 中模仿 python 元组?

c - 转换大写和小写为什么我们添加 +32 和 -32 请解释

c++ - Makefile 因 C++ 编译而失败

c - 一个二进制文件中的两个过滤器驱动程序导致第二个驱动程序出现系统错误 2

使用 SDL 1.2 的 C 中的自定义旋转功能无法正常工作

makefile - GNU 制作 3.81 : eval function not working?

makefile - Linux内核模块编程Makefile

c - 生成文件错误:/usr/bin/ld: cannot find -lsqlite3

c++ - 制作文件g++共享库

c - 如何从 char、int 和 float 类型的指针值构造 char 数组 (C)