make和gcc有什么区别?

时间:2021-06-06 02:12:32

The last sentence in the article caught my eye

文章的最后一句引起了我的注意

[F]or C/C++ developers and students interested in learning to program in C/C++ rather than users of Linux. This is because the compiling of source code is made simple in GNU/Linux by the use of the 'make' command.

[F]或C / C ++开发人员和有兴趣学习使用C / C ++而不是Linux用户编程的学生。这是因为通过使用'make'命令在GNU / Linux中简化了源代码的编译。

I have always used gcc to compile my C/C++ programs, whereas javac to compile my Java programs. I have only used make to install programs to my computer by configure/make/make install.

我总是使用gcc编译我的C / C ++程序,而javac则编译我的Java程序。我只使用make通过configure / make / make install将程序安装到我的计算机上。

It seems that you can compile apparently all your programs with the command make.

看来你可以使用命令make编译所有程序。

What is the difference between make and gcc?

make和gcc有什么区别?

11 个解决方案

#1


Well ... gcc is a compiler, make is a tool to help build programs. The difference is huge. You can never build a program purely using make; it's not a compiler. What make does it introduce a separate file of "rules", that describes how to go from source code to finished program. It then interprets this file, figures out what needs to be compiled, and calls gcc for you. This is very useful for larger projects, with hundreds or thousands of source code files, and to keep track of things like compiler options, include paths, and so on.

嗯... gcc是一个编译器,make是一个帮助构建程序的工具。差异很大。你永远不能纯粹使用make来构建一个程序;它不是编译器。它是什么使它引入了一个单独的“规则”文件,描述了如何从源代码转到完成的程序。然后它解释这个文件,找出需要编译的内容,并为你调用gcc。这对于具有数百或数千个源代码文件的大型项目非常有用,并且可以跟踪编译器选项,包含路径等内容。

#2


gcc compiles and/or links a single file. It supports multiple languages, but does not knows how to combine several source files into a non-trivial, running program - you will usually need at least two invocations of gcc (compile and link) to create even the simplest of programs.

gcc编译和/或链接单个文件。它支持多种语言,但不知道如何将多个源文件组合成一个非平凡的运行程序 - 您通常需要至少两次gcc(编译和链接)调用来创建最简单的程序。

Wikipedia page on GCC describes it as a "compiler system":

GCC上的*页面将其描述为“编译系统”:

The GNU Compiler Collection (usually shortened to GCC) is a compiler system produced by the GNU Project supporting various programming languages.

GNU编译器集合(通常缩写为GCC)是由GNU项目生成的编译器系统,支持各种编程语言。

make is a "build tool" that invokes the compiler (which could be gcc) in a particular sequence to compile multiple sources and link them together. It also tracks dependencies between various source files and object files that result from compilation of sources and does only the operations on components that have changed since last build.

make是一个“构建工具”,它以特定的顺序调用编译器(可能是gcc)来编译多个源并将它们链接在一起。它还跟踪源自编译源的各种源文件和目标文件之间的依赖关系,并且仅对自上次构建以来已更改的组件执行操作。

GNUmake is one popular implementation of make. The description from GNUmake is as follows:

GNUmake是make的一个流行实现。 GNUmake的描述如下:

Make is a tool which controls the generation of executables and other non-source files of a program from the program's source files.

Make是一个工具,可以从程序的源文件中控制程序的可执行文件和其他非源文件的生成。

Make gets its knowledge of how to build your program from a file called the makefile, which lists each of the non-source files and how to compute it from other files.

Make了解如何从名为makefile的文件构建程序,该文件列出了每个非源文件以及如何从其他文件计算它。

#3


gcc is a C compiler: it takes a C source file and creates machine code, either in the form of unlinked object files or as an actual executable program, which has been linked to all object modules and libraries.

gcc是一个C编译器:它接受一个C源文件,并以未链接的目标文件的形式或作为实际的可执行程序创建机器代码,该程序已链接到所有对象模块和库。

make is useful for controlling the build process of a project. A typical C program consists of several modules (.c) and header files (.h). It would be time-consuming to always compile everything after you change anything, so make is designed to only compile the parts that need to be re-compiled after a change.

make对于控制项目的构建过程很有用。典型的C程序由几个模块(.c)和头文件(.h)组成。在更改任何内容后始终编译所有内容将非常耗时,因此make旨在仅编译在更改后需要重新编译的部分。

It does this by following rules created by the programmer. For example:

它通过遵循程序员创建的规则来实现。例如:

foo.o: foo.c foo.h
    cc -c foo.c

This rule tells make that the file foo.o depends on the files foo.c and foo.h, and if either of them changes, it can be built by running the command on the second line. (The above is not actual syntax: make wants the commands indented by a TAB characters, which I can't do in this editing mode. Imagine it's there, though.)

此规则告诉make文件foo.o依赖于文件foo.c和foo.h,如果其中任何一个发生更改,则可以通过在第二行上运行命令来构建它。 (上面不是实际的语法:make想要用TAB字符缩进的命令,但在这种编辑模式下我无法做到。但想象一下,它就在那里。)

make reads its rules from a file that is usually called a Makefile. Since these files are (traditionally) written by hand, make has a lot of magic to let you shorten the rules. For example, it knows that a foo.o can be built from a foo.c, and it knows what the command to do so is. Thus, the above rule could be shortened to this:

make从通常称为Makefile的文件中读取其规则。由于这些文件是(传统上)手工编写的,因此make有很多魔法可以让你缩短规则。例如,它知道foo.o可以从foo.c构建,并且它知道这样做的命令是什么。因此,上述规则可以缩短为:

foo.o: foo.h

A small program consisting of three modules might have a Makefile like this:

一个由三个模块组成的小程序可能有一个像这样的Makefile:

mycmd: main.o foo.o bar.o
    $(CC) $(LDFLAGS) -o mycmd main.o foo.o bar.o
foo.o: foo.h bar.h
bar.o: bar.h

make can do more than just compile programs. A typical Makefile will have a rule to clean out unwanted files:

make可以做的不仅仅是编译程序。典型的Makefile将有一个清除不需要的文件的规则:

clean:
    rm -f *.o core myapp

Another rule might run tests:

另一条规则可能会运行测试

check: myapp
    ./myapp < test.input > test.output
    diff -u test.correct test.output

A Makefile might "build" documentation: run a tool to convert documentation from some markup language to HTML and PDF, for example.

Makefile可能会“构建”文档:例如,运行一个工具将文档从某种标记语言转换为HTML和PDF。

A Makefile might have an install rule to copy the binary program it builds to wherever the user or system administrator wants it installed.

Makefile可能有一个安装规则,可将其构建的二进制程序复制到用户或系统管理员希望安装的位置。

And so on. Since make is generic and powerful, it is typically used to automate the whole process from unpacking a source tarball to the point where the software is ready to be used by the user.

等等。由于make是通用且功能强大的,因此它通常用于自动化整个过程,从解压缩源tar包到软件已准备好供用户使用。

There is a whole lot of to learn about make if you want to learn it fully. The GNU version of make has particularly good documentation: http://www.gnu.org/software/make/manual/ has it in various forms.

如果你想完全学习,有很多东西可以学习。 GNU版本的make具有特别好的文档:http://www.gnu.org/software/make/manual/以各种形式提供它。

#4


Make often uses gcc to compile a multitude of C or C++ files.

Make经常使用gcc编译大量的C或C ++文件。

#5


Make is a tool for building any complex system where there are dependancies between the various system components, by doing the minimal amount of work necessary.

Make是一个工具,用于构建任何复杂系统,在这些系统中,各种系统组件之间存在依赖关系,通过执行必要的最少量工作。

If you want to find out all the things make can be used for, the GNU make manual is excellent.

如果你想了解所有可以使用的东西,GNU make手册非常好。

#6


make uses a Makefile in the current directory to apply a set of rules to its input arguments. Make also knows some default rules so that it executes even if it doesn't find a Makefile (or similar) file in the current directory. The rule to execute for cpp files so happens to call gcc on many systems.

make在当前目录中使用Makefile将一组规则应用于其输入参数。 Make也知道一些默认规则,即使它在当前目录中找不到Makefile(或类似)文件也会执行。执行cpp文件的规则恰好在许多系统上调用gcc。

Notice that you don't call make with the input file names but rather with rule names which reflect the output. So calling make xyz will strive to execute rule xyz which by default builds a file xyz (for example based on a source code file xyz.cpp.

请注意,您不使用输入文件名调用make,而是使用反映输出的规则名称。所以调用make xyz将努力执行规则xyz,默认情况下构建一个文件xyz(例如基于源代码文件xyz.cpp)。

#7


make is essentially an expert system for building code. You set up rules for how things are built, and what they depend on. Make can then look at the timestamps on all your files and figure out exactly what needs to be rebuilt at any time.

make本质上是构建代码的专家系统。您可以为事物的构建方式以及它们所依赖的内容设置规则。然后,Make可以查看所有文件的时间戳,并确切地确定需要在任何时间重建的内容。

gcc is the "gnu compiler collection". There are many languages it supports (C, C++, Ada, etc depending on your setup), but still it is just one tool out of many that make may use to build your system.

gcc是“gnu编译器集合”。它支持许多语言(C,C ++,Ada等,具体取决于您的设置),但它仍然是许多可用于构建系统的工具。

#8


You can use make to compile your C and C++ programs by calling gcc or g++ in your makefile to do all the compilation and linking steps, allowing you to do all these steps with one simple command. It is not a replacement for the compiler.

您可以使用make来编译C和C ++程序,方法是在makefile中调用gcc或g ++来执行所有编译和链接步骤,允许您使用一个简单的命令执行所有这些步骤。它不是编译器的替代品。

#9


gcc is a compiler like javac. You give it source files, it gives you a program.

gcc是一个像javac这样的编译器。你给它源文件,它给你一个程序。

make is a build tool. It takes a file that describes how to build the files in your project based on dependencies between files, so when you change one source file, you don't have to rebuild everything (like if you used a build script). make usually uses gcc to actually compile source files.

make是一个构建工具。它需要一个文件来描述如何根据文件之间的依赖关系在项目中构建文件,因此当您更改一个源文件时,您不必重建所有内容(如果您使用了构建脚本)。 make通常使用gcc来实际编译源文件。

#10


'gcc' is the compiler - the program that actually turns the source code into an executable. You have to tell it where the source code is, what to output, and various other things like libraries and options.

'gcc'是编译器 - 实际上将源代码转换为可执行文件的程序。您必须告诉它源代码的位置,输出内容以及库和选项等各种其他内容。

'make' is more like a scripting language for compiling programs. It's a way to hide all the details of compiling your source (all those arguments you have to pass the compiler). You script all of the above details once in the Makefile, so you don't have to type it every time for every file. It will also do nifty things like only recompile source files that have been updated, and handle dependancies (if I recompile this file, I will then need to recompile THAT file.)

'make'更像是用于编译程序的脚本语言。这是一种隐藏编译源代码的所有细节的方法(所有那些你必须通过编译器的参数)。您可以在Makefile中编写一次上述所有详细信息,因此您不必每次都为每个文件键入它。它还会做一些漂亮的事情,比如只重新编译已更新的源文件,并处理依赖性(如果我重新编译这个文件,我将需要重新编译该文件。)

#11


The biggest difference is that make is turing complete (Are makefiles Turing complete?) while gcc is not.

最大的区别是make是图灵完整的(makefile文件是否完整?)而gcc则没有。

#1


Well ... gcc is a compiler, make is a tool to help build programs. The difference is huge. You can never build a program purely using make; it's not a compiler. What make does it introduce a separate file of "rules", that describes how to go from source code to finished program. It then interprets this file, figures out what needs to be compiled, and calls gcc for you. This is very useful for larger projects, with hundreds or thousands of source code files, and to keep track of things like compiler options, include paths, and so on.

嗯... gcc是一个编译器,make是一个帮助构建程序的工具。差异很大。你永远不能纯粹使用make来构建一个程序;它不是编译器。它是什么使它引入了一个单独的“规则”文件,描述了如何从源代码转到完成的程序。然后它解释这个文件,找出需要编译的内容,并为你调用gcc。这对于具有数百或数千个源代码文件的大型项目非常有用,并且可以跟踪编译器选项,包含路径等内容。

#2


gcc compiles and/or links a single file. It supports multiple languages, but does not knows how to combine several source files into a non-trivial, running program - you will usually need at least two invocations of gcc (compile and link) to create even the simplest of programs.

gcc编译和/或链接单个文件。它支持多种语言,但不知道如何将多个源文件组合成一个非平凡的运行程序 - 您通常需要至少两次gcc(编译和链接)调用来创建最简单的程序。

Wikipedia page on GCC describes it as a "compiler system":

GCC上的*页面将其描述为“编译系统”:

The GNU Compiler Collection (usually shortened to GCC) is a compiler system produced by the GNU Project supporting various programming languages.

GNU编译器集合(通常缩写为GCC)是由GNU项目生成的编译器系统,支持各种编程语言。

make is a "build tool" that invokes the compiler (which could be gcc) in a particular sequence to compile multiple sources and link them together. It also tracks dependencies between various source files and object files that result from compilation of sources and does only the operations on components that have changed since last build.

make是一个“构建工具”,它以特定的顺序调用编译器(可能是gcc)来编译多个源并将它们链接在一起。它还跟踪源自编译源的各种源文件和目标文件之间的依赖关系,并且仅对自上次构建以来已更改的组件执行操作。

GNUmake is one popular implementation of make. The description from GNUmake is as follows:

GNUmake是make的一个流行实现。 GNUmake的描述如下:

Make is a tool which controls the generation of executables and other non-source files of a program from the program's source files.

Make是一个工具,可以从程序的源文件中控制程序的可执行文件和其他非源文件的生成。

Make gets its knowledge of how to build your program from a file called the makefile, which lists each of the non-source files and how to compute it from other files.

Make了解如何从名为makefile的文件构建程序,该文件列出了每个非源文件以及如何从其他文件计算它。

#3


gcc is a C compiler: it takes a C source file and creates machine code, either in the form of unlinked object files or as an actual executable program, which has been linked to all object modules and libraries.

gcc是一个C编译器:它接受一个C源文件,并以未链接的目标文件的形式或作为实际的可执行程序创建机器代码,该程序已链接到所有对象模块和库。

make is useful for controlling the build process of a project. A typical C program consists of several modules (.c) and header files (.h). It would be time-consuming to always compile everything after you change anything, so make is designed to only compile the parts that need to be re-compiled after a change.

make对于控制项目的构建过程很有用。典型的C程序由几个模块(.c)和头文件(.h)组成。在更改任何内容后始终编译所有内容将非常耗时,因此make旨在仅编译在更改后需要重新编译的部分。

It does this by following rules created by the programmer. For example:

它通过遵循程序员创建的规则来实现。例如:

foo.o: foo.c foo.h
    cc -c foo.c

This rule tells make that the file foo.o depends on the files foo.c and foo.h, and if either of them changes, it can be built by running the command on the second line. (The above is not actual syntax: make wants the commands indented by a TAB characters, which I can't do in this editing mode. Imagine it's there, though.)

此规则告诉make文件foo.o依赖于文件foo.c和foo.h,如果其中任何一个发生更改,则可以通过在第二行上运行命令来构建它。 (上面不是实际的语法:make想要用TAB字符缩进的命令,但在这种编辑模式下我无法做到。但想象一下,它就在那里。)

make reads its rules from a file that is usually called a Makefile. Since these files are (traditionally) written by hand, make has a lot of magic to let you shorten the rules. For example, it knows that a foo.o can be built from a foo.c, and it knows what the command to do so is. Thus, the above rule could be shortened to this:

make从通常称为Makefile的文件中读取其规则。由于这些文件是(传统上)手工编写的,因此make有很多魔法可以让你缩短规则。例如,它知道foo.o可以从foo.c构建,并且它知道这样做的命令是什么。因此,上述规则可以缩短为:

foo.o: foo.h

A small program consisting of three modules might have a Makefile like this:

一个由三个模块组成的小程序可能有一个像这样的Makefile:

mycmd: main.o foo.o bar.o
    $(CC) $(LDFLAGS) -o mycmd main.o foo.o bar.o
foo.o: foo.h bar.h
bar.o: bar.h

make can do more than just compile programs. A typical Makefile will have a rule to clean out unwanted files:

make可以做的不仅仅是编译程序。典型的Makefile将有一个清除不需要的文件的规则:

clean:
    rm -f *.o core myapp

Another rule might run tests:

另一条规则可能会运行测试

check: myapp
    ./myapp < test.input > test.output
    diff -u test.correct test.output

A Makefile might "build" documentation: run a tool to convert documentation from some markup language to HTML and PDF, for example.

Makefile可能会“构建”文档:例如,运行一个工具将文档从某种标记语言转换为HTML和PDF。

A Makefile might have an install rule to copy the binary program it builds to wherever the user or system administrator wants it installed.

Makefile可能有一个安装规则,可将其构建的二进制程序复制到用户或系统管理员希望安装的位置。

And so on. Since make is generic and powerful, it is typically used to automate the whole process from unpacking a source tarball to the point where the software is ready to be used by the user.

等等。由于make是通用且功能强大的,因此它通常用于自动化整个过程,从解压缩源tar包到软件已准备好供用户使用。

There is a whole lot of to learn about make if you want to learn it fully. The GNU version of make has particularly good documentation: http://www.gnu.org/software/make/manual/ has it in various forms.

如果你想完全学习,有很多东西可以学习。 GNU版本的make具有特别好的文档:http://www.gnu.org/software/make/manual/以各种形式提供它。

#4


Make often uses gcc to compile a multitude of C or C++ files.

Make经常使用gcc编译大量的C或C ++文件。

#5


Make is a tool for building any complex system where there are dependancies between the various system components, by doing the minimal amount of work necessary.

Make是一个工具,用于构建任何复杂系统,在这些系统中,各种系统组件之间存在依赖关系,通过执行必要的最少量工作。

If you want to find out all the things make can be used for, the GNU make manual is excellent.

如果你想了解所有可以使用的东西,GNU make手册非常好。

#6


make uses a Makefile in the current directory to apply a set of rules to its input arguments. Make also knows some default rules so that it executes even if it doesn't find a Makefile (or similar) file in the current directory. The rule to execute for cpp files so happens to call gcc on many systems.

make在当前目录中使用Makefile将一组规则应用于其输入参数。 Make也知道一些默认规则,即使它在当前目录中找不到Makefile(或类似)文件也会执行。执行cpp文件的规则恰好在许多系统上调用gcc。

Notice that you don't call make with the input file names but rather with rule names which reflect the output. So calling make xyz will strive to execute rule xyz which by default builds a file xyz (for example based on a source code file xyz.cpp.

请注意,您不使用输入文件名调用make,而是使用反映输出的规则名称。所以调用make xyz将努力执行规则xyz,默认情况下构建一个文件xyz(例如基于源代码文件xyz.cpp)。

#7


make is essentially an expert system for building code. You set up rules for how things are built, and what they depend on. Make can then look at the timestamps on all your files and figure out exactly what needs to be rebuilt at any time.

make本质上是构建代码的专家系统。您可以为事物的构建方式以及它们所依赖的内容设置规则。然后,Make可以查看所有文件的时间戳,并确切地确定需要在任何时间重建的内容。

gcc is the "gnu compiler collection". There are many languages it supports (C, C++, Ada, etc depending on your setup), but still it is just one tool out of many that make may use to build your system.

gcc是“gnu编译器集合”。它支持许多语言(C,C ++,Ada等,具体取决于您的设置),但它仍然是许多可用于构建系统的工具。

#8


You can use make to compile your C and C++ programs by calling gcc or g++ in your makefile to do all the compilation and linking steps, allowing you to do all these steps with one simple command. It is not a replacement for the compiler.

您可以使用make来编译C和C ++程序,方法是在makefile中调用gcc或g ++来执行所有编译和链接步骤,允许您使用一个简单的命令执行所有这些步骤。它不是编译器的替代品。

#9


gcc is a compiler like javac. You give it source files, it gives you a program.

gcc是一个像javac这样的编译器。你给它源文件,它给你一个程序。

make is a build tool. It takes a file that describes how to build the files in your project based on dependencies between files, so when you change one source file, you don't have to rebuild everything (like if you used a build script). make usually uses gcc to actually compile source files.

make是一个构建工具。它需要一个文件来描述如何根据文件之间的依赖关系在项目中构建文件,因此当您更改一个源文件时,您不必重建所有内容(如果您使用了构建脚本)。 make通常使用gcc来实际编译源文件。

#10


'gcc' is the compiler - the program that actually turns the source code into an executable. You have to tell it where the source code is, what to output, and various other things like libraries and options.

'gcc'是编译器 - 实际上将源代码转换为可执行文件的程序。您必须告诉它源代码的位置,输出内容以及库和选项等各种其他内容。

'make' is more like a scripting language for compiling programs. It's a way to hide all the details of compiling your source (all those arguments you have to pass the compiler). You script all of the above details once in the Makefile, so you don't have to type it every time for every file. It will also do nifty things like only recompile source files that have been updated, and handle dependancies (if I recompile this file, I will then need to recompile THAT file.)

'make'更像是用于编译程序的脚本语言。这是一种隐藏编译源代码的所有细节的方法(所有那些你必须通过编译器的参数)。您可以在Makefile中编写一次上述所有详细信息,因此您不必每次都为每个文件键入它。它还会做一些漂亮的事情,比如只重新编译已更新的源文件,并处理依赖性(如果我重新编译这个文件,我将需要重新编译该文件。)

#11


The biggest difference is that make is turing complete (Are makefiles Turing complete?) while gcc is not.

最大的区别是make是图灵完整的(makefile文件是否完整?)而gcc则没有。