In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use the Make command under Linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
How does Make work
For those who don't know the mechanism behind it, the make command receives the target like a command-line argument. These targets are usually stored in a special file named "Makefile", which also contains actions corresponding 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 for the target and its dependencies. If these dependencies are targets themselves, continue to establish their dependencies for the scan Makefile for these dependencies, and then compile them. Once the primary dependency has been compiled, the primary target is then compiled (which is passed in through the make command).
Now, suppose you make changes to a source file, and you execute the make command again, which will compile only the target files associated with that source file, so compiling the final executable saves a lot of time.
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
The following is the content of the project:
$ls
AnotherTest.c Makefile test.c test.h
Here is the content of Makefile:
All: test
Test: test.o anotherTest.o
Gcc-Wall test.o anotherTest.o-o test
Test.o: test.c
Gcc-c-Wall test.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 make commands under Linux:
1. A simple example.
To compile the entire project, you can simply use make or take the target all after the make 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 created by the make command for the first time as well as the actual target.
If you look at the contents of the directory again, there are some .o files and execution files in it:
$ls
AnotherTest.c anotherTest.o Makefile test test.c test.h test.o
Now, suppose you make some changes to the test.c file and re-use make to compile the 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, while another Test.o has not been recompiled.
Now clean up all the target files and executable test, you can use the target clean:
$make clean
Rm-rf * .o test
$ls
AnotherTest.c Makefile test.c test.h
You can see that all .o files and executable test have been deleted.
two。 Make all targets always re-established with the-B option
So far, you may have noticed that the make command does not compile files that have not changed since the last compilation, but if you want to override the default behavior of make, you can use the-B option.
Here is an example:
$make
Make: Nothing to be done for `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 target files and the final execution file.
3. Use the-d option to print debugging information
If you want to know what make actually does during 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; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for x86_64-pc-linux-gnu
Reading makefiles...
Reading makefile `Makefile'...
Updating makefiles... .
Considering target file `Makefile'.
Looking for an 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 long output, and you can see that I used the more command to display the output page by page.
4. Use the-C option to change the directory
You can provide different directory paths for the make command, and you will change 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 of 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 be done for `all'.
Make: Leaving directory `/ home/himanshu/practice/make-dir
You can see that the make command is first cut to a specific directory, executed there, and then switched back.
5. Treat other files as Makefile with the-f option
If you want to rename the Makefile file, such as my_makefile or something else, we want make to think of it as Makefile, using the-f option.
Make-f my_makefile
In this way, the make command chooses to scan my_makefile instead of Makefile.
The above is all the contents of this article entitled "how to use Make commands under Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.