在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(我們使用的程序名稱不帶擴展名)
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:
例子:
makeNow 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-essentialAfter installing make utility, to make sure that properly installed it in our machine we check the version of its version installed:
安裝make實用程序后,為確保在我們的計算機中正確安裝了該實用程序,請檢查已安裝版本的版本:
make --version .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}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:
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:
現在我們看到了全部:
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:
提示:
command: make
命令 : make
is equivalent to : make all
等效于 : 全部
While creating makefile use tabs for spacing don't mix tabs and spaces.
創建makefile時,請使用制表符分隔空格,請勿混用制表符和空格。
Recommended Articles:
推薦文章:
Compression techniques in Linux
Linux中的壓縮技術
Archiving Files Using Linux Command Line
使用Linux命令行歸檔文件
ZIP files on command line in Linux
Linux中命令行上的ZIP文件
翻譯自: https://www.includehelp.com/linux/make-utility-makefile.aspx
總結
以上是生活随笔為你收集整理的在Linux中制作实用程序(MakeFile)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: filterwriter_Java Fi
- 下一篇: ot协议是什么_OT的完整形式是什么?