gpt4 book ai didi

linux - make 命令和运行 build .sh 脚本之间的主要区别是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 09:15:13 25 4
gpt4 key购买 nike

在这本 Linux 手册中写道 make是:

a utility for building and maintaining groups of programs (and other types of files) from source code.

Description The purpose of the make utility is to determine automatically which pieces of a large program need to be re-compiled, and issue the commands necessary to recompile them.

这里的automatic determination是什么意思?运行 make 或只运行 ./build.sh 我有我的构建脚本的主要/显着区别是什么?

最佳答案

What is the principal/significant difference between running make or just running ./build.sh where I have my build scripts?

make 可以很容易地使用,它只重建需要重建的部分(仅此而已)。


对比示例

考虑一个要构建的项目,program,由以下文件组成:

main.c
foo.c
bar.c
foo.h
bar.h

对于此讨论,我们假设 foo.cbar.c 仅包括(即:依赖于)foo.hbar.c,分别。

  • 使用 build.sh 脚本,它只包含用于构建软件的命令,如下所示:

    gcc -c foo.c
    gcc -c bar.c
    gcc -c main.c
    gcc foo.o bar.o main.o -o program

    如果只是foo.h 被修改,这个项目需要重建。使用这种方法,这将通过重新运行 build.sh 来完成,它会编译所有 源文件。请注意,bar.omain.o 实际上都不需要再次生成,因为文件 bar.c bar.hmain.c 没有改变,它们根本不依赖于 foo.h(即:它们不受在 foo.h 中更改)。

  • 使用 make 但是,如果使用得当,会生成一个依赖图,这样只会再次生成需要更新的文件:在本例中为 foo. c程序

    这依赖于源文件和目标文件(以及其他事物)之间的依赖关系被正确指定给 make。这是通过 makefile 实现的:

    program: main.o foo.o bar.o
    gcc -o $@ $^

    foo.c: foo.h
    bar.c: bar.h

    基本上,它明确指定:

    • 程序依赖于main.ofoo.obar.o
    • foo.c 依赖于 foo.h
    • bar.c 依赖于bar.c

    隐含地:

    • main.o 依赖main.c
    • foo.o 依赖于 foo.c
    • bar.o 依赖bar.c

    这样一来,make 就知道每当发生变化时必须重建或更新的内容,并且它只会更新这些内容,而不是从头开始构建所有内容。

关于linux - make 命令和运行 build .sh 脚本之间的主要区别是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48719028/

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