Example detailed Linux under the Make command _linux

Source: Internet
Author: User

Objective

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.

How make is working

For those who do not know the underlying mechanism, the make command receives the target as if it were a command line parameter. These goals are usually stored in a special file named "Makefile", and the file also contains actions that correspond to the target. For more information, read a series of articles on how makefiles works.

When the make command is executed for the first time, it scans the Makefile to find the target and its dependencies. If these dependencies themselves are targets, continue to establish their dependencies for these dependency-scan Makefile and compile them. Once the master relies on compilation, the main target is compiled (this is passed in by the Make command).

Now, if you modify a source file and you execute the make command again, it will only compile the target file associated with the source file, so it saves a lot of time to compile the final executable.

Make command instance

The following is the test environment used in this article:

Os--ubunut 13.04
shell--bash 4.2.45
application--gnu make 3.81

Here is the project's content:

$ ls 
anothertest.c Makefile test.c test.h

Here's what the Makefile:

All:test 

test:test.o anothertest.o 
 gcc-wall test.o anothertest.o-o test

test.o:test.c gcc-c 
 -Wall te st.c 

anothertest.o:anothertest.c 
 gcc-c-wall anothertest.c clean 

: 
 rm-rf *.O Test

Now let's look at some examples of the application of the Make command in Linux:

1. A simple example

In order to compile the entire project, you can simply use make or take the target all after the makes command.

$ make 
gcc-c-wall test.c gcc-c-wall anothertest.c gcc-wall test.o anothertest.o-o 
test

You can see the dependencies that the make command created for the first time and the actual target.

If you look at the contents again, there are a few more. o Files and execution files:

$ ls 
anothertest.c anothertest.o Makefile test test.c test.h TEST.O

Now, let's say you made some changes to the test.c file and reuse the make compilation project:

$ make 
gcc-c-wall test.c 
gcc-wall test.o anothertest.o-o test

You can see that only TEST.O has been recompiled, yet another TEST.O has not been recompiled.

Now clear all the target files and executable file test, you can use target clean:

$ make clean
rm-rf *.o test

$ ls
anothertest.c Makefile test.c test.h

You can see all of the. o files and the execution file test are deleted.

2. Use the-B option to always re-establish all goals

So far, you may have noticed that the make command does not compile files that have not changed since the last compilation, but you can use the-B option if you want to override the default behavior of making.

Here's an example:

$ make make:nothing to is done as
' all '.

$ make-b
gcc-c-wall test.c gcc-c-wall anothertest.c gcc-wall test.o anothertest.o-o
test

You can see that although the make command does not compile any files, Make-b forces the compilation of all the target files and the final execution files.

3. Print debugging information using the-D option

If you want to know what you actually did with make execution, use the-D option.

This is an example:

 $ make-d | more GNU make 3.81 Copyright (C) 2006 Free Software Foundation, Inc. This is free software;
The source for copying conditions. There is NO warranty;

Not even for merchantability or FITNESS for A particular purpose.
This is built for X86_64-pc-linux-gnu Reading makefiles ...
Reading makefile ' makefile ' ...
Updating makefiles .....
Considering target file ' Makefile '.
Looking for a implicit rule for ' Makefile '.
Trying pattern rule with stem ' Makefile '.
Trying implicit prerequisite ' makefile.o '.
Trying pattern rule with stem ' Makefile '.
Trying implicit prerequisite ' makefile.c '.
Trying pattern rule with stem ' Makefile '.
Trying implicit prerequisite ' makefile.cc '.
Trying pattern rule with stem ' Makefile '.
Trying implicit prerequisite ' makefile.c '.
Trying pattern rule with stem ' Makefile '.
Trying implicit prerequisite ' Makefile.cpp '.
Trying pattern rule with stem ' Makefile '. --more--

This is a very long output and you also see that I used the more command to show the output on a page.

4. Use the-C option to change the directory

You can provide a different directory path for make commands and switch directories before looking for Makefile.

This is a directory, assuming you are in the current directory:

$ ls 
file file2 frnd frnd1.cpp log1.txt log3.txt log5.txt file1 file name with spaces Frnd1 frnd.cpp log2.txt log4
. txt

But the Makefile file for the make command you want to run is saved in the. /make-dir/directory, you can do this:

$ make-c. /make-dir/ 
make:entering directory '/home/himanshu/practice/make-dir ' make:nothing to is done as 
' all '. 
Make:leaving directory '/home/himanshu/practice/make-dir

You can see that the make command first cuts to a specific directory, executes there, and then switches back.

5. View other files as Makefile with the-f option

If you want to rename a Makefile file, such as a my_makefile or some other name, we want make to use it as a Makefile, and the-f option can be used.

Make-f My_makefile

In this way, the make command chooses to scan my_makefile instead of makefile.

Summarize

The above is about the full content of the Linux make command, the article introduced by the example or very detailed, I hope the content of this article for everyone's study or work can bring some help, if you have questions you can message exchange.

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.