Introduction to make tools

Source: Internet
Author: User

Programming in Linux environment, it is very important to use the make tool and compile makefile files. Of course, the make tool program in a Windows EnvironmentProgramPersonnel are also useful.

The MAKEFILE file describes the compilation and connection rules of the entire project. These include: Which source files in the project need to be compiled, how to compile, how to create those intermediate files, how to create these intermediate files, and how to finally generate the executable files we want. Although it may seem complicated, the advantage of compiling makefile for the project is that you can use a single line of command to complete "Automated compilation". Once the correct makefile is provided, the only thing you have to do to compile the entire project is to input the make command. The entire project is fully automatically compiled, greatly improving the efficiency.

Make is a command tool that explains the rules in makefile. The MAKEFILE file describes the compilation sequence and rules of all files in the project. Makefile has its own writing format, keywords, and functions. In makefile, you can use any command provided by the system shell to complete the desired work. Makefile is used in most IDE development environments and has become a compilation method for projects.

After modifying several source files in the project, you only need to re-execute the make command, the update of the target file of the source file, the intermediate file, and the final executable program will be automatically completed based on the modification. Make compares the last modification time of the corresponding file to determine which files need to be updated and those files do not need to be updated. Execute the corresponding command for the make file to be updated to recreate it. Make does not do anything for the file that does not need to be rebuilt.

MAKEFILE fileRulesContains filesDependencyLink and update this ruleTargetRequiredCommand, As shown below:

Objective: [dependency]

[Command]

Make program accordingRulesThe process of deciding whether to execute the command defined by the rule is calledExecution rules.

Generally, make outputs the command line to be executed to the standard output device before executing the command line.Echo. However, if the command line of the rule starts with the character "@", make will not display the command to be executed when executing this command.

If you use the "-n" or "-- just-print" command line parameters of make ", make only displays the commands to be executed (including the commands starting with the "@" character), but does not actually execute these commands.

To ignore execution failures of unrelated commands, we can add a minus sign "-" before the command to tell make to ignore execution failures of this command.

By default, make runs the first in makefile.RulesThe first target of this rule is calledUltimate Goal(That is, a makefile must be updated or created.Target).

 

Now let's talk about using Scala language (continued) on the. NET platform.ArticleThe example below briefly describes how to compile the MAKEFILE file.

In the middle, the five files in the first line are source program files, and the two files in the last line are generatedTargetFile. The remaining four files are intermediate files generated during compilation.

 

Rows 1st to 8th define someVariableFor future use. The reference method of the variable is $ (variable name), which we can see in row 8th.

Rows 10th to 11th definePattern rulesSpecifies how to generate a. dll file from the. CS file. InPattern rulesMedium,TargetIsPattern character"%" File, useModeTo match the target file. The pattern character "%" in the file name can match any non-null string, which must be the same except for the pattern character. For example, %. dll matches all files ending with. dll.Pattern rulesOfDependencyYou can also use "%" in the file. The value of the mode character "%" in the file is determined by "%" in the target. For examplePattern rules%. Dll: %. CS, which indicates that all. DLL files depend on the corresponding. CS files.

InPattern rulesOfCommand, The followingAutomated Variables:

    • $ @:RulesOfTargetFile Name
    • $ <:RulesThe firstDependencyFile Name

TheRulesAll is ours.Ultimate Goal. This rule does not existCommandOnly to generate or updateDependency: Dotnet.exe and cstest.exe.

Rows 15th to 16th definedRulesSpecifies how to generate dotnet.exe from DOTNET. msil.

Rows 18th to 19th definedRulesSpecifies how to generate cstest.exe from cstest. CS.

Rows 21st to 22nd definedRulesSpecifies how to generate DOTNET. msil from DOTNET. Scala.

Rows 24th to 26th definedRulesUsed to run compiledTargetFile.

Rows 28th to 29th definedRulesUsed to clear all compiled files in the current directory.TargetClean has noDependencyFile. It has only one purpose, that is, to execute the commands defined by the target name. The targets in the MAKEFILE file that do not have any dependencies and only execute actions are calledPseudo target. Enter make clean to execute clean.TargetDefinedCommand.

 

Let's try it:

Ben @ Ben-vbox :~ /Projects/scalanet $Make
GMCs-out: runtimeframework. dll-T: Library-PKG: DOTNET-R: system. xml. LINQ. dll runtimeframework. CS
GMCs-out: DOTNET. dll-T: Library-PKG: DOTNET-R: system. xml. LINQ. dll DOTNET. CS
GMCs-out: thexmltree. dll-T: Library-PKG: DOTNET-R: system. xml. LINQ. dll thexmltree. CS
Scalac-net-xassem-path runtimeframework. dll: DOTNET. dll: thexmltree. dll DOTNET. Scala
Ilasm-output: dotnet.exe DOTNET. msil
Refreshing 'dotnet. msil', no listing file, to EXE --> 'dotnet.exe'
Operation completed successfully
GMCs-out: cstest.exe-R: runtimeframework. dll-R: DOTNET. dll-R: thexmltree. dll cstest. CS

We can see that everything is normal, and all files are compiled as we imagined:

    • Call GMCs three times to compile three. DLL files, and usePattern rules.
    • Call scalac-net to compile the DOTNET. msil file, and useRules.
    • Call ilasm to compile the dotnet.exe file, and useRules.
    • Call GMCs to compile the cstest.exe file and useRules.

Let's continue:

Ben @ Ben-vbox :~ /Projects/scalanet $Make
Make: nothing can be done as 'all '.
Ben @ Ben-vbox :~ /Projects/scalanet $Touch cstest. CS
Ben @ Ben-vbox :~ /Projects/scalanet $Make
GMCs-out: cstest.exe-R: runtimeframework. dll-R: DOTNET. dll-R: thexmltree. dll cstest. CS
Ben @ Ben-vbox :~ /Projects/scalanet $Make-NClean
Rm-f *. EXE *. dll *. msil
Ben @ Ben-vbox :~ /Projects/scalanet $Ls-L *. exe
-Rwxr-XR-x 1 Ben 4096 2009-12-27 cstest.exe
-RW-r -- 1 Ben 3584 2009-12-27 dotnet.exe

We can see that the first make command does nothing, because all the targets are up to date. Then let's assume that we modified the cstest. Cs source program file and used the make command again. As we expected, it only re-compiled the cstest. CS file. Finally, we use the-n parameter to execute the make-n clean command. It only displays the command to be executed, but does not actually execute it. Therefore, the. exe file is still in progress and has not been deleted.

 

Through the above learning, you should have a preliminary understanding of how to compile makefile files. For further study, we recommend that you use the gun make Chinese manual. This manual is very long and contains a lot of content. It can be seen that the make tool has very powerful functions.

However, I believe that the content described in this article is sufficient in most cases to believe in the "80/20 law ". As a good programmer, in the face of a complex problem, we should seek a solution that is as simple, direct, and efficient as possible, rather than complicate the implementation of a simple problem. If you want to highlight your proficiency in a certain language on a simple question, it is a very stupid and immature behavior.

 

In Windows, you can use the nmake command to replace the make tool. The MAKEFILE file is basically the same as that in Linux.

Of course, the following two things are sometimes useful:

    • Mingw: Minimalist GNU for Windows
    • Cygwin: a Linux-like environment for Windows

 

by the way, all Source Code in this article can be downloaded on the http://bitbucket.org/ben.skyiv/scalanet/downloads/ page.
You can also use the Hg clone http://bitbucket.org/ben.skyiv/scalanet/ command to download the file.
for more information about HG, see the mercurial memorandum.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.