Write with me Makefile (ii) _makefile

Source: Internet
Author: User

Write Makefile with me.


Chenhao

Overview
——

What is makefile. Maybe a lot of WINODWS programmers don't know this, because the Windows IDE does the work for you, but I think that to be a good and professional programmer, Makefile still have to understand. It's like there are so many HTML editors now, but if you want to be a professional, you still have to understand the meaning of the HTML logo. Especially in Unix software compiled, you can not write makefile, will not write makefile, from a side to explain whether a person has the ability to complete large-scale projects.

Because, makefile is related to the compilation rules of the whole project. A project in the source file does not count, its type, function, modules are placed in several directories, makefile defined a series of rules to specify which files need to compile first, which files need to compile, which files need to recompile, and even more complex functional operations, Because makefile is like a shell script, it can also execute operating system commands.

The benefit of makefile IS-"automated compilation", once written, only need a make command, the entire project completely automatic compilation, greatly improve the efficiency of software development. Make is a command tool, a command tool that interprets instructions in makefile, and in general, most Ides have this command, such as: Delphi's Make,visual C + + Nmake,linux under GNU. It can be seen that makefile has become a method of compiling engineering.

There are fewer articles on how to write makefile, which is why I want to write this article. Of course, different manufacturers make different, but also have various syntax, but its essence is in the "file dependencies" on the fuss, here, I only to the GNU make, my environment is Redhat Linux 8.0,make version is 3.80. Surely, this make is the most widely used and the most used. It is also the most consistent with the IEEE 1003.2-1992 Standard (POSIX.2).

In this document, will be the source code for C/A + + as our foundation, so there must be some knowledge of C + + compilation, related to this aspect, and please see the relevant compiler documentation. The default compiler here is GCC and cc under UNIX.

About compiling and linking programs
——————————

Here, I would like to say more about the procedures of compiling some of the norms and methods, in general, whether C, C + +, or PAS, the first to compile the source file into the intermediate code file, under Windows is the. obj file, Unix is an. o file, that is, Object file, This action is called compilation (compile). Then a lot of object file is synthesized to execute the file, this action is called link.

At compile time, the compiler needs the correct syntax, the declaration of functions and variables. For the latter, it is usually the location where you need to tell the compiler header file (the header file should be just a declaration, and the definition should be placed in a C + + file), and as long as all the syntax is correct, the compiler can compile the intermediate target file. In general, each source file should correspond to an intermediate target file (o file or obj file).

Links are mainly linked functions and global variables, so we can use these intermediate target files (o files or obj files) to link our applications. The linker does not care about the source file where the function resides, just the intermediate object file of the function, most of the time, because there are too many source files, there are too many intermediate target files generated by the compilation, and when the link needs to clearly indicate the intermediate target filename, which is inconvenient for compiling, We're going to pack the middle target file, which is called "Library file" in Windows, the. lib file, under Unix, is archive file, which is. A.

To sum up, the source file first generates an intermediate target file, which is then generated by the intermediate target file. At compile time, the compiler detects only the program syntax, and whether the function or variable is declared. If the function is not declared, the compiler gives a warning, but can generate object File. And when you link a program, the linker will look for the implementation of the function in all object file, if cannot find, that will report link error code (Linker error), in VC, this kind of error is generally: Link 2001 error, meaning say, linker failed to find the realization of function. You need to specify the object File for the function.

Well, to be sure, GNU make has a lot of content, gossip less, or let's start.

Makefile Introduction
———————

When the make command executes, a Makefile file is required to tell the make command how to compile and link the program.

First, we use an example to illustrate the writing rules of Makefile. In order to give everyone a sense of awareness. This example comes from the GNU Make Manual, in which our project has 8 C files, and 3 header files, and we're going to write a makefile to tell make commands how to compile and link the files. Our Rules are:
1 if the project has not been compiled, then all of our C files will be compiled and linked.
2 If a few C files of this project are modified, we only compile the modified C file and link the target program.
3 If the header file of the project is changed, we need to compile the C file that references the header files and link the target program.

As long as our makefile is well written, all of this we can do with just one make command, and the make command automatically intelligently determines which files need to be recompiled based on the current file modifications to compile the required files and linked target programs.


I. Rules of the MAKEFILE

Before telling this makefile, let's take a rough look at the makefile rules.

Target ...: Prerequisites ...
Command
...
...

Target is either an object file, or it can be an executable file. It can also be a label (label), which is described in the following "pseudo target" section for the label.

Prerequisites is the file or target that is needed to generate the target.

command is what you need to execute. (Arbitrary shell commands)

This is a file dependency, that is, target files of one or more objects depend on the files in prerequisites, and their generation rules are defined in the command. The white point is that if more than one file in prerequisites is newer than the target file, the commands defined by the command are executed. This is the makefile rule. Which is the core content of makefile.

At the end of the story, that's what makefile is all about, as if my document should be over. Oh. Not all, this is the main line and the core of makefile, but to write a good makefile is not enough, I will be followed by 1.1 points to combine my work experience to you slowly come. There's a lot of content. :)


Second sample

As mentioned earlier, if a project has 3 headers, and 8 C files, our makefile should look like this in order to complete the three rules mentioned above.

EDIT:MAIN.O KBD.O COMMAND.O display.o/
INSERT.O SEARCH.O FILES.O UTILS.O
Cc-o edit MAIN.O kbd.o command.o display.o/
INSERT.O SEARCH.O FILES.O UTILS.O

MAIN.O:MAIN.C Defs.h
Cc-c MAIN.C
KBD.O:KBD.C Defs.h Command.h
Cc-c KBD.C
COMMAND.O:COMMAND.C Defs.h Command.h
Cc-c COMMAND.C
DISPLAY.O:DISPLAY.C Defs.h Buffer.h
Cc-c display.c
Insert.o:insert.c Defs.h Buffer.h
Cc-c insert.c
SEARCH.O:SEARCH.C Defs.h Buffer.h
Cc-c search.c
FILES.O:FILES.C defs.h buffer.h Command.h
Cc-c FILES.C
UTILS.O:UTILS.C Defs.h
Cc-c UTILS.C
Clean:
RM Edit MAIN.O KBD.O COMMAND.O display.o/
INSERT.O SEARCH.O FILES.O UTILS.O

The backslash (/) is the meaning of the line break. This makes it easier for makefile to read easily. We can save this content in a file "Makefile" or "Makefile", and then enter the command "make" directly in the directory to generate the Execute file edit. If you want to delete the execution file and all the intermediate target files, simply execute the "make clean".

In this makefile, the destination file (target) contains: Execute file Edit and intermediate object file (*.O), and dependent files (prerequisites) are those. c files and. h files after the colon. Each. o file has a set of dependent files, and these. o files are also dependent files for executing file edit. The essence of dependencies is the description of which files are generated by the target file, in other words, which files are updated by the target file.

After defining the dependencies, the subsequent line defines how to generate the operating system commands for the target file, and must begin with a TAB key. Remember that make does not matter how the command works, but he executes the commands that are defined. Make compares the modified dates of the targets and prerequisites files, and if the prerequisites file has a date that is newer than the date of the targets file, or if Target does not exist, make executes the subsequent defined command.

The point here is that clean is not a file, it's just an action name, a bit like the lable in C, with nothing in it, so that make doesn't automatically find the dependencies of the file, and does not automatically execute the commands that are defined later. To execute the subsequent command, it is obvious that the name of the lable should be indicated after the make command. Such a method is very useful, we can in a makefile to define not compile or compile-independent commands, such as program packaging, program backup, and so on.


Source: http://blog.csdn.net/haoel/article/details/2886/

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.