Makefile-is very important!

Source: Internet
Author: User

http://blog.csdn.net/ruglcc/article/details/7814546

This article for the reprint, is the original author series of articles of the aggregation plus callout.

support original, please go to the vast God blog:

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


Makefile is important.


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.

0.1 about program compiling and linking

    here, I would like to say more about the code and methods of program compilation, in general, whether C, C + +, or PAS, the first to compile the source file into the intermediate code file , which is in Windows. obj file, under Unix is an. o file, or Object file, which is called compilation (compile) . Then a lot of object file is synthesized to execute the file, this action is called link.   &NBSP
     
      &NBSP 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). &NBSP
        links , 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" , the. lib file under Windows, under UNIX, archive file, or. 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 objectfile of the function.

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

1 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 for this project has been changed, we need to compile the C file referencing 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.

The rules of 1.1 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. :)

1.2 An example

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.

How 1.3 make Works

In the default way, we just enter the make command. So

Make will find a file named "Makefile" or "Makefile" in the current directory.   If found, it will find the first target file in the file, in the example above, he will find "edit" this file, and the file as the final target file.   If the edit file does not exist, or if the subsequent. o file on which edit is dependent is newer than the edit file, he will execute the command defined later to generate the edit file. If the. o file that edit relies on also exists, make will find a dependency on the target. o file in the current file, and then generate an. o file based on that rule if found. (This is a bit like a stack process) of course, your C files and h files are there, so make generates the. o file and then declares the final task of make with the. o file, which is the execution file edit.

    This is the dependency of the entire make, and makes will find the dependencies of the files layer by layer until the first target file is finally compiled. In the process of searching, if there is an error, such as the last dependent file can not be found, then make will exit directly and error, and for the defined command errors, or the compilation is not successful, make at all ignore. Make just the dependency of the file, that is, if after I find the dependency, the file after the colon is still out, then I am sorry, I do not work.

With this analysis, we know that, like clean, which is not directly or indirectly associated with the first target file, the commands defined later will not be automatically executed, but we can show that we want to make execution. This is the command-"Make clean", which clears all the target files for recompilation .

So in our programming, if this project has been compiled, when we have modified one of the source files, such as file.c, then depending on our dependence, our target FILE.O will be recompiled (that is, the command defined after this dependency relationship), so the file.o file is up to date, so FILE.O's file modification time is newer than edit, so edit will be relink (see edit The command defined after the document is marked.

And if we change the "Command.h", then KDB.O, COMMAND.O, and FILES.O will be recompiled, and edit will be linked again.

using variables in 1.4 makefile

In the example above, let's look at the rules for Edit:

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

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.