Linux programming-Problems with multiple source files (Chapter Nineth)

Source: Internet
Author: User

Nineth Chapter Development Tools
This chapter describes some of the program development tools in Linux systems, some of which can also be used in UNIX systems. The Linux system provides a set of tools, each of which can accomplish a separate task, in addition to the compilers and debuggers required by the developer. and allows developers to combine them creatively, and this combination is what Linux inherits from Unix's philosophical system.
Problems with more than 9.1 source files
When you write a small program, many people recompile the application by recompiling all the files after editing the source file. But for large programs, using this simple approach can lead to some obvious problems. Edit--compile--the cycle of testing this cycle will become longer. If only one source file is changed, You may need to recompile all the source files.
If you create multiple headers in your program and include them in different source files, this approach can lead to a potentially more serious problem. For example, there are 3 header files a.h,b.h,c.h,3 A C source file main.c,2.c and 3.c, as shown below:
/* MAIN.C */
#include "A.h"
...
/* 2.C */
#include "A.h"
#include "B.h"
...
/* 3.C */
#include "B.h"
#include "c.h"
...
If the programmer only modified the header file c.h, then the source files Main.c and 2.c do not need to recompile, because they do not rely on the header file, and for the source file 3.c, because it contains the header file c.h, so after the header file c.h changes, you must compile it. If you forget to recompile, The final program is probably not working properly.
The Make tool solves this problem by recompiling all affected source files as necessary.
The make command is not only used to compile the program, it can be used to accomplish tasks whenever it is necessary to generate output files through multiple input files, and other uses include document processing (for example, for Troff or Tex documents)
9.2 make command and makefile file
Although the make command has a lot of intelligence built-in, it doesn't know how to build an application by itself. You must provide it with a file that tells it how the application should be constructed, and this file is called Makefile.
Makefile files are typically placed in the same directory as the other source files of the project. In fact, if you are managing a large project, you can manage different parts of the project separately using several different makefile files.
The combination of the make command and the makefile file provides a very powerful tool in the field of project management. It is not only used to control the compilation of source code, but also for manual page writing and application installation to the target directory.
The grammar of 9.2.1 Makefile
The makefile file consists of a set of dependencies and rules. Each dependency consists of a target (that is, the file to be created) and a set of source files on which the target depends. The rule describes how to create a target from these dependent files. In general, the target is a separate executable file.
The make command reads the contents of the makefile file, determines the target file or the file to be created, and then compares the date and time of the source file on which the target depends to determine which rule to use to construct the target. Usually before you create the final target file, It needs to create some intermediate targets first. The make command determines the order in which the target files are created and the correct rule invocation order according to the makefile file.
Options and parameters for the 9.2.2 make command
The make program itself has a number of options, including the 3 most commonly used options shown in the school:
-K: Its purpose is to have the make command continue to execute when an error is found, rather than stopping when the first error is detected. You can use this option to discover all the source files that were not compiled successfully in one operation.
-N: Its purpose is to have the make command output the steps that will be performed, rather than actually performing these operations.
-F <filename>: Its role is to tell the make command which file to use as the makefile file. If this option is not used, the standard version of the Make command will first look for a file named Makefile in the current directory. If the file does not exist, It looks for a file named makefile. By convention many Linux programmers use file name makefile, because if a directory is a file with lowercase letters, the makefile file will appear first in the directory's file list.
To instruct the make command to create a specific target (usually an executable file), the name of the target can be used as a parameter to the make command.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux programming-Problems with multiple source files (Chapter Nineth)

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.