Make commands in Linux/unix environment

Source: Internet
Author: User

Make is a very important compilation command, both in Linux and in UNIX environments. Whether you are developing your own projects or installing applications, we often use make or make install. With make tools, we can break down large development projects into more manageable modules, and for an application that includes hundreds of source files, using make and makefile tools, you can succinctly and concisely streamline the complex interrelationships between the various source files. And so many source files, if you have to type the GCC command to compile each time, it would be a disaster for programmers. The Make tool automates compilation and can compile only those parts of the programmer that have been modified since the last compilation. Therefore, effective use of make and makefile tools can greatly improve the efficiency of project development. At the same time master make and makefile, you will not be faced with Linux under the application of software helpless.

Unfortunately, in many books on Linux applications, this powerful but very complex compilation tool is not covered in detail. Here I will give you a detailed introduction to make and its description file makefile.

Makefile file

The most important and basic function of make tools is to describe the interrelationships between source programs through makefile files and to automate the maintenance of compilation work. The makefile file needs to be written in a syntax that explains how to compile each source file and connect to build the executable file, and requires that you define dependencies between the source files. The makefile file is a common way for many compilers-including Windows NT compilers-to maintain compilation information, except that in an integrated development environment, users modify makefile files through a friendly interface.

In UNIX systems, it is customary to use Makefile as a makfile file. If you want to use a different file as a makefile, you can specify the makefile file by using the Make command option similar to the following:

$ make -f Makefile.debug

For example, a program named Prog is compiled from three C source files filea.c, FILEB.C and FILEC.C, and the library file LS, which also contains their own header files A.h, B.h, and c.h. Typically, the C compiler will output three target files filea.o, FILEB.O, and FILEC.O. Suppose FILEA.C and FILEB.C to declare that a file called defs is used, but filec.c not. That is, in both FILEA.C and FILEB.C, there are statements:

#include "defs"

The following document describes the relationships between these files:

#It is a example for describing makefile
prog : filea.o fileb.o filec.o
cc filea.o fileb.o filec.o -LS -o prog
filea.o : filea.c a.h defs
cc -c filea.c
fileb.o : fileb.c b.h defs
cc -c fileb.c
filec.o : filec.c c.h
cc -c filec.c

This description document is a simple makefile file.

From the example above, note that the first character is a line for the behavior comment of #. The first non-comment line specifies that the prog is delivered by three target files FILEA.O, FILEB.O, and FILEC.O chains. The third line describes how to create an executable file from a file that prog relies on. The next 4, 6, 8 lines specify three target files, along with the. C and. h files that they rely on and the defs files. The 5, 7, and 9 lines specify how to set goals from the files on which the target depends.

When FILEA.C or A.H files are modified after compilation, the Make tool can automatically recompile FILEA.O, and if the FILEA.C and A.H are not modified between the two compilations, and if TEST.O exists, there is no need to recompile. This dependency is particularly important in the compilation of multiple source files. This dependency definition allows the make tool to avoid many unnecessary compilation efforts. Of course, the use of shell scripts can also achieve the effect of automatic compilation, but the shell script will compile any source files, including which do not need to recompile the source files, and make The tool automatically determines which source file should be compiled based on the time that the target was last compiled and the update time of the source file on which the target depends.

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.