在Linux中制作实用程序(MakeFile)

Hey folks, have you ever used IDEs? Most probably, yes. So what's your favorite one? Geany, CodeBlocks, DevC++, Eclipse, NetBeans or something else?

大家好,您曾经使用过IDE吗? 很有可能,是的。 那你最喜欢哪一个呢? Geany,CodeBlocks,DevC ++,Eclipse,NetBeans或其他?

We use IDEs for sake of ease and quick coding. What if I tell you it is making you dumber at programming because it is as it hides a lot of background details from us so this article is about it only.

我们使用IDE是为了方便快捷地进行编码。 如果我告诉您,这会使您在编程上变得笨拙,那是因为它隐藏了我们很多背景细节,因此本文仅是关于它的。

You might say ‘Hey I used the terminal on Linux and I know it's compilation done' trust me I don't mean that. You might be aware with g++ compiler or GCC (GNU C Compiler) perhaps CC (Clang's Compiler) or any other compilers (for other languages too) but it is only for one single source code and what about if you are working on a project with multiple source code and having dependencies, and need to update every time any file updates other files also need to update here make comes handy.

您可能会说:“嘿,我在Linux上使用了终端,我知道它已经完成编译了”,相信我,我不是那个意思。 您可能知道使用g ++编译器或GCC(GNU C编译器)也许使用CC(Clang的编译器)或任何其他编译器(也适用于其他语言),但这仅适用于一个源代码,如果您正在使用多个源代码且具有依赖性,并且每次文件更新时都需要更新,其他文件也需要更新,因此在这里很方便。

So what is make? It is a GNU utility to maintain groups of programs. Yes, you can use it on the terminal with the syntax:

那是什么? 它是一个GNU实用程序,用于维护程序组。 是的,您可以使用以下语法在终端上使用它:

    make  [option]... [target]...

什么牌子的? (What make does?)

This utility actually determines (automatically) which portion of the program need to recompile and issues the command to recompile them.

该实用程序实际上(自动)确定程序的哪一部分需要重新编译,并发出命令对其进行重新编译。

In this article, will discuss make's implementation using C language. Although make can be used for any other language whose compiler is compatible to run with a shell command. In fact, make can use to describe any task where some files need to update automatically from others whenever the others change.

在本文中,将讨论使用C语言进行make的实现 。 尽管make可以用于其编译器与shell命令兼容的任何其他语言。 实际上,make可以用来描述任何任务,其中只要其他文件发生更改,某些文件就需要自动从其他文件更新。

Let's have a small demonstration of make:

让我们来简单地演示一下make

Step 1: Create a C program [We created a simple program with name program1]

步骤1:创建一个C程序[我们创建了一个简单的程序,名称为program1]

Step 2: We follow command for make which is
make program1 (we used the name of the program without extension)

第2步:我们遵循make命令
make program1(我们使用的程序名称不带扩展名)

makefile 1
makefile 2
makefile 3
makefile 4

Now what will happen in this example is, make will look for program1 and when it doesn't found anywhere it will look source code with the same name. In our case, we had program1.c then make will check whether it can build from that source code that is whether necessary compilers are present or not. Now, we had C compiler and it runs the code and creates our object.

现在,在此示例中将发生的事情是,make将查找program1,并且在找不到的地方将查找具有相同名称的源代码。 在我们的例子中,我们有program1.c,然后make将检查它是否可以从该源代码进行构建,即是否存在必需的编译器。 现在,我们有了C编译器,它运行代码并创建我们的对象。

To prepare to make we need to create a file called makefile which maps the relationships among files in our program and also states the commands for updating each file. Usually, executables files are updated from object files and those object files are build by compiling the source code. After creating a makefile, each time we change some source files is sufficient for all required recompilations. The make program takes the makefile content and last modification times of the file and then decide which file For all those files which it will rebuild, it will issue the commands which are mentioned in the makefile. make execute commands present in the makefile (or sometimes can be named as Makefile). Targets are updated using make if it depends on a prerequisite files where some modification have been done since the target was last modified, or the target does not exist.

为了准备我们需要创建一个名为makefile文件,该文件的映射关系,在我们的程序文件中,也指出了命令用于更新每个文件。 通常,可执行文件是从目标文件更新的,而这些目标文件是通过编译源代码来构建的。 创建makefile后,每次更改一些源文件就足以进行所有必需的重新编译。 make程序获取makefile的内容和文件的最后修改时间,然后确定哪个文件对于将要重建的所有那些文件,它将发出makefile中提到的命令。 make执行命令(存在于makefile中)(有时可以称为Makefile)。 如果目标依赖于自上次修改目标以来已经进行了一些修改的前提文件,或者目标不存在,则使用make更新目标。

Examples:

例子:

    make

Now we will understand makefile, but before that, we need to make utility in our machine, usually make come pre-installed but in case we don't have we can download it by:

现在我们将了解makefile,但在此之前,我们需要在计算机中制作实用程序,通常会预先安装make,但是如果没有,我们可以通过以下方式下载它:

    sudo apt install build-essential

makefile 5

After installing make utility, to make sure that properly installed it in our machine we check the version of its version installed:

安装make实用程序后,为确保在我们的计算机中正确安装了该实用程序,请检查已安装版本的版本:

    make --version

makefile 6

Time to create our makefile, so what do is we create a file with any text editor (vi, vim or nano) to create a file name makefile (or Makefile, both will work).

是时候创建我们的makefile了,所以我们要做的是使用任何文本编辑器(vi,vim或nano)创建一个文件,以创建文件名makefile(或Makefile,都可以使用)。

We created our makefile:

我们创建了我们的makefile:

makefile 7

Now we will try all three options we made.

现在,我们将尝试使用所有三个选项。

First, we run make with clean and then demo.

首先,我们先运行make,然后进行演示。

You can notice the pattern that first it prints the command it is going to run then execute it.

您会注意到一种模式,它首先打印将要运行的命令,然后执行它。

Now we see the make all:

现在我们看到了全部:

makefile 8

Did you notice something?

你有注意到吗?

Yes, it produced an error as program1 doesn't exist ( we cleaned it in above using clean option). Once a command executiongives fault it stop make execution. So we create the program1 and call make all command and this time we get success.

是的,由于program1不存在,它产生了一个错误(我们在上面使用clean选项清除了它)。 一旦命令执行出现故障,它将停止执行。 因此,我们创建了program1并调用make all命令,这一次我们获得了成功。

Tips:

提示:

  1. command: make

    命令 : make

  2. is equivalent to : make all

    等效于 : 全部

While creating makefile use tabs for spacing don't mix tabs and spaces.

创建makefile时,请使用制表符分隔空格,请勿混用制表符和空格。

Recommended Articles:

推荐文章:

翻译自: https://www.includehelp.com/linux/make-utility-makefile.aspx

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值