Build a MASM development environment and learn to write makefile

Source: Internet
Author: User

Question: [original] Would you like to leave ide alone and use the mysterious makefile to enjoy pure code writing? -- Build a MASM development environment with you to learn to write makefile
Author: liuzewei
Time: 2007-12-21,23: 24
Chain: http://bbs.pediy.com/showthread.php? T = 56912

//
// Preface
//
If you have downloaded some open-source code from the Internet, you may have been named makefile files.
A strong interest. To unveil its secret, you don't just need to use NotePad to open it and look at its strange generation.
Code. Because our ide has built up a wall between them and, aside from the existing IDE, we must
We need to find a set of alternative tools, which are often very unfamiliar to us. You may still find them after many exciting attempts.
You cannot take that key first step. Today, we will take this step together to enter a new world.

Note
It follows hands-on step by step. After I have explored it, I can have a perceptual knowledge.

//
// Overview
//
To take this step, we need to do two things:
1. Build a development environment (we use MASM as an example, C ++ and other similar classes) to break away from the limitations of the inherent ide
2. Understand the most basic makefile writing method. Use the development environment we set up to write a hello makefile assembler.

//
// 1. Set up the MASM Development Environment
//
1. Components
Components required for building a development environment are: Operating System, header files and library files that use the operating system API, Source
Code compiler, resource file compiler, linker, source code editor, and makefile Interpreter

A> for the operating system, we will not find it. Set up the MASM development environment and use Windows.

B> for header files and library files, we use the existing files in the include and Lib directories of masm32.

C> source code compiler, resource compiler and linker. We use the existing

D> the source code editor has many options, including notepad, editpuls, ultraedit,
Notepad2, etc. We chose notepad2.

E> The makefile interpreter is Microsoft's nmake, which comes with VC and can be downloaded separately from the Internet.

2. Assembly
The above lists so many things that will be dizzy at first glance, right? Don't worry about them. Now let's assemble them. Assembly is to put the above
Find all the things and then "put" them together

A> Create a folder named masmide on the desktop.

B> open http://www.masm32.com/masmdl.htm, download m32v9r.zip to the desktop, decompress the package, and decompress the package.
It is a file named install.exe. Run it and install masm32 to the c drive.

C> enter the masm32 folder in the root directory of the C drive, and replace the three bin/include/lib files in the folder.
Directory to the previously created masmide directory. Copy the entire masm32 folder.
Delete because we no longer need it.
Views: 1237
File Size: 75.0 kb "src =" http://bbs.pediy.com/attachment.php? Attachmentid = 10518 & D = 1198250310 "border =" 0 "alt =" Name: masm32.jpg
Views: 1237
File Size: 75.0 kb ">

D> open http://www.flos-freeware.ch/notepad2.html and click the link next to the page to download
Notepad2, which supports syntax highlighting, simplicity, and open source ...... Text Editor. You can directly
Download the attachment notepad2.zip below this post. I have prepared fonts and colors for notepad2.
Views: 1235
File Size: 58.3 kb "src =" http://bbs.pediy.com/attachment.php? Attachmentid = 10515 & D = 1198250277 "border =" 0 "alt =" Name: downnotepad2.jpg
Views: 1235
File Size: 58.3 kb ">

E> after the text editor is downloaded, copy it to the root directory of our masmide.

F> copy or directly download the attachment nmake.zip from the VC directory, decompress the package, and copy it to the masmide object.
In the bin directory under the directory,
Views: 1226
File Size: 73.1 kb "src =" http://bbs.pediy.com/attachment.php? Attachmentid = 10520 & D = 1198250325 "border =" 0 "alt =" Name: nmake. jpg
Views: 1226
File Size: 73.1 kb ">

Let's take a rest. Go to the start of the Operating System-> Run-> Enter cmd-> press enter to open a command prompt.
Window. Enter nmake, ML, or link in the flashing prompt, and press Enter. The black window is grayed out.
A line that tells us that the command does not exist or the executable program does not exist. It seems that our work is not complete yet.
Continue.
Views: 1216
File Size: 39.8 kb "src =" http://bbs.pediy.com/attachment.php? Attachmentid = 10514 & D = 1198250277 "border =" 0 "alt =" Name: Invalid notexist. jpg
Views: 1216
File Size: 39.8 kb ">

3. Preparation
Run the program in the command prompt, the operating system (actually shell? Or cmd.exe? We don't care about it.
The execution program will be searched in the current directory. If the execution program does not exist, it will be included in the environment variable of the operating system.
If the directory does not exist, the system prompts that the program does not exist. Here, if you
It is not very clear what the environment variables are, it is best to "Research and Research" to understand what is going on. Of course, if you don't want to figure it out
It doesn't matter if it is white. skip this part directly. Download the attachment envpath.zip and unzip it. After you run it, it will be set automatically.
(It is best to copy it to our masmide directory for future use ). For friends who want to know details,
Let's discuss it in detail.

A> to build a complete MASM development environment, we need to add the complete path of the masmide/bin directory to the name
For the PATH environment variable, this is for the operating system to find the executable in our masmide/bin directory
File preparation. In addition, we need to separate the masmide/include and masmide/lib paths.
Added to the environment variables named include and Lib. They are for our compiler ml.exe and links
Link.exe locates the header file and library file while compiling and linking the program and prepares the file. Ml.exe processes the source file
When the include preprocessing command is run, the files listed after the command are searched in the directory where the source file is located.
If not found, it will be searched in the directory containing the environment variable named include, if not found
The chain will generate an error that does not exist in a certain header file and stop compilation. The link.exe is similar.

B> there are many ways to modify or add environment variables. You can use batch processing, modify the registry, and manually add the environment variables,
You can also directly add the method using the script mentioned below. Like the source code editor, each person has his/her own happiness.
Well, here I will only talk about the method that I think is better. For other methods, please search by the search engine.

C> use VB script to automatically add environment variables. This is what we are learning and selling today. The code written may be
Obviously, it's okay to download the attachment envpath.zip. Here we will cut a chart, each
A friend can take a look at the font of the Code Editor we will use. Let's just relax.
Views: 1224
File Size: 74.3 kb "src =" http://bbs.pediy.com/attachment.php? Attachmentid = 10516 & D = 1198250295 "border =" 0 "alt =" Name: envpath. jpg
Views: 1224
File Size: 74.3 kb ">

So far, all the work for building the MASM development environment has been completed. Now our masmide directory is roughly like this:
Views: 1216
File Size: 56.4 kb "src =" http://bbs.pediy.com/attachment.php? Attachmentid = 10519 & D = 1198250310 "border =" 0 "alt =" Name: masmide. jpg
Views: 1216
File Size: 56.4 kb ">

//
// II. Hello makefile
//
1. Create a new folder named hellomakefile next to the masmide folder on the desktop.
Like masmide, you can put this folder in any path)

2. Run notepad2.exe, save the blank file as hellomakefile/hellomakefile. ASM, and type 1
Complete Assembly source code ., For the complete source code, download the attachment hellomakefilesrc.zip.
Views: 1204
File Size: 72.7 kb "src =" http://bbs.pediy.com/attachment.php? Attachmentid = 10521 & D = 1198250325 "border =" 0 "alt =" Name: srcasm. jpg
Views: 1204
File Size: 72.7 kb ">

3. Create a new file named makefile (no suffix) under the hellomakefile directory and use notepad2
Open and type the corresponding code. The code with more detailed comments is provided here, so that we can view the makefile
Writing a file has a perceptual knowledge. Like the preceding VB script code, I know very little about it.
For more information, please reply and correct your mistakes so that I will not be mistaken. Okay, let's talk about it.
Let's go!

# First, the Comment symbol of makefile is #, which is only a single line comment method.

# Then, in my current understanding, makefile, like the preceding VB script, is an interpreted script.
# Language, which helps us complete repeated and complicated compilation links. That is to say, this "script language" will
# It clearly serves us with two things: Compilation and linking. At the same time, its working object is file.

# As a language, a logical design concept often exists behind it as its basis, for example
# Transition process, object-oriented, and so on. So what is makefile intended? Makefile is file-oriented,
# Or Based on the file dependency. For a process-oriented language, its source code consists of processes or functions.
. To compile a function, we must first write the function header in a certain format so that the compiler or interpreter can
# Knowing that this is a function, we need to write the function body in the next tab of the function header to complete the specific
# Action. Makefile is similar, it is based on the file dependency, so its source files are
# Rely on the relationship to complete its "mission ". Similarly, to compile a file dependency, we must first
# Write a "Header" according to the syntax specified by the header. Then, we start to write
# Let the makefile interpreter execute the actions (commands) to be completed by depending on the link ). And letter
# Slightly different from the number, a function has no tabs or spaces at the beginning of the line, but a dependency must exist.

# Let's take a look at a general view of the dependency:
# Target file name: the file on which the target file depends
# Command sequence

# Before writing makefile, let's make an analogy. For a function in a process language,
# This function will be executed only when we actively and explicitly call it, while the dependency in makefile is not
. We know that when we look at the attributes of a file, we will see the file creation time and final repair
# Change time. The working object of makefile is a file. The final modification of the target file in a dependency
# When the time exceeds the target file, the "body" of the dependency will be executed, that is, the corresponding command sequence will be
# Execution. One obvious benefit of such execution rules is that we only compile and link files that have been modified.
# This is very beneficial for a large project.

#
# The following is a complete makefile required to compile our hellomakefile.
#

# Dependency of the final executable file (equivalent to the main function)
# In this example, the command sequence is to link the OBJ file to an executable file.
Hellomakefile.exe: hellomakefile. OBJ
Link/subsystem: Windows hellomakefile. OBJ

# Sub-dependencies (equivalent to a sub-function)
# In this example, the command sequence is to compile the ASM file into an OBJ file.
Hellomakefile. OBJ: hellomakefile. ASM
ML/C/coff hellomakefile. ASM

Type the above Code into our hellomakefile/MAKEFILE file, save it, and switch to edit
The notepad2 window of the hellomakefile/hellomakefile. ASM Assembly source file. Select a file from the menu->
Run-> command-> Enter cmd-> press enter to open a command prompt window (the current path is automatically set to the source file
Path ). In this window, enter nmake and press Enter. nmake searches for makefile files in the current directory.
After the command is found, the command will be explained to help us complete the compilation and link work. For example:
Views: 1205
File Size: 131.6 kb "src =" http://bbs.pediy.com/attachment.php? Attachmentid = 10517 & D = 1198250295 "border =" 0 "alt =" Name: makefile. jpg
Views: 1205
File Size: 131.6 kb ">

Haha, do you want to cheer? Slow down. Let's go a little farther. Let's do one for the makefile above.
Click a small improvement and the code is as follows (Hey, friend, you can think about this step yourself ^ ).

# Makefile supports variables (equivalent to macros). Here we define a name variable and assign a value to hellomakefile.
Name = hellomakefile

# Define a variable named objs and assign a value to it by referencing the name variable.
# Use $ () to reference variables in makefile
Objs = $ (name). OBJ

# Two variables named link_flag and ml_flag are defined here.
# Assign values to link and compile parameters respectively.
Link_flag =/subsystem: Windows
Ml_flag =/C/coff

# The dependency is the same as the previous makefile. Here, only variables are introduced.
Alias (namecmd.exe: $ (objs)
Link $ (link_flag) $ (objs)

$ (Objs): $ (name). ASM
ML $ (ml_flag) $ (name). ASM

# Here is a special dependency. The target file is called a "pseudo target", and the makefile interpreter will execute
# The first dependency in the makefile and the last modification time of all target files are less
# Depends on the dependency of the file. The following dependency does not directly depend on the file, and of course there will be no indirect dependency.
# Dependency file. At the same time, it is not the first dependency in the MAKEFILE file. It will never be resolved.
# Releaser execution, unless we specify the interpreter for execution as a command line parameter, for example, input: nmake clean
# Purpose: complete cleanup, backup, and so on.
Clean:
Del *. OBJ

//
// More information
//
This is the result of a whole day of study. I finally learned about makefile.
How long is there. I can't help but be excited. I want to share with you the hope of friends who have been waiting outside makefile for as long as I do.
You have some help. If you need more information, we strongly recommend that you download the attachment and write makefile.zip together. This
It is an excellent makefile tutorial written by Meng Yan. Finally, it is really difficult to write or read such an article. Thank you.
Thank you for your reading.

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.